2 * Copyright (c) 2017 Intel and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 #include <vppinfra/error.h>
16 #include <vppinfra/hash.h>
17 #include <vnet/vnet.h>
18 #include <vnet/ip/ip.h>
19 #include <vnet/ethernet/ethernet.h>
20 #include <gtpu/gtpu.h>
22 /* Statistics (not all errors) */
23 #define foreach_gtpu_encap_error \
24 _(ENCAPSULATED, "good packets encapsulated")
26 static char * gtpu_encap_error_strings[] = {
27 #define _(sym,string) string,
28 foreach_gtpu_encap_error
33 #define _(sym,str) GTPU_ENCAP_ERROR_##sym,
34 foreach_gtpu_encap_error
49 u8 * format_gtpu_encap_trace (u8 * s, va_list * args)
51 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
52 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
53 gtpu_encap_trace_t * t
54 = va_arg (*args, gtpu_encap_trace_t *);
56 s = format (s, "GTPU encap to gtpu_tunnel%d teid %d",
57 t->tunnel_index, t->teid);
62 #define foreach_fixed_header4_offset \
65 #define foreach_fixed_header6_offset \
66 _(0) _(1) _(2) _(3) _(4) _(5) _(6)
69 gtpu_encap_inline (vlib_main_t * vm,
70 vlib_node_runtime_t * node,
71 vlib_frame_t * from_frame,
74 u32 n_left_from, next_index, * from, * to_next;
75 gtpu_main_t * gtm = >pu_main;
76 vnet_main_t * vnm = gtm->vnet_main;
77 vnet_interface_main_t * im = &vnm->interface_main;
78 u32 pkts_encapsulated = 0;
79 u16 old_l0 = 0, old_l1 = 0;
80 u32 thread_index = vlib_get_thread_index();
81 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
82 u32 sw_if_index0 = 0, sw_if_index1 = 0;
83 u32 next0 = 0, next1 = 0;
84 vnet_hw_interface_t * hi0, * hi1;
85 gtpu_tunnel_t * t0 = NULL, * t1 = NULL;
87 from = vlib_frame_vector_args (from_frame);
88 n_left_from = from_frame->n_vectors;
90 next_index = node->cached_next_index;
91 stats_sw_if_index = node->runtime_data[0];
92 stats_n_packets = stats_n_bytes = 0;
94 while (n_left_from > 0)
98 vlib_get_next_frame (vm, node, next_index,
99 to_next, n_left_to_next);
101 while (n_left_from >= 4 && n_left_to_next >= 2)
104 vlib_buffer_t * b0, * b1;
105 u32 flow_hash0, flow_hash1;
107 ip4_header_t * ip4_0, * ip4_1;
108 ip6_header_t * ip6_0, * ip6_1;
109 udp_header_t * udp0, * udp1;
110 gtpu_header_t * gtpu0, * gtpu1;
111 u64 * copy_src0, * copy_dst0;
112 u64 * copy_src1, * copy_dst1;
113 u32 * copy_src_last0, * copy_dst_last0;
114 u32 * copy_src_last1, * copy_dst_last1;
116 ip_csum_t sum0, sum1;
118 /* Prefetch next iteration. */
120 vlib_buffer_t * p2, * p3;
122 p2 = vlib_get_buffer (vm, from[2]);
123 p3 = vlib_get_buffer (vm, from[3]);
125 vlib_prefetch_buffer_header (p2, LOAD);
126 vlib_prefetch_buffer_header (p3, LOAD);
128 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
129 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
141 b0 = vlib_get_buffer (vm, bi0);
142 b1 = vlib_get_buffer (vm, bi1);
144 flow_hash0 = vnet_l2_compute_flow_hash (b0);
145 flow_hash1 = vnet_l2_compute_flow_hash (b1);
147 /* Get next node index and adj index from tunnel next_dpo */
148 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
149 hi0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
150 t0 = >m->tunnels[hi0->dev_instance];
151 /* Note: change to always set next0 if it may be set to drop */
152 next0 = t0->next_dpo.dpoi_next_node;
153 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = t0->next_dpo.dpoi_index;
155 /* Get next node index and adj index from tunnel next_dpo */
156 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_TX];
157 hi1 = vnet_get_sup_hw_interface (vnm, sw_if_index1);
158 t1 = >m->tunnels[hi1->dev_instance];
159 /* Note: change to always set next1 if it may be set to drop */
160 next1 = t1->next_dpo.dpoi_next_node;
161 vnet_buffer(b1)->ip.adj_index[VLIB_TX] = t1->next_dpo.dpoi_index;
163 /* Apply the rewrite string. $$$$ vnet_rewrite? */
164 vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
165 vlib_buffer_advance (b1, -(word)_vec_len(t1->rewrite));
169 ip4_0 = vlib_buffer_get_current(b0);
170 ip4_1 = vlib_buffer_get_current(b1);
172 /* Copy the fixed header */
173 copy_dst0 = (u64 *) ip4_0;
174 copy_src0 = (u64 *) t0->rewrite;
175 copy_dst1 = (u64 *) ip4_1;
176 copy_src1 = (u64 *) t1->rewrite;
177 /* Copy first 32 octets 8-bytes at a time */
178 #define _(offs) copy_dst0[offs] = copy_src0[offs];
179 foreach_fixed_header4_offset;
181 #define _(offs) copy_dst1[offs] = copy_src1[offs];
182 foreach_fixed_header4_offset;
184 /* Last 4 octets. Hopefully gcc will be our friend */
185 copy_dst_last0 = (u32 *)(©_dst0[4]);
186 copy_src_last0 = (u32 *)(©_src0[4]);
187 copy_dst_last0[0] = copy_src_last0[0];
188 copy_dst_last1 = (u32 *)(©_dst1[4]);
189 copy_src_last1 = (u32 *)(©_src1[4]);
190 copy_dst_last1[0] = copy_src_last1[0];
192 /* Fix the IP4 checksum and length */
193 sum0 = ip4_0->checksum;
194 new_l0 = /* old_l0 always 0, see the rewrite setup */
195 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
196 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
197 length /* changed member */);
198 ip4_0->checksum = ip_csum_fold (sum0);
199 ip4_0->length = new_l0;
200 sum1 = ip4_1->checksum;
201 new_l1 = /* old_l1 always 0, see the rewrite setup */
202 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
203 sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
204 length /* changed member */);
205 ip4_1->checksum = ip_csum_fold (sum1);
206 ip4_1->length = new_l1;
208 /* Fix UDP length and set source port */
209 udp0 = (udp_header_t *)(ip4_0+1);
210 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
212 udp0->length = new_l0;
213 udp0->src_port = flow_hash0;
214 udp1 = (udp_header_t *)(ip4_1+1);
215 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b1)
217 udp1->length = new_l1;
218 udp1->src_port = flow_hash1;
220 /* Fix GTPU length */
221 gtpu0 = (gtpu_header_t *)(udp0+1);
222 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
223 - sizeof (*ip4_0) - sizeof(*udp0));
224 gtpu0->length = new_l0;
225 gtpu1 = (gtpu_header_t *)(udp1+1);
226 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b1)
227 - sizeof (*ip4_1) - sizeof(*udp1));
228 gtpu1->length = new_l1;
234 ip6_0 = vlib_buffer_get_current(b0);
235 ip6_1 = vlib_buffer_get_current(b1);
237 /* Copy the fixed header */
238 copy_dst0 = (u64 *) ip6_0;
239 copy_src0 = (u64 *) t0->rewrite;
240 copy_dst1 = (u64 *) ip6_1;
241 copy_src1 = (u64 *) t1->rewrite;
242 /* Copy first 56 (ip6) octets 8-bytes at a time */
243 #define _(offs) copy_dst0[offs] = copy_src0[offs];
244 foreach_fixed_header6_offset;
246 #define _(offs) copy_dst1[offs] = copy_src1[offs];
247 foreach_fixed_header6_offset;
249 /* Fix IP6 payload length */
251 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
253 ip6_0->payload_length = new_l0;
255 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
257 ip6_1->payload_length = new_l1;
259 /* Fix UDP length and set source port */
260 udp0 = (udp_header_t *)(ip6_0+1);
261 udp0->length = new_l0;
262 udp0->src_port = flow_hash0;
263 udp1 = (udp_header_t *)(ip6_1+1);
264 udp1->length = new_l1;
265 udp1->src_port = flow_hash1;
267 /* IPv6 UDP checksum is mandatory */
268 udp0->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0,
270 if (udp0->checksum == 0)
271 udp0->checksum = 0xffff;
272 udp1->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b1,
274 if (udp1->checksum == 0)
275 udp1->checksum = 0xffff;
277 /* Fix GTPU length */
278 gtpu0 = (gtpu_header_t *)(udp0+1);
279 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
280 - sizeof (*ip4_0) - sizeof(*udp0));
281 gtpu0->length = new_l0;
282 gtpu1 = (gtpu_header_t *)(udp1+1);
283 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b1)
284 - sizeof (*ip4_1) - sizeof(*udp1));
285 gtpu1->length = new_l1;
288 pkts_encapsulated += 2;
289 len0 = vlib_buffer_length_in_chain (vm, b0);
290 len1 = vlib_buffer_length_in_chain (vm, b1);
291 stats_n_packets += 2;
292 stats_n_bytes += len0 + len1;
294 /* Batch stats increment on the same gtpu tunnel so counter is not
295 incremented per packet. Note stats are still incremented for deleted
296 and admin-down tunnel where packets are dropped. It is not worthwhile
297 to check for this rare case and affect normal path performance. */
298 if (PREDICT_FALSE ((sw_if_index0 != stats_sw_if_index) ||
299 (sw_if_index1 != stats_sw_if_index)))
301 stats_n_packets -= 2;
302 stats_n_bytes -= len0 + len1;
303 if (sw_if_index0 == sw_if_index1)
306 vlib_increment_combined_counter
307 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
308 thread_index, stats_sw_if_index,
309 stats_n_packets, stats_n_bytes);
310 stats_sw_if_index = sw_if_index0;
312 stats_n_bytes = len0 + len1;
316 vlib_increment_combined_counter
317 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
318 thread_index, sw_if_index0, 1, len0);
319 vlib_increment_combined_counter
320 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
321 thread_index, sw_if_index1, 1, len1);
325 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
327 gtpu_encap_trace_t *tr =
328 vlib_add_trace (vm, node, b0, sizeof (*tr));
329 tr->tunnel_index = t0 - gtm->tunnels;
333 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
335 gtpu_encap_trace_t *tr =
336 vlib_add_trace (vm, node, b1, sizeof (*tr));
337 tr->tunnel_index = t1 - gtm->tunnels;
341 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
342 to_next, n_left_to_next,
343 bi0, bi1, next0, next1);
346 while (n_left_from > 0 && n_left_to_next > 0)
352 ip4_header_t * ip4_0;
353 ip6_header_t * ip6_0;
355 gtpu_header_t * gtpu0;
356 u64 * copy_src0, * copy_dst0;
357 u32 * copy_src_last0, * copy_dst_last0;
368 b0 = vlib_get_buffer (vm, bi0);
370 flow_hash0 = vnet_l2_compute_flow_hash(b0);
372 /* Get next node index and adj index from tunnel next_dpo */
373 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
374 hi0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
375 t0 = >m->tunnels[hi0->dev_instance];
376 /* Note: change to always set next0 if it may be set to drop */
377 next0 = t0->next_dpo.dpoi_next_node;
378 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = t0->next_dpo.dpoi_index;
380 /* Apply the rewrite string. $$$$ vnet_rewrite? */
381 vlib_buffer_advance (b0, -(word)_vec_len(t0->rewrite));
385 ip4_0 = vlib_buffer_get_current(b0);
387 /* Copy the fixed header */
388 copy_dst0 = (u64 *) ip4_0;
389 copy_src0 = (u64 *) t0->rewrite;
390 /* Copy first 32 octets 8-bytes at a time */
391 #define _(offs) copy_dst0[offs] = copy_src0[offs];
392 foreach_fixed_header4_offset;
394 /* Last 4 octets. Hopefully gcc will be our friend */
395 copy_dst_last0 = (u32 *)(©_dst0[4]);
396 copy_src_last0 = (u32 *)(©_src0[4]);
397 copy_dst_last0[0] = copy_src_last0[0];
399 /* Fix the IP4 checksum and length */
400 sum0 = ip4_0->checksum;
401 new_l0 = /* old_l0 always 0, see the rewrite setup */
402 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
403 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
404 length /* changed member */);
405 ip4_0->checksum = ip_csum_fold (sum0);
406 ip4_0->length = new_l0;
408 /* Fix UDP length and set source port */
409 udp0 = (udp_header_t *)(ip4_0+1);
410 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
412 udp0->length = new_l0;
413 udp0->src_port = flow_hash0;
415 /* Fix GTPU length */
416 gtpu0 = (gtpu_header_t *)(udp0+1);
417 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
418 - sizeof (*ip4_0) - sizeof(*udp0));
419 gtpu0->length = new_l0;
426 ip6_0 = vlib_buffer_get_current(b0);
427 /* Copy the fixed header */
428 copy_dst0 = (u64 *) ip6_0;
429 copy_src0 = (u64 *) t0->rewrite;
430 /* Copy first 56 (ip6) octets 8-bytes at a time */
431 #define _(offs) copy_dst0[offs] = copy_src0[offs];
432 foreach_fixed_header6_offset;
434 /* Fix IP6 payload length */
436 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
438 ip6_0->payload_length = new_l0;
440 /* Fix UDP length and set source port */
441 udp0 = (udp_header_t *)(ip6_0+1);
442 udp0->length = new_l0;
443 udp0->src_port = flow_hash0;
445 /* IPv6 UDP checksum is mandatory */
446 udp0->checksum = ip6_tcp_udp_icmp_compute_checksum(vm, b0,
448 if (udp0->checksum == 0)
449 udp0->checksum = 0xffff;
451 /* Fix GTPU length */
452 gtpu0 = (gtpu_header_t *)(udp0+1);
453 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain(vm, b0)
454 - sizeof (*ip4_0) - sizeof(*udp0));
455 gtpu0->length = new_l0;
458 pkts_encapsulated ++;
459 len0 = vlib_buffer_length_in_chain (vm, b0);
460 stats_n_packets += 1;
461 stats_n_bytes += len0;
463 /* Batch stats increment on the same gtpu tunnel so counter is not
464 incremented per packet. Note stats are still incremented for deleted
465 and admin-down tunnel where packets are dropped. It is not worthwhile
466 to check for this rare case and affect normal path performance. */
467 if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
469 stats_n_packets -= 1;
470 stats_n_bytes -= len0;
472 vlib_increment_combined_counter
473 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
474 thread_index, stats_sw_if_index,
475 stats_n_packets, stats_n_bytes);
477 stats_n_bytes = len0;
478 stats_sw_if_index = sw_if_index0;
481 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
483 gtpu_encap_trace_t *tr =
484 vlib_add_trace (vm, node, b0, sizeof (*tr));
485 tr->tunnel_index = t0 - gtm->tunnels;
488 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
489 to_next, n_left_to_next,
493 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
496 /* Do we still need this now that tunnel tx stats is kept? */
497 vlib_node_increment_counter (vm, node->node_index,
498 GTPU_ENCAP_ERROR_ENCAPSULATED,
501 /* Increment any remaining batch stats */
504 vlib_increment_combined_counter
505 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX,
506 thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
507 node->runtime_data[0] = stats_sw_if_index;
510 return from_frame->n_vectors;
514 gtpu4_encap (vlib_main_t * vm,
515 vlib_node_runtime_t * node,
516 vlib_frame_t * from_frame)
518 return gtpu_encap_inline (vm, node, from_frame, /* is_ip4 */ 1);
522 gtpu6_encap (vlib_main_t * vm,
523 vlib_node_runtime_t * node,
524 vlib_frame_t * from_frame)
526 return gtpu_encap_inline (vm, node, from_frame, /* is_ip4 */ 0);
529 VLIB_REGISTER_NODE (gtpu4_encap_node) = {
530 .function = gtpu4_encap,
531 .name = "gtpu4-encap",
532 .vector_size = sizeof (u32),
533 .format_trace = format_gtpu_encap_trace,
534 .type = VLIB_NODE_TYPE_INTERNAL,
535 .n_errors = ARRAY_LEN(gtpu_encap_error_strings),
536 .error_strings = gtpu_encap_error_strings,
537 .n_next_nodes = GTPU_ENCAP_N_NEXT,
539 [GTPU_ENCAP_NEXT_DROP] = "error-drop",
543 VLIB_NODE_FUNCTION_MULTIARCH (gtpu4_encap_node, gtpu4_encap)
545 VLIB_REGISTER_NODE (gtpu6_encap_node) = {
546 .function = gtpu6_encap,
547 .name = "gtpu6-encap",
548 .vector_size = sizeof (u32),
549 .format_trace = format_gtpu_encap_trace,
550 .type = VLIB_NODE_TYPE_INTERNAL,
551 .n_errors = ARRAY_LEN(gtpu_encap_error_strings),
552 .error_strings = gtpu_encap_error_strings,
553 .n_next_nodes = GTPU_ENCAP_N_NEXT,
555 [GTPU_ENCAP_NEXT_DROP] = "error-drop",
559 VLIB_NODE_FUNCTION_MULTIARCH (gtpu6_encap_node, gtpu6_encap)