Fix IPv6 csum calculation in GTP-U encapsulation
[vpp.git] / src / plugins / gtpu / gtpu_encap.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
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>
21
22 /* Statistics (not all errors) */
23 #define foreach_gtpu_encap_error    \
24 _(ENCAPSULATED, "good packets encapsulated")
25
26 static char * gtpu_encap_error_strings[] = {
27 #define _(sym,string) string,
28   foreach_gtpu_encap_error
29 #undef _
30 };
31
32 typedef enum {
33 #define _(sym,str) GTPU_ENCAP_ERROR_##sym,
34     foreach_gtpu_encap_error
35 #undef _
36     GTPU_ENCAP_N_ERROR,
37 } gtpu_encap_error_t;
38
39 #define foreach_gtpu_encap_next        \
40 _(DROP, "error-drop")                  \
41 _(IP4_LOOKUP, "ip4-lookup")             \
42 _(IP6_LOOKUP, "ip6-lookup")
43
44 typedef enum {
45     GTPU_ENCAP_NEXT_DROP,
46     GTPU_ENCAP_NEXT_IP4_LOOKUP,
47     GTPU_ENCAP_NEXT_IP6_LOOKUP,
48     GTPU_ENCAP_N_NEXT,
49 } gtpu_encap_next_t;
50
51 typedef struct {
52   u32 tunnel_index;
53   u32 teid;
54 } gtpu_encap_trace_t;
55
56 u8 * format_gtpu_encap_trace (u8 * s, va_list * args)
57 {
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 *);
62
63   s = format (s, "GTPU encap to gtpu_tunnel%d teid %d",
64               t->tunnel_index, t->teid);
65   return s;
66 }
67
68
69 #define foreach_fixed_header4_offset            \
70     _(0) _(1) _(2) _(3)
71
72 #define foreach_fixed_header6_offset            \
73     _(0) _(1) _(2) _(3) _(4) _(5) _(6)
74
75 always_inline uword
76 gtpu_encap_inline (vlib_main_t * vm,
77                     vlib_node_runtime_t * node,
78                     vlib_frame_t * from_frame,
79                     u32 is_ip4)
80 {
81   u32 n_left_from, next_index, * from, * to_next;
82   gtpu_main_t * gtm = &gtpu_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;
93
94   from = vlib_frame_vector_args (from_frame);
95   n_left_from = from_frame->n_vectors;
96
97   next_index = node->cached_next_index;
98   stats_sw_if_index = node->runtime_data[0];
99   stats_n_packets = stats_n_bytes = 0;
100
101   while (n_left_from > 0)
102     {
103       u32 n_left_to_next;
104
105       vlib_get_next_frame (vm, node, next_index,
106                            to_next, n_left_to_next);
107
108       while (n_left_from >= 8 && n_left_to_next >= 4)
109         {
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;
128
129           /* Prefetch next iteration. */
130           {
131             vlib_buffer_t * p4, * p5, * p6, * p7;
132
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]);
137
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);
142
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);
147           }
148
149           bi0 = from[0];
150           bi1 = from[1];
151           bi2 = from[2];
152           bi3 = from[3];
153           to_next[0] = bi0;
154           to_next[1] = bi1;
155           to_next[2] = bi2;
156           to_next[3] = bi3;
157           from += 4;
158           to_next += 4;
159           n_left_to_next -= 4;
160           n_left_from -= 4;
161
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);
166
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);
171
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 = &gtm->tunnels[hi0->dev_instance];
182           t1 = &gtm->tunnels[hi1->dev_instance];
183           t2 = &gtm->tunnels[hi2->dev_instance];
184           t3 = &gtm->tunnels[hi3->dev_instance];
185
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;
195
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));
201
202           if (is_ip4)
203             {
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);
208
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;
218
219               /* Copy first 32 octets 8-bytes at a time */
220 #define _(offs) copy_dst0[offs] = copy_src0[offs];
221               foreach_fixed_header4_offset;
222 #undef _
223 #define _(offs) copy_dst1[offs] = copy_src1[offs];
224               foreach_fixed_header4_offset;
225 #undef _
226 #define _(offs) copy_dst2[offs] = copy_src2[offs];
227               foreach_fixed_header4_offset;
228 #undef _
229 #define _(offs) copy_dst3[offs] = copy_src3[offs];
230               foreach_fixed_header4_offset;
231 #undef _
232               /* Last 4 octets. Hopefully gcc will be our friend */
233               copy_dst_last0 = (u32 *)(&copy_dst0[4]);
234               copy_src_last0 = (u32 *)(&copy_src0[4]);
235               copy_dst_last0[0] = copy_src_last0[0];
236               copy_dst_last1 = (u32 *)(&copy_dst1[4]);
237               copy_src_last1 = (u32 *)(&copy_src1[4]);
238               copy_dst_last1[0] = copy_src_last1[0];
239               copy_dst_last2 = (u32 *)(&copy_dst2[4]);
240               copy_src_last2 = (u32 *)(&copy_src2[4]);
241               copy_dst_last2[0] = copy_src_last2[0];
242               copy_dst_last3 = (u32 *)(&copy_dst3[4]);
243               copy_src_last3 = (u32 *)(&copy_src3[4]);
244               copy_dst_last3[0] = copy_src_last3[0];
245
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;
275
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)
279                                              - sizeof (*ip4_0));
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)
284                                              - sizeof (*ip4_1));
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)
289                                              - sizeof (*ip4_2));
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)
294                                              - sizeof (*ip4_3));
295               udp3->length = new_l3;
296               udp3->src_port = flow_hash3;
297
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                                              - GTPU_V1_HDR_LEN);
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)
307                                              - GTPU_V1_HDR_LEN);
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)
312                                              - GTPU_V1_HDR_LEN);
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)
317                                              - GTPU_V1_HDR_LEN);
318               gtpu3->length = new_l3;
319             }
320           else /* ipv6 */
321             {
322               int bogus = 0;
323
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);
328
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;
341 #undef _
342 #define _(offs) copy_dst1[offs] = copy_src1[offs];
343               foreach_fixed_header6_offset;
344 #undef _
345 #define _(offs) copy_dst2[offs] = copy_src2[offs];
346               foreach_fixed_header6_offset;
347 #undef _
348 #define _(offs) copy_dst3[offs] = copy_src3[offs];
349               foreach_fixed_header6_offset;
350 #undef _
351               /* Fix IP6 payload length */
352               new_l0 =
353                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
354                                       - sizeof(*ip6_0));
355               ip6_0->payload_length = new_l0;
356               new_l1 =
357                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
358                                       - sizeof(*ip6_1));
359               ip6_1->payload_length = new_l1;
360               new_l2 =
361                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b2)
362                                       - sizeof(*ip6_2));
363               ip6_2->payload_length = new_l2;
364               new_l3 =
365                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b3)
366                                       - sizeof(*ip6_3));
367               ip6_3->payload_length = new_l3;
368
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;
382
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)
387                                              - GTPU_V1_HDR_LEN);
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)
392                                              - GTPU_V1_HDR_LEN);
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)
397                                              - GTPU_V1_HDR_LEN);
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)
402                                              - GTPU_V1_HDR_LEN);
403               gtpu3->length = new_l3;
404
405               /* IPv6 UDP checksum is mandatory */
406               udp0->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0,
407                                                                  ip6_0, &bogus);
408               if (udp0->checksum == 0)
409                 udp0->checksum = 0xffff;
410               udp1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b1,
411                                                                  ip6_1, &bogus);
412               if (udp1->checksum == 0)
413                 udp1->checksum = 0xffff;
414               udp2->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b2,
415                                                                  ip6_2, &bogus);
416               if (udp2->checksum == 0)
417                 udp2->checksum = 0xffff;
418               udp3->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b3,
419                                                                  ip6_3, &bogus);
420               if (udp3->checksum == 0)
421                 udp3->checksum = 0xffff;
422
423             }
424
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;
432
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) ))
441             {
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 ) )
447                 {
448                   if (stats_n_packets)
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;
454                   stats_n_packets = 4;
455                   stats_n_bytes = len0 + len1 + len2 + len3;
456                 }
457               else
458                 {
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);
471                 }
472             }
473
474           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
475             {
476               gtpu_encap_trace_t *tr =
477                 vlib_add_trace (vm, node, b0, sizeof (*tr));
478               tr->tunnel_index = t0 - gtm->tunnels;
479               tr->teid = t0->teid;
480            }
481
482           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
483             {
484               gtpu_encap_trace_t *tr =
485                 vlib_add_trace (vm, node, b1, sizeof (*tr));
486               tr->tunnel_index = t1 - gtm->tunnels;
487               tr->teid = t1->teid;
488             }
489
490           vlib_validate_buffer_enqueue_x4 (vm, node, next_index,
491                                            to_next, n_left_to_next,
492                                            bi0, bi1, bi2, bi3,
493                                            next0, next1, next2, next3);
494         }
495
496       while (n_left_from > 0 && n_left_to_next > 0)
497         {
498           u32 bi0;
499           vlib_buffer_t * b0;
500           u32 flow_hash0;
501           u32 len0;
502           ip4_header_t * ip4_0;
503           ip6_header_t * ip6_0;
504           udp_header_t * udp0;
505           gtpu_header_t * gtpu0;
506           u64 * copy_src0, * copy_dst0;
507           u32 * copy_src_last0, * copy_dst_last0;
508           u16 new_l0;
509           ip_csum_t sum0;
510
511           bi0 = from[0];
512           to_next[0] = bi0;
513           from += 1;
514           to_next += 1;
515           n_left_from -= 1;
516           n_left_to_next -= 1;
517
518           b0 = vlib_get_buffer (vm, bi0);
519
520           flow_hash0 = vnet_l2_compute_flow_hash(b0);
521
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 = &gtm->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;
529
530           /* Apply the rewrite string. $$$$ vnet_rewrite? */
531           vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
532
533           if (is_ip4)
534             {
535               ip4_0 = vlib_buffer_get_current(b0);
536
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;
543 #undef _
544               /* Last 4 octets. Hopefully gcc will be our friend */
545               copy_dst_last0 = (u32 *)(&copy_dst0[4]);
546               copy_src_last0 = (u32 *)(&copy_src0[4]);
547               copy_dst_last0[0] = copy_src_last0[0];
548
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;
557
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)
561                                              - sizeof (*ip4_0));
562               udp0->length = new_l0;
563               udp0->src_port = flow_hash0;
564
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)
569                                              - GTPU_V1_HDR_LEN);
570               gtpu0->length = new_l0;
571             }
572
573           else /* ip6 path */
574             {
575               int bogus = 0;
576
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;
584 #undef _
585               /* Fix IP6 payload length */
586               new_l0 =
587                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
588                                       - sizeof(*ip6_0));
589               ip6_0->payload_length = new_l0;
590
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;
595
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)
600                                              - GTPU_V1_HDR_LEN);
601               gtpu0->length = new_l0;
602
603               /* IPv6 UDP checksum is mandatory */
604               udp0->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0,
605                                                                  ip6_0, &bogus);
606               if (udp0->checksum == 0)
607                 udp0->checksum = 0xffff;
608             }
609
610           pkts_encapsulated ++;
611           len0 = vlib_buffer_length_in_chain (vm, b0);
612           stats_n_packets += 1;
613           stats_n_bytes += len0;
614
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))
620             {
621               stats_n_packets -= 1;
622               stats_n_bytes -= len0;
623               if (stats_n_packets)
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);
628               stats_n_packets = 1;
629               stats_n_bytes = len0;
630               stats_sw_if_index = sw_if_index0;
631             }
632
633           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
634             {
635               gtpu_encap_trace_t *tr =
636                 vlib_add_trace (vm, node, b0, sizeof (*tr));
637               tr->tunnel_index = t0 - gtm->tunnels;
638               tr->teid = t0->teid;
639             }
640           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
641                                            to_next, n_left_to_next,
642                                            bi0, next0);
643         }
644
645       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
646     }
647
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,
651                                pkts_encapsulated);
652
653   /* Increment any remaining batch stats */
654   if (stats_n_packets)
655     {
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;
660     }
661
662   return from_frame->n_vectors;
663 }
664
665 static uword
666 gtpu4_encap (vlib_main_t * vm,
667               vlib_node_runtime_t * node,
668               vlib_frame_t * from_frame)
669 {
670   return gtpu_encap_inline (vm, node, from_frame, /* is_ip4 */ 1);
671 }
672
673 static uword
674 gtpu6_encap (vlib_main_t * vm,
675               vlib_node_runtime_t * node,
676               vlib_frame_t * from_frame)
677 {
678   return gtpu_encap_inline (vm, node, from_frame, /* is_ip4 */ 0);
679 }
680
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,
690   .next_nodes = {
691 #define _(s,n) [GTPU_ENCAP_NEXT_##s] = n,
692     foreach_gtpu_encap_next
693 #undef _
694   },
695 };
696
697 VLIB_NODE_FUNCTION_MULTIARCH (gtpu4_encap_node, gtpu4_encap)
698
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,
708   .next_nodes = {
709 #define _(s,n) [GTPU_ENCAP_NEXT_##s] = n,
710     foreach_gtpu_encap_next
711 #undef _
712   },
713 };
714
715 VLIB_NODE_FUNCTION_MULTIARCH (gtpu6_encap_node, gtpu6_encap)
716