4442c42ad66a7d55605652fda718bb03f176a7cc
[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               /* IPv6 UDP checksum is mandatory */
384               udp0->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0,
385                                                                  ip6_0, &bogus);
386               if (udp0->checksum == 0)
387                 udp0->checksum = 0xffff;
388               udp1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b1,
389                                                         ip6_1, &bogus);
390               if (udp1->checksum == 0)
391                 udp1->checksum = 0xffff;
392               udp2->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b2,
393                                                                  ip6_2, &bogus);
394               if (udp2->checksum == 0)
395                 udp2->checksum = 0xffff;
396               udp3->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b3,
397                                                         ip6_3, &bogus);
398               if (udp3->checksum == 0)
399                 udp3->checksum = 0xffff;
400
401               /* Fix GTPU length */
402               gtpu0 = (gtpu_header_t *)(udp0+1);
403               new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
404                                              - sizeof (*ip6_0) - sizeof(*udp0)
405                                              - GTPU_V1_HDR_LEN);
406               gtpu0->length = new_l0;
407               gtpu1 = (gtpu_header_t *)(udp1+1);
408               new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b1)
409                                              - sizeof (*ip6_1) - sizeof(*udp1)
410                                              - GTPU_V1_HDR_LEN);
411               gtpu1->length = new_l1;
412               gtpu2 = (gtpu_header_t *)(udp2+1);
413               new_l2 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b2)
414                                              - sizeof (*ip6_2) - sizeof(*udp2)
415                                              - GTPU_V1_HDR_LEN);
416               gtpu2->length = new_l2;
417               gtpu3 = (gtpu_header_t *)(udp3+1);
418               new_l3 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b3)
419                                              - sizeof (*ip6_3) - sizeof(*udp3)
420                                              - GTPU_V1_HDR_LEN);
421               gtpu3->length = new_l3;
422             }
423
424           pkts_encapsulated += 4;
425           len0 = vlib_buffer_length_in_chain (vm, b0);
426           len1 = vlib_buffer_length_in_chain (vm, b1);
427           len2 = vlib_buffer_length_in_chain (vm, b2);
428           len3 = vlib_buffer_length_in_chain (vm, b3);
429           stats_n_packets += 4;
430           stats_n_bytes += len0 + len1 + len2 + len3;
431
432           /* Batch stats increment on the same gtpu tunnel so counter is not
433              incremented per packet. Note stats are still incremented for deleted
434              and admin-down tunnel where packets are dropped. It is not worthwhile
435              to check for this rare case and affect normal path performance. */
436           if (PREDICT_FALSE ((sw_if_index0 != stats_sw_if_index) ||
437                              (sw_if_index1 != stats_sw_if_index) ||
438                              (sw_if_index2 != stats_sw_if_index) ||
439                              (sw_if_index3 != stats_sw_if_index) ))
440             {
441               stats_n_packets -= 4;
442               stats_n_bytes -= len0 + len1 + len2 + len3;
443               if ( (sw_if_index0 == sw_if_index1 ) &&
444                    (sw_if_index1 == sw_if_index2 ) &&
445                    (sw_if_index2 == sw_if_index3 ) )
446                 {
447                   if (stats_n_packets)
448                     vlib_increment_combined_counter
449                       (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
450                        thread_index, stats_sw_if_index,
451                        stats_n_packets, stats_n_bytes);
452                   stats_sw_if_index = sw_if_index0;
453                   stats_n_packets = 4;
454                   stats_n_bytes = len0 + len1 + len2 + len3;
455                 }
456               else
457                 {
458                   vlib_increment_combined_counter
459                       (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
460                        thread_index, sw_if_index0, 1, len0);
461                   vlib_increment_combined_counter
462                       (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
463                        thread_index, sw_if_index1, 1, len1);
464                   vlib_increment_combined_counter
465                       (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
466                        thread_index, sw_if_index2, 1, len2);
467                   vlib_increment_combined_counter
468                       (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
469                        thread_index, sw_if_index3, 1, len3);
470                 }
471             }
472
473           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
474             {
475               gtpu_encap_trace_t *tr =
476                 vlib_add_trace (vm, node, b0, sizeof (*tr));
477               tr->tunnel_index = t0 - gtm->tunnels;
478               tr->teid = t0->teid;
479            }
480
481           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
482             {
483               gtpu_encap_trace_t *tr =
484                 vlib_add_trace (vm, node, b1, sizeof (*tr));
485               tr->tunnel_index = t1 - gtm->tunnels;
486               tr->teid = t1->teid;
487             }
488
489           vlib_validate_buffer_enqueue_x4 (vm, node, next_index,
490                                            to_next, n_left_to_next,
491                                            bi0, bi1, bi2, bi3,
492                                            next0, next1, next2, next3);
493         }
494
495       while (n_left_from > 0 && n_left_to_next > 0)
496         {
497           u32 bi0;
498           vlib_buffer_t * b0;
499           u32 flow_hash0;
500           u32 len0;
501           ip4_header_t * ip4_0;
502           ip6_header_t * ip6_0;
503           udp_header_t * udp0;
504           gtpu_header_t * gtpu0;
505           u64 * copy_src0, * copy_dst0;
506           u32 * copy_src_last0, * copy_dst_last0;
507           u16 new_l0;
508           ip_csum_t sum0;
509
510           bi0 = from[0];
511           to_next[0] = bi0;
512           from += 1;
513           to_next += 1;
514           n_left_from -= 1;
515           n_left_to_next -= 1;
516
517           b0 = vlib_get_buffer (vm, bi0);
518
519           flow_hash0 = vnet_l2_compute_flow_hash(b0);
520
521           /* Get next node index and adj index from tunnel next_dpo */
522           sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
523           hi0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
524           t0 = &gtm->tunnels[hi0->dev_instance];
525           /* Note: change to always set next0 if it may be set to drop */
526           next0 = t0->next_dpo.dpoi_next_node;
527           vnet_buffer(b0)->ip.adj_index[VLIB_TX] = t0->next_dpo.dpoi_index;
528
529           /* Apply the rewrite string. $$$$ vnet_rewrite? */
530           vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
531
532           if (is_ip4)
533             {
534               ip4_0 = vlib_buffer_get_current(b0);
535
536               /* Copy the fixed header */
537               copy_dst0 = (u64 *) ip4_0;
538               copy_src0 = (u64 *) t0->rewrite;
539               /* Copy first 32 octets 8-bytes at a time */
540 #define _(offs) copy_dst0[offs] = copy_src0[offs];
541               foreach_fixed_header4_offset;
542 #undef _
543               /* Last 4 octets. Hopefully gcc will be our friend */
544               copy_dst_last0 = (u32 *)(&copy_dst0[4]);
545               copy_src_last0 = (u32 *)(&copy_src0[4]);
546               copy_dst_last0[0] = copy_src_last0[0];
547
548               /* Fix the IP4 checksum and length */
549               sum0 = ip4_0->checksum;
550               new_l0 = /* old_l0 always 0, see the rewrite setup */
551                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
552               sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
553                                      length /* changed member */);
554               ip4_0->checksum = ip_csum_fold (sum0);
555               ip4_0->length = new_l0;
556
557               /* Fix UDP length and set source port */
558               udp0 = (udp_header_t *)(ip4_0+1);
559               new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
560                                              - sizeof (*ip4_0));
561               udp0->length = new_l0;
562               udp0->src_port = flow_hash0;
563
564               /* Fix GTPU length */
565               gtpu0 = (gtpu_header_t *)(udp0+1);
566               new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
567                                              - sizeof (*ip4_0) - sizeof(*udp0)
568                                              - GTPU_V1_HDR_LEN);
569               gtpu0->length = new_l0;
570             }
571
572           else /* ip6 path */
573             {
574               int bogus = 0;
575
576               ip6_0 = vlib_buffer_get_current(b0);
577               /* Copy the fixed header */
578               copy_dst0 = (u64 *) ip6_0;
579               copy_src0 = (u64 *) t0->rewrite;
580               /* Copy first 56 (ip6) octets 8-bytes at a time */
581 #define _(offs) copy_dst0[offs] = copy_src0[offs];
582               foreach_fixed_header6_offset;
583 #undef _
584               /* Fix IP6 payload length */
585               new_l0 =
586                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
587                                       - sizeof(*ip6_0));
588               ip6_0->payload_length = new_l0;
589
590               /* Fix UDP length  and set source port */
591               udp0 = (udp_header_t *)(ip6_0+1);
592               udp0->length = new_l0;
593               udp0->src_port = flow_hash0;
594
595               /* IPv6 UDP checksum is mandatory */
596               udp0->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0,
597                                                                  ip6_0, &bogus);
598               if (udp0->checksum == 0)
599                 udp0->checksum = 0xffff;
600
601               /* Fix GTPU length */
602               gtpu0 = (gtpu_header_t *)(udp0+1);
603               new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
604                                              - sizeof (*ip4_0) - sizeof(*udp0)
605                                              - GTPU_V1_HDR_LEN);
606               gtpu0->length = new_l0;
607             }
608
609           pkts_encapsulated ++;
610           len0 = vlib_buffer_length_in_chain (vm, b0);
611           stats_n_packets += 1;
612           stats_n_bytes += len0;
613
614           /* Batch stats increment on the same gtpu tunnel so counter is not
615              incremented per packet. Note stats are still incremented for deleted
616              and admin-down tunnel where packets are dropped. It is not worthwhile
617              to check for this rare case and affect normal path performance. */
618           if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
619             {
620               stats_n_packets -= 1;
621               stats_n_bytes -= len0;
622               if (stats_n_packets)
623                 vlib_increment_combined_counter
624                   (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
625                    thread_index, stats_sw_if_index,
626                    stats_n_packets, stats_n_bytes);
627               stats_n_packets = 1;
628               stats_n_bytes = len0;
629               stats_sw_if_index = sw_if_index0;
630             }
631
632           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
633             {
634               gtpu_encap_trace_t *tr =
635                 vlib_add_trace (vm, node, b0, sizeof (*tr));
636               tr->tunnel_index = t0 - gtm->tunnels;
637               tr->teid = t0->teid;
638             }
639           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
640                                            to_next, n_left_to_next,
641                                            bi0, next0);
642         }
643
644       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
645     }
646
647   /* Do we still need this now that tunnel tx stats is kept? */
648   vlib_node_increment_counter (vm, node->node_index,
649                                GTPU_ENCAP_ERROR_ENCAPSULATED,
650                                pkts_encapsulated);
651
652   /* Increment any remaining batch stats */
653   if (stats_n_packets)
654     {
655       vlib_increment_combined_counter
656         (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
657          thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
658       node->runtime_data[0] = stats_sw_if_index;
659     }
660
661   return from_frame->n_vectors;
662 }
663
664 static uword
665 gtpu4_encap (vlib_main_t * vm,
666               vlib_node_runtime_t * node,
667               vlib_frame_t * from_frame)
668 {
669   return gtpu_encap_inline (vm, node, from_frame, /* is_ip4 */ 1);
670 }
671
672 static uword
673 gtpu6_encap (vlib_main_t * vm,
674               vlib_node_runtime_t * node,
675               vlib_frame_t * from_frame)
676 {
677   return gtpu_encap_inline (vm, node, from_frame, /* is_ip4 */ 0);
678 }
679
680 VLIB_REGISTER_NODE (gtpu4_encap_node) = {
681   .function = gtpu4_encap,
682   .name = "gtpu4-encap",
683   .vector_size = sizeof (u32),
684   .format_trace = format_gtpu_encap_trace,
685   .type = VLIB_NODE_TYPE_INTERNAL,
686   .n_errors = ARRAY_LEN(gtpu_encap_error_strings),
687   .error_strings = gtpu_encap_error_strings,
688   .n_next_nodes = GTPU_ENCAP_N_NEXT,
689   .next_nodes = {
690 #define _(s,n) [GTPU_ENCAP_NEXT_##s] = n,
691     foreach_gtpu_encap_next
692 #undef _
693   },
694 };
695
696 VLIB_NODE_FUNCTION_MULTIARCH (gtpu4_encap_node, gtpu4_encap)
697
698 VLIB_REGISTER_NODE (gtpu6_encap_node) = {
699   .function = gtpu6_encap,
700   .name = "gtpu6-encap",
701   .vector_size = sizeof (u32),
702   .format_trace = format_gtpu_encap_trace,
703   .type = VLIB_NODE_TYPE_INTERNAL,
704   .n_errors = ARRAY_LEN(gtpu_encap_error_strings),
705   .error_strings = gtpu_encap_error_strings,
706   .n_next_nodes = GTPU_ENCAP_N_NEXT,
707   .next_nodes = {
708 #define _(s,n) [GTPU_ENCAP_NEXT_##s] = n,
709     foreach_gtpu_encap_next
710 #undef _
711   },
712 };
713
714 VLIB_NODE_FUNCTION_MULTIARCH (gtpu6_encap_node, gtpu6_encap)
715