2 *------------------------------------------------------------------
3 * Copyright (c) 2017 Intel and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
19 #include <sys/ioctl.h>
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/fib/fib_entry.h>
26 #include <vnet/fib/fib_table.h>
27 #include <vnet/mfib/mfib_table.h>
28 #include <vnet/adj/adj_mcast.h>
29 #include <vnet/dpo/dpo.h>
30 #include <vnet/plugin/plugin.h>
31 #include <vpp/app/version.h>
32 #include <gtpu/gtpu.h>
35 gtpu_main_t gtpu_main;
38 VNET_FEATURE_INIT (ip4_gtpu_bypass, static) = {
39 .arc_name = "ip4-unicast",
40 .node_name = "ip4-gtpu-bypass",
41 .runs_before = VNET_FEATURES ("ip4-lookup"),
44 VNET_FEATURE_INIT (ip6_gtpu_bypass, static) = {
45 .arc_name = "ip6-unicast",
46 .node_name = "ip6-gtpu-bypass",
47 .runs_before = VNET_FEATURES ("ip6-lookup"),
52 format_decap_next (u8 * s, va_list * args)
54 u32 next_index = va_arg (*args, u32);
58 case GTPU_INPUT_NEXT_DROP:
59 return format (s, "drop");
60 case GTPU_INPUT_NEXT_L2_INPUT:
61 return format (s, "l2");
62 case GTPU_INPUT_NEXT_IP4_INPUT:
63 return format (s, "ip4");
64 case GTPU_INPUT_NEXT_IP6_INPUT:
65 return format (s, "ip6");
67 return format (s, "index %d", next_index);
73 format_gtpu_tunnel (u8 * s, va_list * args)
75 gtpu_tunnel_t *t = va_arg (*args, gtpu_tunnel_t *);
76 gtpu_main_t *ngm = >pu_main;
78 s = format (s, "[%d] src %U dst %U teid %d fib-idx %d sw-if-idx %d ",
80 format_ip46_address, &t->src, IP46_TYPE_ANY,
81 format_ip46_address, &t->dst, IP46_TYPE_ANY,
82 t->teid, t->encap_fib_index, t->sw_if_index);
84 s = format (s, "encap-dpo-idx %d ", t->next_dpo.dpoi_index);
85 s = format (s, "decap-next-%U ", format_decap_next, t->decap_next_index);
87 if (PREDICT_FALSE (ip46_address_is_multicast (&t->dst)))
88 s = format (s, "mcast-sw-if-idx %d ", t->mcast_sw_if_index);
94 format_gtpu_name (u8 * s, va_list * args)
96 u32 dev_instance = va_arg (*args, u32);
97 return format (s, "gtpu_tunnel%d", dev_instance);
100 static clib_error_t *
101 gtpu_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
103 u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
104 VNET_HW_INTERFACE_FLAG_LINK_UP : 0;
105 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
107 return /* no error */ 0;
111 VNET_DEVICE_CLASS (gtpu_device_class,static) = {
113 .format_device_name = format_gtpu_name,
114 .format_tx_trace = format_gtpu_encap_trace,
115 .admin_up_down_function = gtpu_interface_admin_up_down,
120 format_gtpu_header_with_length (u8 * s, va_list * args)
122 u32 dev_instance = va_arg (*args, u32);
123 s = format (s, "unimplemented dev %u", dev_instance);
128 VNET_HW_INTERFACE_CLASS (gtpu_hw_class) =
131 .format_header = format_gtpu_header_with_length,
132 .build_rewrite = default_build_rewrite,
133 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
138 gtpu_tunnel_restack_dpo (gtpu_tunnel_t * t)
140 dpo_id_t dpo = DPO_INVALID;
141 u32 encap_index = ip46_address_is_ip4 (&t->dst) ?
142 gtpu4_encap_node.index : gtpu6_encap_node.index;
143 fib_forward_chain_type_t forw_type = ip46_address_is_ip4 (&t->dst) ?
144 FIB_FORW_CHAIN_TYPE_UNICAST_IP4 : FIB_FORW_CHAIN_TYPE_UNICAST_IP6;
146 fib_entry_contribute_forwarding (t->fib_entry_index, forw_type, &dpo);
147 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
151 static gtpu_tunnel_t *
152 gtpu_tunnel_from_fib_node (fib_node_t * node)
154 return ((gtpu_tunnel_t *) (((char *) node) -
155 STRUCT_OFFSET_OF (gtpu_tunnel_t, node)));
159 * Function definition to backwalk a FIB node -
160 * Here we will restack the new dpo of GTPU DIP to encap node.
162 static fib_node_back_walk_rc_t
163 gtpu_tunnel_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
165 gtpu_tunnel_restack_dpo (gtpu_tunnel_from_fib_node (node));
166 return (FIB_NODE_BACK_WALK_CONTINUE);
170 * Function definition to get a FIB node from its index
173 gtpu_tunnel_fib_node_get (fib_node_index_t index)
176 gtpu_main_t *gtm = >pu_main;
178 t = pool_elt_at_index (gtm->tunnels, index);
184 * Function definition to inform the FIB node that its last lock has gone.
187 gtpu_tunnel_last_lock_gone (fib_node_t * node)
190 * The GTPU tunnel is a root of the graph. As such
191 * it never has children and thus is never locked.
197 * Virtual function table registered by GTPU tunnels
198 * for participation in the FIB object graph.
200 const static fib_node_vft_t gtpu_vft = {
201 .fnv_get = gtpu_tunnel_fib_node_get,
202 .fnv_last_lock = gtpu_tunnel_last_lock_gone,
203 .fnv_back_walk = gtpu_tunnel_back_walk,
207 #define foreach_copy_field \
209 _(mcast_sw_if_index) \
211 _(decap_next_index) \
216 ip_udp_gtpu_rewrite (gtpu_tunnel_t * t, bool is_ip6)
220 ip4_gtpu_header_t *h4;
221 ip6_gtpu_header_t *h6;
226 int len = is_ip6 ? sizeof *r.h6 : sizeof *r.h4;
228 vec_validate_aligned (r.rw, len - 1, CLIB_CACHE_LINE_BYTES);
232 /* Fixed portion of the (outer) ip header */
235 ip4_header_t *ip = &r.h4->ip4;
238 ip->ip_version_and_header_length = 0x45;
240 ip->protocol = IP_PROTOCOL_UDP;
242 ip->src_address = t->src.ip4;
243 ip->dst_address = t->dst.ip4;
245 /* we fix up the ip4 header length and checksum after-the-fact */
246 ip->checksum = ip4_header_checksum (ip);
250 ip6_header_t *ip = &r.h6->ip6;
253 ip->ip_version_traffic_class_and_flow_label =
254 clib_host_to_net_u32 (6 << 28);
256 ip->protocol = IP_PROTOCOL_UDP;
258 ip->src_address = t->src.ip6;
259 ip->dst_address = t->dst.ip6;
262 /* UDP header, randomize src port on something, maybe? */
263 udp->src_port = clib_host_to_net_u16 (2152);
264 udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_GTPU);
267 gtpu->ver_flags = GTPU_V1_VER | GTPU_PT_GTP;
268 gtpu->type = GTPU_TYPE_GTPU;
269 gtpu->teid = clib_host_to_net_u32 (t->teid);
272 /* Now only support 8-byte gtpu header. TBD */
273 _vec_len (t->rewrite) = sizeof (ip4_gtpu_header_t) - 4;
279 gtpu_decap_next_is_valid (gtpu_main_t * gtm, u32 is_ip6, u32 decap_next_index)
281 vlib_main_t *vm = gtm->vlib_main;
282 u32 input_idx = (!is_ip6) ? gtpu4_input_node.index : gtpu6_input_node.index;
283 vlib_node_runtime_t *r = vlib_node_get_runtime (vm, input_idx);
285 return decap_next_index < r->n_next_nodes;
289 vtep_addr_ref (ip46_address_t * ip)
291 uword *vtep = ip46_address_is_ip4 (ip) ?
292 hash_get (gtpu_main.vtep4, ip->ip4.as_u32) :
293 hash_get_mem (gtpu_main.vtep6, &ip->ip6);
296 ip46_address_is_ip4 (ip) ?
297 hash_set (gtpu_main.vtep4, ip->ip4.as_u32, 1) :
298 hash_set_mem_alloc (>pu_main.vtep6, &ip->ip6, 1);
303 vtep_addr_unref (ip46_address_t * ip)
305 uword *vtep = ip46_address_is_ip4 (ip) ?
306 hash_get (gtpu_main.vtep4, ip->ip4.as_u32) :
307 hash_get_mem (gtpu_main.vtep6, &ip->ip6);
311 ip46_address_is_ip4 (ip) ?
312 hash_unset (gtpu_main.vtep4, ip->ip4.as_u32) :
313 hash_unset_mem_free (>pu_main.vtep6, &ip->ip6);
317 typedef CLIB_PACKED (union
321 fib_node_index_t mfib_entry_index;
322 adj_index_t mcast_adj_index;
326 static inline mcast_shared_t
327 mcast_shared_get (ip46_address_t * ip)
329 ASSERT (ip46_address_is_multicast (ip));
330 uword *p = hash_get_mem (gtpu_main.mcast_shared, ip);
332 return (mcast_shared_t)
338 mcast_shared_add (ip46_address_t * dst, fib_node_index_t mfei, adj_index_t ai)
340 mcast_shared_t new_ep = {
341 .mcast_adj_index = ai,
342 .mfib_entry_index = mfei,
345 hash_set_mem_alloc (>pu_main.mcast_shared, dst, new_ep.as_u64);
349 mcast_shared_remove (ip46_address_t * dst)
351 mcast_shared_t ep = mcast_shared_get (dst);
353 adj_unlock (ep.mcast_adj_index);
354 mfib_table_entry_delete_index (ep.mfib_entry_index, MFIB_SOURCE_GTPU);
356 hash_unset_mem_free (>pu_main.mcast_shared, dst);
359 static inline fib_protocol_t
360 fib_ip_proto (bool is_ip6)
362 return (is_ip6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
365 int vnet_gtpu_add_del_tunnel
366 (vnet_gtpu_add_del_tunnel_args_t * a, u32 * sw_if_indexp)
368 gtpu_main_t *gtm = >pu_main;
369 gtpu_tunnel_t *t = 0;
370 vnet_main_t *vnm = gtm->vnet_main;
372 u32 hw_if_index = ~0;
373 u32 sw_if_index = ~0;
374 gtpu4_tunnel_key_t key4;
375 gtpu6_tunnel_key_t key6;
376 u32 is_ip6 = a->is_ip6;
380 key4.src = a->dst.ip4.as_u32; /* decap src in key is encap dst in config */
381 key4.teid = clib_host_to_net_u32 (a->teid);
382 p = hash_get (gtm->gtpu4_tunnel_by_key, key4.as_u64);
386 key6.src = a->dst.ip6;
387 key6.teid = clib_host_to_net_u32 (a->teid);
388 p = hash_get_mem (gtm->gtpu6_tunnel_by_key, &key6);
393 l2input_main_t *l2im = &l2input_main;
395 /* adding a tunnel: tunnel must not already exist */
397 return VNET_API_ERROR_TUNNEL_EXIST;
399 /*if not set explicitly, default to l2 */
400 if (a->decap_next_index == ~0)
401 a->decap_next_index = GTPU_INPUT_NEXT_L2_INPUT;
402 if (!gtpu_decap_next_is_valid (gtm, is_ip6, a->decap_next_index))
403 return VNET_API_ERROR_INVALID_DECAP_NEXT;
405 pool_get_aligned (gtm->tunnels, t, CLIB_CACHE_LINE_BYTES);
406 memset (t, 0, sizeof (*t));
408 /* copy from arg structure */
409 #define _(x) t->x = a->x;
413 ip_udp_gtpu_rewrite (t, is_ip6);
417 hash_set_mem_alloc (>m->gtpu6_tunnel_by_key, &key6,
420 hash_set (gtm->gtpu4_tunnel_by_key, key4.as_u64, t - gtm->tunnels);
422 vnet_hw_interface_t *hi;
423 if (vec_len (gtm->free_gtpu_tunnel_hw_if_indices) > 0)
425 vnet_interface_main_t *im = &vnm->interface_main;
426 hw_if_index = gtm->free_gtpu_tunnel_hw_if_indices
427 [vec_len (gtm->free_gtpu_tunnel_hw_if_indices) - 1];
428 _vec_len (gtm->free_gtpu_tunnel_hw_if_indices) -= 1;
430 hi = vnet_get_hw_interface (vnm, hw_if_index);
431 hi->dev_instance = t - gtm->tunnels;
432 hi->hw_instance = hi->dev_instance;
434 /* clear old stats of freed tunnel before reuse */
435 sw_if_index = hi->sw_if_index;
436 vnet_interface_counter_lock (im);
437 vlib_zero_combined_counter
438 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX],
440 vlib_zero_combined_counter (&im->combined_sw_if_counters
441 [VNET_INTERFACE_COUNTER_RX],
443 vlib_zero_simple_counter (&im->sw_if_counters
444 [VNET_INTERFACE_COUNTER_DROP],
446 vnet_interface_counter_unlock (im);
450 hw_if_index = vnet_register_interface
451 (vnm, gtpu_device_class.index, t - gtm->tunnels,
452 gtpu_hw_class.index, t - gtm->tunnels);
453 hi = vnet_get_hw_interface (vnm, hw_if_index);
456 /* Set gtpu tunnel output node */
457 u32 encap_index = !is_ip6 ?
458 gtpu4_encap_node.index : gtpu6_encap_node.index;
459 vnet_set_interface_output_node (vnm, hw_if_index, encap_index);
461 t->hw_if_index = hw_if_index;
462 t->sw_if_index = sw_if_index = hi->sw_if_index;
464 vec_validate_init_empty (gtm->tunnel_index_by_sw_if_index, sw_if_index,
466 gtm->tunnel_index_by_sw_if_index[sw_if_index] = t - gtm->tunnels;
468 /* setup l2 input config with l2 feature and bd 0 to drop packet */
469 vec_validate (l2im->configs, sw_if_index);
470 l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
471 l2im->configs[sw_if_index].bd_index = 0;
473 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
474 si->flags &= ~VNET_SW_INTERFACE_FLAG_HIDDEN;
475 vnet_sw_interface_set_flags (vnm, sw_if_index,
476 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
478 fib_node_init (&t->node, gtm->fib_node_type);
479 fib_prefix_t tun_dst_pfx;
480 vnet_flood_class_t flood_class = VNET_FLOOD_CLASS_TUNNEL_NORMAL;
482 fib_prefix_from_ip46_addr (&t->dst, &tun_dst_pfx);
483 if (!ip46_address_is_multicast (&t->dst))
486 * source the FIB entry for the tunnel's destination
487 * and become a child thereof. The tunnel will then get poked
488 * when the forwarding for the entry updates, and the tunnel can
489 * re-stack accordingly
491 vtep_addr_ref (&t->src);
492 t->fib_entry_index = fib_table_entry_special_add
493 (t->encap_fib_index, &tun_dst_pfx, FIB_SOURCE_RR,
494 FIB_ENTRY_FLAG_NONE);
495 t->sibling_index = fib_entry_child_add
496 (t->fib_entry_index, gtm->fib_node_type, t - gtm->tunnels);
497 gtpu_tunnel_restack_dpo (t);
501 /* Multicast tunnel -
502 * as the same mcast group can be used for mutiple mcast tunnels
503 * with different VNIs, create the output fib adjecency only if
504 * it does not already exist
506 fib_protocol_t fp = fib_ip_proto (is_ip6);
508 if (vtep_addr_ref (&t->dst) == 1)
510 fib_node_index_t mfei;
512 fib_route_path_t path = {
513 .frp_proto = fib_proto_to_dpo (fp),
514 .frp_addr = zero_addr,
515 .frp_sw_if_index = 0xffffffff,
518 .frp_flags = FIB_ROUTE_PATH_LOCAL,
520 const mfib_prefix_t mpfx = {
522 .fp_len = (is_ip6 ? 128 : 32),
523 .fp_grp_addr = tun_dst_pfx.fp_addr,
527 * Setup the (*,G) to receive traffic on the mcast group
528 * - the forwarding interface is for-us
529 * - the accepting interface is that from the API
531 mfib_table_entry_path_update (t->encap_fib_index,
534 &path, MFIB_ITF_FLAG_FORWARD);
536 path.frp_sw_if_index = a->mcast_sw_if_index;
537 path.frp_flags = FIB_ROUTE_PATH_FLAG_NONE;
538 mfei = mfib_table_entry_path_update (t->encap_fib_index,
542 MFIB_ITF_FLAG_ACCEPT);
545 * Create the mcast adjacency to send traffic to the group
547 ai = adj_mcast_add_or_lock (fp,
548 fib_proto_to_link (fp),
549 a->mcast_sw_if_index);
552 * create a new end-point
554 mcast_shared_add (&t->dst, mfei, ai);
557 dpo_id_t dpo = DPO_INVALID;
558 mcast_shared_t ep = mcast_shared_get (&t->dst);
560 /* Stack shared mcast dst mac addr rewrite on encap */
561 dpo_set (&dpo, DPO_ADJACENCY_MCAST,
562 fib_proto_to_dpo (fp), ep.mcast_adj_index);
564 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
567 flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER;
570 vnet_get_sw_interface (vnet_get_main (), sw_if_index)->flood_class =
575 /* deleting a tunnel: tunnel must exist */
577 return VNET_API_ERROR_NO_SUCH_ENTRY;
579 t = pool_elt_at_index (gtm->tunnels, p[0]);
580 sw_if_index = t->sw_if_index;
582 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */ );
583 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, t->sw_if_index);
584 si->flags |= VNET_SW_INTERFACE_FLAG_HIDDEN;
586 /* make sure tunnel is removed from l2 bd or xconnect */
587 set_int_l2_mode (gtm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0,
589 vec_add1 (gtm->free_gtpu_tunnel_hw_if_indices, t->hw_if_index);
591 gtm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
594 hash_unset (gtm->gtpu4_tunnel_by_key, key4.as_u64);
596 hash_unset_mem_free (>m->gtpu6_tunnel_by_key, &key6);
598 if (!ip46_address_is_multicast (&t->dst))
600 vtep_addr_unref (&t->src);
601 fib_entry_child_remove (t->fib_entry_index, t->sibling_index);
602 fib_table_entry_delete_index (t->fib_entry_index, FIB_SOURCE_RR);
604 else if (vtep_addr_unref (&t->dst) == 0)
606 mcast_shared_remove (&t->dst);
609 fib_node_deinit (&t->node);
610 vec_free (t->rewrite);
611 pool_put (gtm->tunnels, t);
615 *sw_if_indexp = sw_if_index;
621 get_decap_next_for_node (u32 node_index, u32 ipv4_set)
623 gtpu_main_t *gtm = >pu_main;
624 vlib_main_t *vm = gtm->vlib_main;
625 uword input_node = (ipv4_set) ? gtpu4_input_node.index :
626 gtpu6_input_node.index;
628 return vlib_node_add_next (vm, input_node, node_index);
632 unformat_decap_next (unformat_input_t * input, va_list * args)
634 u32 *result = va_arg (*args, u32 *);
635 u32 ipv4_set = va_arg (*args, int);
636 gtpu_main_t *gtm = >pu_main;
637 vlib_main_t *vm = gtm->vlib_main;
641 if (unformat (input, "l2"))
642 *result = GTPU_INPUT_NEXT_L2_INPUT;
643 else if (unformat (input, "ip4"))
644 *result = GTPU_INPUT_NEXT_IP4_INPUT;
645 else if (unformat (input, "ip6"))
646 *result = GTPU_INPUT_NEXT_IP6_INPUT;
647 else if (unformat (input, "node %U", unformat_vlib_node, vm, &node_index))
648 *result = get_decap_next_for_node (node_index, ipv4_set);
649 else if (unformat (input, "%d", &tmp))
657 static clib_error_t *
658 gtpu_add_del_tunnel_command_fn (vlib_main_t * vm,
659 unformat_input_t * input,
660 vlib_cli_command_t * cmd)
662 unformat_input_t _line_input, *line_input = &_line_input;
663 ip46_address_t src, dst;
670 u32 encap_fib_index = 0;
671 u32 mcast_sw_if_index = ~0;
672 u32 decap_next_index = GTPU_INPUT_NEXT_L2_INPUT;
676 vnet_gtpu_add_del_tunnel_args_t _a, *a = &_a;
677 u32 tunnel_sw_if_index;
678 clib_error_t *error = NULL;
680 /* Cant "universally zero init" (={0}) due to GCC bug 53119 */
681 memset (&src, 0, sizeof src);
682 memset (&dst, 0, sizeof dst);
684 /* Get a line of input. */
685 if (!unformat_user (input, unformat_line_input, line_input))
688 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
690 if (unformat (line_input, "del"))
694 else if (unformat (line_input, "src %U",
695 unformat_ip4_address, &src.ip4))
700 else if (unformat (line_input, "dst %U",
701 unformat_ip4_address, &dst.ip4))
706 else if (unformat (line_input, "src %U",
707 unformat_ip6_address, &src.ip6))
712 else if (unformat (line_input, "dst %U",
713 unformat_ip6_address, &dst.ip6))
718 else if (unformat (line_input, "group %U %U",
719 unformat_ip4_address, &dst.ip4,
720 unformat_vnet_sw_interface,
721 vnet_get_main (), &mcast_sw_if_index))
723 grp_set = dst_set = 1;
726 else if (unformat (line_input, "group %U %U",
727 unformat_ip6_address, &dst.ip6,
728 unformat_vnet_sw_interface,
729 vnet_get_main (), &mcast_sw_if_index))
731 grp_set = dst_set = 1;
734 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
736 encap_fib_index = fib_table_find (fib_ip_proto (ipv6_set), tmp);
737 if (encap_fib_index == ~0)
740 clib_error_return (0, "nonexistent encap-vrf-id %d", tmp);
744 else if (unformat (line_input, "decap-next %U", unformat_decap_next,
745 &decap_next_index, ipv4_set))
747 else if (unformat (line_input, "teid %d", &teid))
751 error = clib_error_return (0, "parse error: '%U'",
752 format_unformat_error, line_input);
759 error = clib_error_return (0, "tunnel src address not specified");
765 error = clib_error_return (0, "tunnel dst address not specified");
769 if (grp_set && !ip46_address_is_multicast (&dst))
771 error = clib_error_return (0, "tunnel group address not multicast");
775 if (grp_set == 0 && ip46_address_is_multicast (&dst))
777 error = clib_error_return (0, "dst address must be unicast");
781 if (grp_set && mcast_sw_if_index == ~0)
783 error = clib_error_return (0, "tunnel nonexistent multicast device");
787 if (ipv4_set && ipv6_set)
789 error = clib_error_return (0, "both IPv4 and IPv6 addresses specified");
793 if (ip46_address_cmp (&src, &dst) == 0)
795 error = clib_error_return (0, "src and dst addresses are identical");
799 if (decap_next_index == ~0)
801 error = clib_error_return (0, "next node not found");
805 memset (a, 0, sizeof (*a));
808 a->is_ip6 = ipv6_set;
810 #define _(x) a->x = x;
814 rv = vnet_gtpu_add_del_tunnel (a, &tunnel_sw_if_index);
820 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
821 vnet_get_main (), tunnel_sw_if_index);
824 case VNET_API_ERROR_TUNNEL_EXIST:
825 error = clib_error_return (0, "tunnel already exists...");
828 case VNET_API_ERROR_NO_SUCH_ENTRY:
829 error = clib_error_return (0, "tunnel does not exist...");
833 error = clib_error_return
834 (0, "vnet_gtpu_add_del_tunnel returned %d", rv);
839 unformat_free (line_input);
845 * Add or delete a GTPU Tunnel.
847 * GTPU provides the features needed to allow L2 bridge domains (BDs)
848 * to span multiple servers. This is done by building an L2 overlay on
849 * top of an L3 network underlay using GTPU tunnels.
851 * This makes it possible for servers to be co-located in the same data
852 * center or be separated geographically as long as they are reachable
853 * through the underlay L3 network.
855 * You can refer to this kind of L2 overlay bridge domain as a GTPU
856 * (Virtual eXtensible VLAN) segment.
859 * Example of how to create a GTPU Tunnel:
860 * @cliexcmd{create gtpu tunnel src 10.0.3.1 dst 10.0.3.3 teid 13 encap-vrf-id 7}
861 * Example of how to delete a GTPU Tunnel:
862 * @cliexcmd{create gtpu tunnel src 10.0.3.1 dst 10.0.3.3 teid 13 del}
865 VLIB_CLI_COMMAND (create_gtpu_tunnel_command, static) = {
866 .path = "create gtpu tunnel",
868 "create gtpu tunnel src <local-vtep-addr>"
869 " {dst <remote-vtep-addr>|group <mcast-vtep-addr> <intf-name>} teid <nn>"
870 " [encap-vrf-id <nn>] [decap-next [l2|ip4|ip6|node <name>]] [del]",
871 .function = gtpu_add_del_tunnel_command_fn,
875 static clib_error_t *
876 show_gtpu_tunnel_command_fn (vlib_main_t * vm,
877 unformat_input_t * input,
878 vlib_cli_command_t * cmd)
880 gtpu_main_t *gtm = >pu_main;
883 if (pool_elts (gtm->tunnels) == 0)
884 vlib_cli_output (vm, "No gtpu tunnels configured...");
886 pool_foreach (t, gtm->tunnels, (
888 vlib_cli_output (vm, "%U",
889 format_gtpu_tunnel, t);
897 * Display all the GTPU Tunnel entries.
900 * Example of how to display the GTPU Tunnel entries:
901 * @cliexstart{show gtpu tunnel}
902 * [0] src 10.0.3.1 dst 10.0.3.3 teid 13 encap_fib_index 0 sw_if_index 5 decap_next l2
906 VLIB_CLI_COMMAND (show_gtpu_tunnel_command, static) = {
907 .path = "show gtpu tunnel",
908 .short_help = "show gtpu tunnel",
909 .function = show_gtpu_tunnel_command_fn,
914 vnet_int_gtpu_bypass_mode (u32 sw_if_index, u8 is_ip6, u8 is_enable)
917 vnet_feature_enable_disable ("ip6-unicast", "ip6-gtpu-bypass",
918 sw_if_index, is_enable, 0, 0);
920 vnet_feature_enable_disable ("ip4-unicast", "ip4-gtpu-bypass",
921 sw_if_index, is_enable, 0, 0);
924 static clib_error_t *
925 set_ip_gtpu_bypass (u32 is_ip6,
926 unformat_input_t * input, vlib_cli_command_t * cmd)
928 unformat_input_t _line_input, *line_input = &_line_input;
929 vnet_main_t *vnm = vnet_get_main ();
930 clib_error_t *error = 0;
931 u32 sw_if_index, is_enable;
936 if (!unformat_user (input, unformat_line_input, line_input))
939 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
942 (line_input, unformat_vnet_sw_interface, vnm, &sw_if_index))
944 else if (unformat (line_input, "del"))
948 error = unformat_parse_error (line_input);
953 if (~0 == sw_if_index)
955 error = clib_error_return (0, "unknown interface `%U'",
956 format_unformat_error, line_input);
960 vnet_int_gtpu_bypass_mode (sw_if_index, is_ip6, is_enable);
963 unformat_free (line_input);
968 static clib_error_t *
969 set_ip4_gtpu_bypass (vlib_main_t * vm,
970 unformat_input_t * input, vlib_cli_command_t * cmd)
972 return set_ip_gtpu_bypass (0, input, cmd);
976 * This command adds the 'ip4-gtpu-bypass' graph node for a given interface.
977 * By adding the IPv4 gtpu-bypass graph node to an interface, the node checks
978 * for and validate input gtpu packet and bypass ip4-lookup, ip4-local,
979 * ip4-udp-lookup nodes to speedup gtpu packet forwarding. This node will
980 * cause extra overhead to for non-gtpu packets which is kept at a minimum.
984 * Example of graph node before ip4-gtpu-bypass is enabled:
985 * @cliexstart{show vlib graph ip4-gtpu-bypass}
987 * ip4-gtpu-bypass error-drop [0]
992 * Example of how to enable ip4-gtpu-bypass on an interface:
993 * @cliexcmd{set interface ip gtpu-bypass GigabitEthernet2/0/0}
995 * Example of graph node after ip4-gtpu-bypass is enabled:
996 * @cliexstart{show vlib graph ip4-gtpu-bypass}
998 * ip4-gtpu-bypass error-drop [0] ip4-input
999 * gtpu4-input [1] ip4-input-no-checksum
1003 * Example of how to display the feature enabed on an interface:
1004 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1005 * IP feature paths configured on GigabitEthernet2/0/0...
1013 * Example of how to disable ip4-gtpu-bypass on an interface:
1014 * @cliexcmd{set interface ip gtpu-bypass GigabitEthernet2/0/0 del}
1018 VLIB_CLI_COMMAND (set_interface_ip_gtpu_bypass_command, static) = {
1019 .path = "set interface ip gtpu-bypass",
1020 .function = set_ip4_gtpu_bypass,
1021 .short_help = "set interface ip gtpu-bypass <interface> [del]",
1025 static clib_error_t *
1026 set_ip6_gtpu_bypass (vlib_main_t * vm,
1027 unformat_input_t * input, vlib_cli_command_t * cmd)
1029 return set_ip_gtpu_bypass (1, input, cmd);
1033 * This command adds the 'ip6-gtpu-bypass' graph node for a given interface.
1034 * By adding the IPv6 gtpu-bypass graph node to an interface, the node checks
1035 * for and validate input gtpu packet and bypass ip6-lookup, ip6-local,
1036 * ip6-udp-lookup nodes to speedup gtpu packet forwarding. This node will
1037 * cause extra overhead to for non-gtpu packets which is kept at a minimum.
1041 * Example of graph node before ip6-gtpu-bypass is enabled:
1042 * @cliexstart{show vlib graph ip6-gtpu-bypass}
1043 * Name Next Previous
1044 * ip6-gtpu-bypass error-drop [0]
1049 * Example of how to enable ip6-gtpu-bypass on an interface:
1050 * @cliexcmd{set interface ip6 gtpu-bypass GigabitEthernet2/0/0}
1052 * Example of graph node after ip6-gtpu-bypass is enabled:
1053 * @cliexstart{show vlib graph ip6-gtpu-bypass}
1054 * Name Next Previous
1055 * ip6-gtpu-bypass error-drop [0] ip6-input
1056 * gtpu6-input [1] ip4-input-no-checksum
1060 * Example of how to display the feature enabed on an interface:
1061 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1062 * IP feature paths configured on GigabitEthernet2/0/0...
1070 * Example of how to disable ip6-gtpu-bypass on an interface:
1071 * @cliexcmd{set interface ip6 gtpu-bypass GigabitEthernet2/0/0 del}
1075 VLIB_CLI_COMMAND (set_interface_ip6_gtpu_bypass_command, static) = {
1076 .path = "set interface ip6 gtpu-bypass",
1077 .function = set_ip6_gtpu_bypass,
1078 .short_help = "set interface ip gtpu-bypass <interface> [del]",
1083 gtpu_init (vlib_main_t * vm)
1085 gtpu_main_t *gtm = >pu_main;
1087 gtm->vnet_main = vnet_get_main ();
1088 gtm->vlib_main = vm;
1090 /* initialize the ip6 hash */
1091 gtm->gtpu6_tunnel_by_key = hash_create_mem (0,
1092 sizeof (gtpu6_tunnel_key_t),
1094 gtm->vtep6 = hash_create_mem (0, sizeof (ip6_address_t), sizeof (uword));
1095 gtm->mcast_shared = hash_create_mem (0,
1096 sizeof (ip46_address_t),
1097 sizeof (mcast_shared_t));
1099 udp_register_dst_port (vm, UDP_DST_PORT_GTPU,
1100 gtpu4_input_node.index, /* is_ip4 */ 1);
1101 udp_register_dst_port (vm, UDP_DST_PORT_GTPU6,
1102 gtpu6_input_node.index, /* is_ip4 */ 0);
1104 gtm->fib_node_type = fib_node_register_new_type (>pu_vft);
1109 VLIB_INIT_FUNCTION (gtpu_init);
1112 VLIB_PLUGIN_REGISTER () = {
1113 .version = VPP_BUILD_VER,
1114 .description = "GTPv1-U",
1119 * fd.io coding-style-patch-verification: ON
1122 * eval: (c-set-style "gnu")