Fix GTP-U header in 4x IPv4 vector 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               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 *)(udp3+1);
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;
315             }
316           else /* ipv6 */
317             {
318               int bogus = 0;
319
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);
324
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;
337 #undef _
338 #define _(offs) copy_dst1[offs] = copy_src1[offs];
339               foreach_fixed_header6_offset;
340 #undef _
341 #define _(offs) copy_dst2[offs] = copy_src2[offs];
342               foreach_fixed_header6_offset;
343 #undef _
344 #define _(offs) copy_dst3[offs] = copy_src3[offs];
345               foreach_fixed_header6_offset;
346 #undef _
347               /* Fix IP6 payload length */
348               new_l0 =
349                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
350                                       - sizeof(*ip6_0));
351               ip6_0->payload_length = new_l0;
352               new_l1 =
353                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
354                                       - sizeof(*ip6_1));
355               ip6_1->payload_length = new_l1;
356               new_l2 =
357                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b2)
358                                       - sizeof(*ip6_2));
359               ip6_2->payload_length = new_l2;
360               new_l3 =
361                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b3)
362                                       - sizeof(*ip6_3));
363               ip6_3->payload_length = new_l3;
364
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;
378
379               /* IPv6 UDP checksum is mandatory */
380               udp0->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0,
381                                                                  ip6_0, &bogus);
382               if (udp0->checksum == 0)
383                 udp0->checksum = 0xffff;
384               udp1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b1,
385                                                         ip6_1, &bogus);
386               if (udp1->checksum == 0)
387                 udp1->checksum = 0xffff;
388               udp2->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b2,
389                                                                  ip6_2, &bogus);
390               if (udp2->checksum == 0)
391                 udp2->checksum = 0xffff;
392               udp3->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b3,
393                                                         ip6_3, &bogus);
394               if (udp3->checksum == 0)
395                 udp3->checksum = 0xffff;
396
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;
414             }
415
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;
423
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) ))
432             {
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 ) )
438                 {
439                   if (stats_n_packets)
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;
445                   stats_n_packets = 4;
446                   stats_n_bytes = len0 + len1 + len2 + len3;
447                 }
448               else
449                 {
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);
462                 }
463             }
464
465           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
466             {
467               gtpu_encap_trace_t *tr =
468                 vlib_add_trace (vm, node, b0, sizeof (*tr));
469               tr->tunnel_index = t0 - gtm->tunnels;
470               tr->teid = t0->teid;
471            }
472
473           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
474             {
475               gtpu_encap_trace_t *tr =
476                 vlib_add_trace (vm, node, b1, sizeof (*tr));
477               tr->tunnel_index = t1 - gtm->tunnels;
478               tr->teid = t1->teid;
479             }
480
481           vlib_validate_buffer_enqueue_x4 (vm, node, next_index,
482                                            to_next, n_left_to_next,
483                                            bi0, bi1, bi2, bi3,
484                                            next0, next1, next2, next3);
485         }
486
487       while (n_left_from > 0 && n_left_to_next > 0)
488         {
489           u32 bi0;
490           vlib_buffer_t * b0;
491           u32 flow_hash0;
492           u32 len0;
493           ip4_header_t * ip4_0;
494           ip6_header_t * ip6_0;
495           udp_header_t * udp0;
496           gtpu_header_t * gtpu0;
497           u64 * copy_src0, * copy_dst0;
498           u32 * copy_src_last0, * copy_dst_last0;
499           u16 new_l0;
500           ip_csum_t sum0;
501
502           bi0 = from[0];
503           to_next[0] = bi0;
504           from += 1;
505           to_next += 1;
506           n_left_from -= 1;
507           n_left_to_next -= 1;
508
509           b0 = vlib_get_buffer (vm, bi0);
510
511           flow_hash0 = vnet_l2_compute_flow_hash(b0);
512
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 = &gtm->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;
520
521           /* Apply the rewrite string. $$$$ vnet_rewrite? */
522           vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
523
524           if (is_ip4)
525             {
526               ip4_0 = vlib_buffer_get_current(b0);
527
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;
534 #undef _
535               /* Last 4 octets. Hopefully gcc will be our friend */
536               copy_dst_last0 = (u32 *)(&copy_dst0[4]);
537               copy_src_last0 = (u32 *)(&copy_src0[4]);
538               copy_dst_last0[0] = copy_src_last0[0];
539
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;
548
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)
552                                              - sizeof (*ip4_0));
553               udp0->length = new_l0;
554               udp0->src_port = flow_hash0;
555
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;
561             }
562
563           else /* ip6 path */
564             {
565               int bogus = 0;
566
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;
574 #undef _
575               /* Fix IP6 payload length */
576               new_l0 =
577                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
578                                       - sizeof(*ip6_0));
579               ip6_0->payload_length = new_l0;
580
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;
585
586               /* IPv6 UDP checksum is mandatory */
587               udp0->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0,
588                                                                  ip6_0, &bogus);
589               if (udp0->checksum == 0)
590                 udp0->checksum = 0xffff;
591
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;
597             }
598
599           pkts_encapsulated ++;
600           len0 = vlib_buffer_length_in_chain (vm, b0);
601           stats_n_packets += 1;
602           stats_n_bytes += len0;
603
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))
609             {
610               stats_n_packets -= 1;
611               stats_n_bytes -= len0;
612               if (stats_n_packets)
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);
617               stats_n_packets = 1;
618               stats_n_bytes = len0;
619               stats_sw_if_index = sw_if_index0;
620             }
621
622           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
623             {
624               gtpu_encap_trace_t *tr =
625                 vlib_add_trace (vm, node, b0, sizeof (*tr));
626               tr->tunnel_index = t0 - gtm->tunnels;
627               tr->teid = t0->teid;
628             }
629           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
630                                            to_next, n_left_to_next,
631                                            bi0, next0);
632         }
633
634       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
635     }
636
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,
640                                pkts_encapsulated);
641
642   /* Increment any remaining batch stats */
643   if (stats_n_packets)
644     {
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;
649     }
650
651   return from_frame->n_vectors;
652 }
653
654 static uword
655 gtpu4_encap (vlib_main_t * vm,
656               vlib_node_runtime_t * node,
657               vlib_frame_t * from_frame)
658 {
659   return gtpu_encap_inline (vm, node, from_frame, /* is_ip4 */ 1);
660 }
661
662 static uword
663 gtpu6_encap (vlib_main_t * vm,
664               vlib_node_runtime_t * node,
665               vlib_frame_t * from_frame)
666 {
667   return gtpu_encap_inline (vm, node, from_frame, /* is_ip4 */ 0);
668 }
669
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,
679   .next_nodes = {
680 #define _(s,n) [GTPU_ENCAP_NEXT_##s] = n,
681     foreach_gtpu_encap_next
682 #undef _
683   },
684 };
685
686 VLIB_NODE_FUNCTION_MULTIARCH (gtpu4_encap_node, gtpu4_encap)
687
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,
697   .next_nodes = {
698 #define _(s,n) [GTPU_ENCAP_NEXT_##s] = n,
699     foreach_gtpu_encap_next
700 #undef _
701   },
702 };
703
704 VLIB_NODE_FUNCTION_MULTIARCH (gtpu6_encap_node, gtpu6_encap)
705