virtio: Add RX queue full statisitics
[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
52 #define foreach_fixed_header4_offset            \
53     _(0) _(1) _(2) _(3)
54
55 #define foreach_fixed_header6_offset            \
56     _(0) _(1) _(2) _(3) _(4) _(5) _(6)
57
58 always_inline uword
59 gtpu_encap_inline (vlib_main_t * vm,
60                     vlib_node_runtime_t * node,
61                     vlib_frame_t * from_frame,
62                     u32 is_ip4)
63 {
64   u32 n_left_from, next_index, * from, * to_next;
65   gtpu_main_t * gtm = &gtpu_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;
76
77   from = vlib_frame_vector_args (from_frame);
78   n_left_from = from_frame->n_vectors;
79
80   next_index = node->cached_next_index;
81   stats_sw_if_index = node->runtime_data[0];
82   stats_n_packets = stats_n_bytes = 0;
83
84   while (n_left_from > 0)
85     {
86       u32 n_left_to_next;
87
88       vlib_get_next_frame (vm, node, next_index,
89                            to_next, n_left_to_next);
90
91       while (n_left_from >= 8 && n_left_to_next >= 4)
92         {
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;
111
112           /* Prefetch next iteration. */
113           {
114             vlib_buffer_t * p4, * p5, * p6, * p7;
115
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]);
120
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);
125
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);
130           }
131
132           bi0 = from[0];
133           bi1 = from[1];
134           bi2 = from[2];
135           bi3 = from[3];
136           to_next[0] = bi0;
137           to_next[1] = bi1;
138           to_next[2] = bi2;
139           to_next[3] = bi3;
140           from += 4;
141           to_next += 4;
142           n_left_to_next -= 4;
143           n_left_from -= 4;
144
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);
149
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);
154
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 = &gtm->tunnels[hi0->dev_instance];
165           t1 = &gtm->tunnels[hi1->dev_instance];
166           t2 = &gtm->tunnels[hi2->dev_instance];
167           t3 = &gtm->tunnels[hi3->dev_instance];
168
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;
178
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));
184
185           if (is_ip4)
186             {
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);
191
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;
201
202               /* Copy first 32 octets 8-bytes at a time (minimum size)
203                * TODO: check if clib_memcpy_fast is better */
204 #define _(offs) copy_dst0[offs] = copy_src0[offs];
205               foreach_fixed_header4_offset;
206 #undef _
207 #define _(offs) copy_dst1[offs] = copy_src1[offs];
208               foreach_fixed_header4_offset;
209 #undef _
210 #define _(offs) copy_dst2[offs] = copy_src2[offs];
211               foreach_fixed_header4_offset;
212 #undef _
213 #define _(offs) copy_dst3[offs] = copy_src3[offs];
214               foreach_fixed_header4_offset;
215 #undef _
216
217               /* Copy last octets */
218               if (_vec_len (t0->rewrite) == 36)
219                 {
220                   /* Last 4 octets. Hopefully gcc will be our friend */
221                   copy_dst_last0 = (u32 *) (&copy_dst0[4]);
222                   copy_src_last0 = (u32 *) (&copy_src0[4]);
223                   copy_dst_last0[0] = copy_src_last0[0];
224                 }
225               else
226                 {
227                   /* Near last 8 octets. */
228 #define _(offs) copy_dst0[offs] = copy_src0[offs];
229                   _ (4);
230 #undef _
231                   /* Last 4 octets. Hopefully gcc will be our friend */
232                   copy_dst_last0 = (u32 *) (&copy_dst0[5]);
233                   copy_src_last0 = (u32 *) (&copy_src0[5]);
234                   copy_dst_last0[0] = copy_src_last0[0];
235                 }
236
237               if (_vec_len (t1->rewrite) == 36)
238                 {
239                   /* Last 4 octets. Hopefully gcc will be our friend */
240                   copy_dst_last1 = (u32 *) (&copy_dst1[4]);
241                   copy_src_last1 = (u32 *) (&copy_src1[4]);
242                   copy_dst_last1[0] = copy_src_last1[0];
243                 }
244               else
245                 {
246                   /* Near last 8 octets. */
247 #define _(offs) copy_dst1[offs] = copy_src1[offs];
248                   _ (4);
249 #undef _
250                   /* Last 4 octets. Hopefully gcc will be our friend */
251                   copy_dst_last1 = (u32 *) (&copy_dst1[5]);
252                   copy_src_last1 = (u32 *) (&copy_src1[5]);
253                   copy_dst_last1[0] = copy_src_last1[0];
254                 }
255
256               if (_vec_len (t2->rewrite) == 36)
257                 {
258                   /* Last 4 octets. Hopefully gcc will be our friend */
259                   copy_dst_last2 = (u32 *) (&copy_dst2[4]);
260                   copy_src_last2 = (u32 *) (&copy_src2[4]);
261                   copy_dst_last2[0] = copy_src_last2[0];
262                 }
263               else
264                 {
265                   /* Near last 8 octets. */
266 #define _(offs) copy_dst2[offs] = copy_src2[offs];
267                   _ (4);
268 #undef _
269                   /* Last 4 octets. Hopefully gcc will be our friend */
270                   copy_dst_last2 = (u32 *) (&copy_dst2[5]);
271                   copy_src_last2 = (u32 *) (&copy_src2[5]);
272                   copy_dst_last2[0] = copy_src_last2[0];
273                 }
274
275               if (_vec_len (t3->rewrite) == 36)
276                 {
277                   /* Last 4 octets. Hopefully gcc will be our friend */
278                   copy_dst_last3 = (u32 *) (&copy_dst3[4]);
279                   copy_src_last3 = (u32 *) (&copy_src3[4]);
280                   copy_dst_last3[0] = copy_src_last3[0];
281                 }
282               else
283                 {
284                   /* Near last 8 octets. */
285 #define _(offs) copy_dst3[offs] = copy_src3[offs];
286                   _ (4);
287 #undef _
288                   /* Last 4 octets. Hopefully gcc will be our friend */
289                   copy_dst_last3 = (u32 *) (&copy_dst3[5]);
290                   copy_src_last3 = (u32 *) (&copy_src3[5]);
291                   copy_dst_last3[0] = copy_src_last3[0];
292                 }
293
294               /* Fix the IP4 checksum and length */
295               sum0 = ip4_0->checksum;
296               new_l0 = /* old_l0 always 0, see the rewrite setup */
297                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
298               sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
299                                      length /* changed member */);
300               ip4_0->checksum = ip_csum_fold (sum0);
301               ip4_0->length = new_l0;
302               sum1 = ip4_1->checksum;
303               new_l1 = /* old_l1 always 0, see the rewrite setup */
304                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
305               sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
306                                      length /* changed member */);
307               ip4_1->checksum = ip_csum_fold (sum1);
308               ip4_1->length = new_l1;
309               sum2 = ip4_2->checksum;
310               new_l2 = /* old_l0 always 0, see the rewrite setup */
311                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b2));
312               sum2 = ip_csum_update (sum2, old_l2, new_l2, ip4_header_t,
313                                      length /* changed member */);
314               ip4_2->checksum = ip_csum_fold (sum2);
315               ip4_2->length = new_l2;
316               sum3 = ip4_3->checksum;
317               new_l3 = /* old_l1 always 0, see the rewrite setup */
318                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b3));
319               sum3 = ip_csum_update (sum3, old_l3, new_l3, ip4_header_t,
320                                      length /* changed member */);
321               ip4_3->checksum = ip_csum_fold (sum3);
322               ip4_3->length = new_l3;
323
324               /* Fix UDP length and set source port */
325               udp0 = (udp_header_t *)(ip4_0+1);
326               new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
327                                              - sizeof (*ip4_0));
328               udp0->length = new_l0;
329               udp0->src_port = flow_hash0;
330               udp1 = (udp_header_t *)(ip4_1+1);
331               new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b1)
332                                              - sizeof (*ip4_1));
333               udp1->length = new_l1;
334               udp1->src_port = flow_hash1;
335               udp2 = (udp_header_t *)(ip4_2+1);
336               new_l2 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b2)
337                                              - sizeof (*ip4_2));
338               udp2->length = new_l2;
339               udp2->src_port = flow_hash2;
340               udp3 = (udp_header_t *)(ip4_3+1);
341               new_l3 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b3)
342                                              - sizeof (*ip4_3));
343               udp3->length = new_l3;
344               udp3->src_port = flow_hash3;
345
346               /* Fix GTPU length */
347               gtpu0 = (gtpu_header_t *)(udp0+1);
348               new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
349                                              - sizeof (*ip4_0) - sizeof(*udp0)
350                                              - GTPU_V1_HDR_LEN);
351               gtpu0->length = new_l0;
352               gtpu1 = (gtpu_header_t *)(udp1+1);
353               new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b1)
354                                              - sizeof (*ip4_1) - sizeof(*udp1)
355                                              - GTPU_V1_HDR_LEN);
356               gtpu1->length = new_l1;
357               gtpu2 = (gtpu_header_t *)(udp2+1);
358               new_l2 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b2)
359                                              - sizeof (*ip4_2) - sizeof(*udp2)
360                                              - GTPU_V1_HDR_LEN);
361               gtpu2->length = new_l2;
362               gtpu3 = (gtpu_header_t *)(udp3+1);
363               new_l3 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b3)
364                                              - sizeof (*ip4_3) - sizeof(*udp3)
365                                              - GTPU_V1_HDR_LEN);
366               gtpu3->length = new_l3;
367             }
368           else /* ipv6 */
369             {
370               int bogus = 0;
371
372               ip6_0 = vlib_buffer_get_current(b0);
373               ip6_1 = vlib_buffer_get_current(b1);
374               ip6_2 = vlib_buffer_get_current(b2);
375               ip6_3 = vlib_buffer_get_current(b3);
376
377               /* Copy the fixed header */
378               copy_dst0 = (u64 *) ip6_0;
379               copy_src0 = (u64 *) t0->rewrite;
380               copy_dst1 = (u64 *) ip6_1;
381               copy_src1 = (u64 *) t1->rewrite;
382               copy_dst2 = (u64 *) ip6_2;
383               copy_src2 = (u64 *) t2->rewrite;
384               copy_dst3 = (u64 *) ip6_3;
385               copy_src3 = (u64 *) t3->rewrite;
386               /* Copy first 56 (ip6) octets 8-bytes at a time (minimum size) */
387 #define _(offs) copy_dst0[offs] = copy_src0[offs];
388               foreach_fixed_header6_offset;
389 #undef _
390 #define _(offs) copy_dst1[offs] = copy_src1[offs];
391               foreach_fixed_header6_offset;
392 #undef _
393 #define _(offs) copy_dst2[offs] = copy_src2[offs];
394               foreach_fixed_header6_offset;
395 #undef _
396 #define _(offs) copy_dst3[offs] = copy_src3[offs];
397               foreach_fixed_header6_offset;
398 #undef _
399
400               /* Copy last octets */
401               if (_vec_len (t0->rewrite) == 64)
402                 {
403                   /* Last 8 octets.  */
404 #define _(offs) copy_dst0[offs] = copy_src0[offs];
405                   _ (7);
406 #undef _
407                 }
408
409               if (_vec_len (t1->rewrite) == 64)
410                 {
411                   /* Last 8 octets.  */
412 #define _(offs) copy_dst1[offs] = copy_src1[offs];
413                   _ (7);
414 #undef _
415                 }
416
417               if (_vec_len (t2->rewrite) == 64)
418                 {
419                   /* Last 8 octets.  */
420 #define _(offs) copy_dst2[offs] = copy_src2[offs];
421                   _ (7);
422 #undef _
423                 }
424
425               if (_vec_len (t3->rewrite) == 64)
426                 {
427                   /* Last 8 octets.  */
428 #define _(offs) copy_dst3[offs] = copy_src3[offs];
429                   _ (7);
430 #undef _
431                 }
432
433               /* Fix IP6 payload length */
434               new_l0 =
435                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
436                                       - sizeof(*ip6_0));
437               ip6_0->payload_length = new_l0;
438               new_l1 =
439                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
440                                       - sizeof(*ip6_1));
441               ip6_1->payload_length = new_l1;
442               new_l2 =
443                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b2)
444                                       - sizeof(*ip6_2));
445               ip6_2->payload_length = new_l2;
446               new_l3 =
447                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b3)
448                                       - sizeof(*ip6_3));
449               ip6_3->payload_length = new_l3;
450
451               /* Fix UDP length  and set source port */
452               udp0 = (udp_header_t *)(ip6_0+1);
453               udp0->length = new_l0;
454               udp0->src_port = flow_hash0;
455               udp1 = (udp_header_t *)(ip6_1+1);
456               udp1->length = new_l1;
457               udp1->src_port = flow_hash1;
458               udp2 = (udp_header_t *)(ip6_2+1);
459               udp2->length = new_l2;
460               udp2->src_port = flow_hash2;
461               udp3 = (udp_header_t *)(ip6_3+1);
462               udp3->length = new_l3;
463               udp3->src_port = flow_hash3;
464
465               /* Fix GTPU length */
466               gtpu0 = (gtpu_header_t *)(udp0+1);
467               new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
468                                              - sizeof (*ip6_0) - sizeof(*udp0)
469                                              - GTPU_V1_HDR_LEN);
470               gtpu0->length = new_l0;
471               gtpu1 = (gtpu_header_t *)(udp1+1);
472               new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b1)
473                                              - sizeof (*ip6_1) - sizeof(*udp1)
474                                              - GTPU_V1_HDR_LEN);
475               gtpu1->length = new_l1;
476               gtpu2 = (gtpu_header_t *)(udp2+1);
477               new_l2 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b2)
478                                              - sizeof (*ip6_2) - sizeof(*udp2)
479                                              - GTPU_V1_HDR_LEN);
480               gtpu2->length = new_l2;
481               gtpu3 = (gtpu_header_t *)(udp3+1);
482               new_l3 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b3)
483                                              - sizeof (*ip6_3) - sizeof(*udp3)
484                                              - GTPU_V1_HDR_LEN);
485               gtpu3->length = new_l3;
486
487               /* IPv6 UDP checksum is mandatory */
488               udp0->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0,
489                                                                  ip6_0, &bogus);
490               if (udp0->checksum == 0)
491                 udp0->checksum = 0xffff;
492               udp1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b1,
493                                                                  ip6_1, &bogus);
494               if (udp1->checksum == 0)
495                 udp1->checksum = 0xffff;
496               udp2->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b2,
497                                                                  ip6_2, &bogus);
498               if (udp2->checksum == 0)
499                 udp2->checksum = 0xffff;
500               udp3->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b3,
501                                                                  ip6_3, &bogus);
502               if (udp3->checksum == 0)
503                 udp3->checksum = 0xffff;
504
505             }
506
507           pkts_encapsulated += 4;
508           len0 = vlib_buffer_length_in_chain (vm, b0);
509           len1 = vlib_buffer_length_in_chain (vm, b1);
510           len2 = vlib_buffer_length_in_chain (vm, b2);
511           len3 = vlib_buffer_length_in_chain (vm, b3);
512           stats_n_packets += 4;
513           stats_n_bytes += len0 + len1 + len2 + len3;
514
515           /* save inner packet flow_hash for load-balance node */
516           vnet_buffer (b0)->ip.flow_hash = flow_hash0;
517           vnet_buffer (b1)->ip.flow_hash = flow_hash1;
518           vnet_buffer (b2)->ip.flow_hash = flow_hash2;
519           vnet_buffer (b3)->ip.flow_hash = flow_hash3;
520
521           /* Batch stats increment on the same gtpu tunnel so counter is not
522              incremented per packet. Note stats are still incremented for deleted
523              and admin-down tunnel where packets are dropped. It is not worthwhile
524              to check for this rare case and affect normal path performance. */
525           if (PREDICT_FALSE ((sw_if_index0 != stats_sw_if_index) ||
526                              (sw_if_index1 != stats_sw_if_index) ||
527                              (sw_if_index2 != stats_sw_if_index) ||
528                              (sw_if_index3 != stats_sw_if_index) ))
529             {
530               stats_n_packets -= 4;
531               stats_n_bytes -= len0 + len1 + len2 + len3;
532               if ( (sw_if_index0 == sw_if_index1 ) &&
533                    (sw_if_index1 == sw_if_index2 ) &&
534                    (sw_if_index2 == sw_if_index3 ) )
535                 {
536                   if (stats_n_packets)
537                     vlib_increment_combined_counter
538                       (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
539                        thread_index, stats_sw_if_index,
540                        stats_n_packets, stats_n_bytes);
541                   stats_sw_if_index = sw_if_index0;
542                   stats_n_packets = 4;
543                   stats_n_bytes = len0 + len1 + len2 + len3;
544                 }
545               else
546                 {
547                   vlib_increment_combined_counter
548                       (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
549                        thread_index, sw_if_index0, 1, len0);
550                   vlib_increment_combined_counter
551                       (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
552                        thread_index, sw_if_index1, 1, len1);
553                   vlib_increment_combined_counter
554                       (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
555                        thread_index, sw_if_index2, 1, len2);
556                   vlib_increment_combined_counter
557                       (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
558                        thread_index, sw_if_index3, 1, len3);
559                 }
560             }
561
562           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
563             {
564               gtpu_encap_trace_t *tr =
565                 vlib_add_trace (vm, node, b0, sizeof (*tr));
566               tr->tunnel_index = t0 - gtm->tunnels;
567               tr->tteid = t0->tteid;
568               tr->pdu_extension = t0->pdu_extension;
569               tr->qfi = t0->qfi;
570             }
571
572           if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
573             {
574               gtpu_encap_trace_t *tr =
575                 vlib_add_trace (vm, node, b1, sizeof (*tr));
576               tr->tunnel_index = t1 - gtm->tunnels;
577               tr->tteid = t1->tteid;
578               tr->pdu_extension = t1->pdu_extension;
579               tr->qfi = t1->qfi;
580             }
581
582           if (PREDICT_FALSE(b2->flags & VLIB_BUFFER_IS_TRACED))
583             {
584               gtpu_encap_trace_t *tr =
585                 vlib_add_trace (vm, node, b2, sizeof (*tr));
586               tr->tunnel_index = t2 - gtm->tunnels;
587               tr->tteid = t2->tteid;
588               tr->pdu_extension = t2->pdu_extension;
589               tr->qfi = t2->qfi;
590             }
591
592           if (PREDICT_FALSE (b3->flags & VLIB_BUFFER_IS_TRACED))
593             {
594               gtpu_encap_trace_t *tr =
595                 vlib_add_trace (vm, node, b3, sizeof (*tr));
596               tr->tunnel_index = t3 - gtm->tunnels;
597               tr->tteid = t3->tteid;
598               tr->pdu_extension = t3->pdu_extension;
599               tr->qfi = t3->qfi;
600             }
601
602           vlib_validate_buffer_enqueue_x4 (vm, node, next_index,
603                                            to_next, n_left_to_next,
604                                            bi0, bi1, bi2, bi3,
605                                            next0, next1, next2, next3);
606         }
607
608       while (n_left_from > 0 && n_left_to_next > 0)
609         {
610           u32 bi0;
611           vlib_buffer_t * b0;
612           u32 flow_hash0;
613           u32 len0;
614           ip4_header_t * ip4_0;
615           ip6_header_t * ip6_0;
616           udp_header_t * udp0;
617           gtpu_header_t * gtpu0;
618           u64 * copy_src0, * copy_dst0;
619           u32 * copy_src_last0, * copy_dst_last0;
620           u16 new_l0;
621           ip_csum_t sum0;
622
623           bi0 = from[0];
624           to_next[0] = bi0;
625           from += 1;
626           to_next += 1;
627           n_left_from -= 1;
628           n_left_to_next -= 1;
629
630           b0 = vlib_get_buffer (vm, bi0);
631
632           flow_hash0 = vnet_l2_compute_flow_hash(b0);
633
634           /* Get next node index and adj index from tunnel next_dpo */
635           sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
636           hi0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
637           t0 = &gtm->tunnels[hi0->dev_instance];
638           /* Note: change to always set next0 if it may be set to drop */
639           next0 = t0->next_dpo.dpoi_next_node;
640           vnet_buffer(b0)->ip.adj_index[VLIB_TX] = t0->next_dpo.dpoi_index;
641
642           /* Apply the rewrite string. $$$$ vnet_rewrite.
643            * The correct total size is set in ip_udp_gtpu_rewrite() */
644           vlib_buffer_advance (b0, -(word) _vec_len (t0->rewrite));
645
646           if (is_ip4)
647             {
648               ip4_0 = vlib_buffer_get_current(b0);
649
650               /* Copy the fixed header */
651               copy_dst0 = (u64 *) ip4_0;
652               copy_src0 = (u64 *) t0->rewrite;
653               /* Copy first 32 octets 8-bytes at a time */
654 #define _(offs) copy_dst0[offs] = copy_src0[offs];
655               foreach_fixed_header4_offset;
656 #undef _
657
658               /* Copy last octets */
659               if (_vec_len (t0->rewrite) == 36)
660                 {
661                   /* Last 4 octets. Hopefully gcc will be our friend */
662                   copy_dst_last0 = (u32 *) (&copy_dst0[4]);
663                   copy_src_last0 = (u32 *) (&copy_src0[4]);
664                   copy_dst_last0[0] = copy_src_last0[0];
665                 }
666               else
667                 {
668                   /* Near last 8 octets. */
669 #define _(offs) copy_dst0[offs] = copy_src0[offs];
670                   _ (4);
671 #undef _
672                   /* Last 4 octets. Hopefully gcc will be our friend */
673                   copy_dst_last0 = (u32 *) (&copy_dst0[5]);
674                   copy_src_last0 = (u32 *) (&copy_src0[5]);
675                   copy_dst_last0[0] = copy_src_last0[0];
676                 }
677
678               /* Fix the IP4 checksum and length */
679               sum0 = ip4_0->checksum;
680               new_l0 = /* old_l0 always 0, see the rewrite setup */
681                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
682               sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
683                                      length /* changed member */);
684               ip4_0->checksum = ip_csum_fold (sum0);
685               ip4_0->length = new_l0;
686
687               /* Fix UDP length and set source port */
688               udp0 = (udp_header_t *)(ip4_0+1);
689               new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
690                                              - sizeof (*ip4_0));
691               udp0->length = new_l0;
692               udp0->src_port = flow_hash0;
693
694               /* Fix GTPU length */
695               gtpu0 = (gtpu_header_t *)(udp0+1);
696               new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
697                                              - sizeof (*ip4_0) - sizeof(*udp0)
698                                              - GTPU_V1_HDR_LEN);
699               gtpu0->length = new_l0;
700             }
701
702           else /* ip6 path */
703             {
704               int bogus = 0;
705
706               ip6_0 = vlib_buffer_get_current(b0);
707               /* Copy the fixed header */
708               copy_dst0 = (u64 *) ip6_0;
709               copy_src0 = (u64 *) t0->rewrite;
710               /* Copy first 56 (ip6) octets 8-bytes at a time */
711 #define _(offs) copy_dst0[offs] = copy_src0[offs];
712               foreach_fixed_header6_offset;
713 #undef _
714
715               /* Copy last octets */
716               if (_vec_len (t0->rewrite) == 64)
717                 {
718                   /* Last 8 octets.  */
719 #define _(offs) copy_dst0[offs] = copy_src0[offs];
720                   _ (7);
721 #undef _
722                 }
723
724               /* Fix IP6 payload length */
725               new_l0 =
726                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
727                                       - sizeof(*ip6_0));
728               ip6_0->payload_length = new_l0;
729
730               /* Fix UDP length  and set source port */
731               udp0 = (udp_header_t *)(ip6_0+1);
732               udp0->length = new_l0;
733               udp0->src_port = flow_hash0;
734
735               /* Fix GTPU length */
736               gtpu0 = (gtpu_header_t *)(udp0+1);
737               new_l0 = clib_host_to_net_u16 (
738                 vlib_buffer_length_in_chain (vm, b0) - sizeof (*ip6_0) -
739                 sizeof (*udp0) - GTPU_V1_HDR_LEN);
740               gtpu0->length = new_l0;
741
742               /* IPv6 UDP checksum is mandatory */
743               udp0->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0,
744                                                                  ip6_0, &bogus);
745               if (udp0->checksum == 0)
746                 udp0->checksum = 0xffff;
747             }
748
749           pkts_encapsulated ++;
750           len0 = vlib_buffer_length_in_chain (vm, b0);
751           stats_n_packets += 1;
752           stats_n_bytes += len0;
753
754           /* save inner packet flow_hash for load-balance node */
755           vnet_buffer (b0)->ip.flow_hash = flow_hash0;
756
757           /* Batch stats increment on the same gtpu tunnel so counter is not
758              incremented per packet. Note stats are still incremented for deleted
759              and admin-down tunnel where packets are dropped. It is not worthwhile
760              to check for this rare case and affect normal path performance. */
761           if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
762             {
763               stats_n_packets -= 1;
764               stats_n_bytes -= len0;
765               if (stats_n_packets)
766                 vlib_increment_combined_counter
767                   (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
768                    thread_index, stats_sw_if_index,
769                    stats_n_packets, stats_n_bytes);
770               stats_n_packets = 1;
771               stats_n_bytes = len0;
772               stats_sw_if_index = sw_if_index0;
773             }
774
775           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
776             {
777               gtpu_encap_trace_t *tr =
778                 vlib_add_trace (vm, node, b0, sizeof (*tr));
779               tr->tunnel_index = t0 - gtm->tunnels;
780               tr->tteid = t0->tteid;
781               tr->pdu_extension = t0->pdu_extension;
782               tr->qfi = t0->qfi;
783             }
784           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
785                                            to_next, n_left_to_next,
786                                            bi0, next0);
787         }
788
789       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
790     }
791
792   /* Do we still need this now that tunnel tx stats is kept? */
793   vlib_node_increment_counter (vm, node->node_index,
794                                GTPU_ENCAP_ERROR_ENCAPSULATED,
795                                pkts_encapsulated);
796
797   /* Increment any remaining batch stats */
798   if (stats_n_packets)
799     {
800       vlib_increment_combined_counter
801         (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
802          thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
803       node->runtime_data[0] = stats_sw_if_index;
804     }
805
806   return from_frame->n_vectors;
807 }
808
809 VLIB_NODE_FN (gtpu4_encap_node) (vlib_main_t * vm,
810               vlib_node_runtime_t * node,
811               vlib_frame_t * from_frame)
812 {
813   return gtpu_encap_inline (vm, node, from_frame, /* is_ip4 */ 1);
814 }
815
816 VLIB_NODE_FN (gtpu6_encap_node) (vlib_main_t * vm,
817               vlib_node_runtime_t * node,
818               vlib_frame_t * from_frame)
819 {
820   return gtpu_encap_inline (vm, node, from_frame, /* is_ip4 */ 0);
821 }
822
823 VLIB_REGISTER_NODE (gtpu4_encap_node) = {
824   .name = "gtpu4-encap",
825   .vector_size = sizeof (u32),
826   .format_trace = format_gtpu_encap_trace,
827   .type = VLIB_NODE_TYPE_INTERNAL,
828   .n_errors = ARRAY_LEN(gtpu_encap_error_strings),
829   .error_strings = gtpu_encap_error_strings,
830   .n_next_nodes = GTPU_ENCAP_N_NEXT,
831   .next_nodes = {
832 #define _(s,n) [GTPU_ENCAP_NEXT_##s] = n,
833     foreach_gtpu_encap_next
834 #undef _
835   },
836 };
837
838 VLIB_REGISTER_NODE (gtpu6_encap_node) = {
839   .name = "gtpu6-encap",
840   .vector_size = sizeof (u32),
841   .format_trace = format_gtpu_encap_trace,
842   .type = VLIB_NODE_TYPE_INTERNAL,
843   .n_errors = ARRAY_LEN(gtpu_encap_error_strings),
844   .error_strings = gtpu_encap_error_strings,
845   .n_next_nodes = GTPU_ENCAP_N_NEXT,
846   .next_nodes = {
847 #define _(s,n) [GTPU_ENCAP_NEXT_##s] = n,
848     foreach_gtpu_encap_next
849 #undef _
850   },
851 };
852