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/fib/fib_entry_track.h>
28 #include <vnet/mfib/mfib_table.h>
29 #include <vnet/adj/adj_mcast.h>
30 #include <vnet/dpo/dpo.h>
31 #include <vnet/plugin/plugin.h>
32 #include <vpp/app/version.h>
33 #include <gtpu/gtpu.h>
36 gtpu_main_t gtpu_main;
39 VNET_FEATURE_INIT (ip4_gtpu_bypass, static) = {
40 .arc_name = "ip4-unicast",
41 .node_name = "ip4-gtpu-bypass",
42 .runs_before = VNET_FEATURES ("ip4-lookup"),
45 VNET_FEATURE_INIT (ip6_gtpu_bypass, static) = {
46 .arc_name = "ip6-unicast",
47 .node_name = "ip6-gtpu-bypass",
48 .runs_before = VNET_FEATURES ("ip6-lookup"),
52 u8 * format_gtpu_encap_trace (u8 * s, va_list * args)
54 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
55 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
56 gtpu_encap_trace_t * t
57 = va_arg (*args, gtpu_encap_trace_t *);
59 s = format (s, "GTPU encap to gtpu_tunnel%d teid %d",
60 t->tunnel_index, t->teid);
65 format_decap_next (u8 * s, va_list * args)
67 u32 next_index = va_arg (*args, u32);
71 case GTPU_INPUT_NEXT_DROP:
72 return format (s, "drop");
73 case GTPU_INPUT_NEXT_L2_INPUT:
74 return format (s, "l2");
75 case GTPU_INPUT_NEXT_IP4_INPUT:
76 return format (s, "ip4");
77 case GTPU_INPUT_NEXT_IP6_INPUT:
78 return format (s, "ip6");
80 return format (s, "index %d", next_index);
86 format_gtpu_tunnel (u8 * s, va_list * args)
88 gtpu_tunnel_t *t = va_arg (*args, gtpu_tunnel_t *);
89 gtpu_main_t *ngm = >pu_main;
91 s = format (s, "[%d] src %U dst %U teid %d fib-idx %d sw-if-idx %d ",
93 format_ip46_address, &t->src, IP46_TYPE_ANY,
94 format_ip46_address, &t->dst, IP46_TYPE_ANY,
95 t->teid, t->encap_fib_index, t->sw_if_index);
97 s = format (s, "encap-dpo-idx %d ", t->next_dpo.dpoi_index);
98 s = format (s, "decap-next-%U ", format_decap_next, t->decap_next_index);
100 if (PREDICT_FALSE (ip46_address_is_multicast (&t->dst)))
101 s = format (s, "mcast-sw-if-idx %d ", t->mcast_sw_if_index);
107 format_gtpu_name (u8 * s, va_list * args)
109 u32 dev_instance = va_arg (*args, u32);
110 return format (s, "gtpu_tunnel%d", dev_instance);
113 static clib_error_t *
114 gtpu_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
116 u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
117 VNET_HW_INTERFACE_FLAG_LINK_UP : 0;
118 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
120 return /* no error */ 0;
124 VNET_DEVICE_CLASS (gtpu_device_class,static) = {
126 .format_device_name = format_gtpu_name,
127 .format_tx_trace = format_gtpu_encap_trace,
128 .admin_up_down_function = gtpu_interface_admin_up_down,
133 format_gtpu_header_with_length (u8 * s, va_list * args)
135 u32 dev_instance = va_arg (*args, u32);
136 s = format (s, "unimplemented dev %u", dev_instance);
141 VNET_HW_INTERFACE_CLASS (gtpu_hw_class) =
144 .format_header = format_gtpu_header_with_length,
145 .build_rewrite = default_build_rewrite,
146 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
151 gtpu_tunnel_restack_dpo (gtpu_tunnel_t * t)
153 dpo_id_t dpo = DPO_INVALID;
154 u32 encap_index = ip46_address_is_ip4 (&t->dst) ?
155 gtpu4_encap_node.index : gtpu6_encap_node.index;
156 fib_forward_chain_type_t forw_type = ip46_address_is_ip4 (&t->dst) ?
157 FIB_FORW_CHAIN_TYPE_UNICAST_IP4 : FIB_FORW_CHAIN_TYPE_UNICAST_IP6;
159 fib_entry_contribute_forwarding (t->fib_entry_index, forw_type, &dpo);
160 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
164 static gtpu_tunnel_t *
165 gtpu_tunnel_from_fib_node (fib_node_t * node)
167 return ((gtpu_tunnel_t *) (((char *) node) -
168 STRUCT_OFFSET_OF (gtpu_tunnel_t, node)));
172 * Function definition to backwalk a FIB node -
173 * Here we will restack the new dpo of GTPU DIP to encap node.
175 static fib_node_back_walk_rc_t
176 gtpu_tunnel_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
178 gtpu_tunnel_restack_dpo (gtpu_tunnel_from_fib_node (node));
179 return (FIB_NODE_BACK_WALK_CONTINUE);
183 * Function definition to get a FIB node from its index
186 gtpu_tunnel_fib_node_get (fib_node_index_t index)
189 gtpu_main_t *gtm = >pu_main;
191 t = pool_elt_at_index (gtm->tunnels, index);
197 * Function definition to inform the FIB node that its last lock has gone.
200 gtpu_tunnel_last_lock_gone (fib_node_t * node)
203 * The GTPU tunnel is a root of the graph. As such
204 * it never has children and thus is never locked.
210 * Virtual function table registered by GTPU tunnels
211 * for participation in the FIB object graph.
213 const static fib_node_vft_t gtpu_vft = {
214 .fnv_get = gtpu_tunnel_fib_node_get,
215 .fnv_last_lock = gtpu_tunnel_last_lock_gone,
216 .fnv_back_walk = gtpu_tunnel_back_walk,
220 #define foreach_copy_field \
222 _(mcast_sw_if_index) \
224 _(decap_next_index) \
229 ip_udp_gtpu_rewrite (gtpu_tunnel_t * t, bool is_ip6)
233 ip4_gtpu_header_t *h4;
234 ip6_gtpu_header_t *h6;
239 int len = is_ip6 ? sizeof *r.h6 : sizeof *r.h4;
241 vec_validate_aligned (r.rw, len - 1, CLIB_CACHE_LINE_BYTES);
245 /* Fixed portion of the (outer) ip header */
248 ip4_header_t *ip = &r.h4->ip4;
251 ip->ip_version_and_header_length = 0x45;
253 ip->protocol = IP_PROTOCOL_UDP;
255 ip->src_address = t->src.ip4;
256 ip->dst_address = t->dst.ip4;
258 /* we fix up the ip4 header length and checksum after-the-fact */
259 ip->checksum = ip4_header_checksum (ip);
263 ip6_header_t *ip = &r.h6->ip6;
266 ip->ip_version_traffic_class_and_flow_label =
267 clib_host_to_net_u32 (6 << 28);
269 ip->protocol = IP_PROTOCOL_UDP;
271 ip->src_address = t->src.ip6;
272 ip->dst_address = t->dst.ip6;
275 /* UDP header, randomize src port on something, maybe? */
276 udp->src_port = clib_host_to_net_u16 (2152);
277 udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_GTPU);
280 gtpu->ver_flags = GTPU_V1_VER | GTPU_PT_GTP;
281 gtpu->type = GTPU_TYPE_GTPU;
282 gtpu->teid = clib_host_to_net_u32 (t->teid);
285 /* Now only support 8-byte gtpu header. TBD */
286 _vec_len (t->rewrite) = sizeof (ip4_gtpu_header_t) - 4;
292 gtpu_decap_next_is_valid (gtpu_main_t * gtm, u32 is_ip6, u32 decap_next_index)
294 vlib_main_t *vm = gtm->vlib_main;
295 u32 input_idx = (!is_ip6) ? gtpu4_input_node.index : gtpu6_input_node.index;
296 vlib_node_runtime_t *r = vlib_node_get_runtime (vm, input_idx);
298 return decap_next_index < r->n_next_nodes;
302 vtep_addr_ref (ip46_address_t * ip)
304 uword *vtep = ip46_address_is_ip4 (ip) ?
305 hash_get (gtpu_main.vtep4, ip->ip4.as_u32) :
306 hash_get_mem (gtpu_main.vtep6, &ip->ip6);
309 ip46_address_is_ip4 (ip) ?
310 hash_set (gtpu_main.vtep4, ip->ip4.as_u32, 1) :
311 hash_set_mem_alloc (>pu_main.vtep6, &ip->ip6, 1);
316 vtep_addr_unref (ip46_address_t * ip)
318 uword *vtep = ip46_address_is_ip4 (ip) ?
319 hash_get (gtpu_main.vtep4, ip->ip4.as_u32) :
320 hash_get_mem (gtpu_main.vtep6, &ip->ip6);
324 ip46_address_is_ip4 (ip) ?
325 hash_unset (gtpu_main.vtep4, ip->ip4.as_u32) :
326 hash_unset_mem_free (>pu_main.vtep6, &ip->ip6);
330 typedef CLIB_PACKED (union
334 fib_node_index_t mfib_entry_index;
335 adj_index_t mcast_adj_index;
339 static inline mcast_shared_t
340 mcast_shared_get (ip46_address_t * ip)
342 ASSERT (ip46_address_is_multicast (ip));
343 uword *p = hash_get_mem (gtpu_main.mcast_shared, ip);
345 return (mcast_shared_t)
351 mcast_shared_add (ip46_address_t * dst, fib_node_index_t mfei, adj_index_t ai)
353 mcast_shared_t new_ep = {
354 .mcast_adj_index = ai,
355 .mfib_entry_index = mfei,
358 hash_set_mem_alloc (>pu_main.mcast_shared, dst, new_ep.as_u64);
362 mcast_shared_remove (ip46_address_t * dst)
364 mcast_shared_t ep = mcast_shared_get (dst);
366 adj_unlock (ep.mcast_adj_index);
367 mfib_table_entry_delete_index (ep.mfib_entry_index, MFIB_SOURCE_GTPU);
369 hash_unset_mem_free (>pu_main.mcast_shared, dst);
372 int vnet_gtpu_add_del_tunnel
373 (vnet_gtpu_add_del_tunnel_args_t * a, u32 * sw_if_indexp)
375 gtpu_main_t *gtm = >pu_main;
376 gtpu_tunnel_t *t = 0;
377 vnet_main_t *vnm = gtm->vnet_main;
379 u32 hw_if_index = ~0;
380 u32 sw_if_index = ~0;
381 gtpu4_tunnel_key_t key4;
382 gtpu6_tunnel_key_t key6;
383 bool is_ip6 = !ip46_address_is_ip4 (&a->dst);
387 key4.src = a->dst.ip4.as_u32; /* decap src in key is encap dst in config */
388 key4.teid = clib_host_to_net_u32 (a->teid);
389 p = hash_get (gtm->gtpu4_tunnel_by_key, key4.as_u64);
393 key6.src = a->dst.ip6;
394 key6.teid = clib_host_to_net_u32 (a->teid);
395 p = hash_get_mem (gtm->gtpu6_tunnel_by_key, &key6);
400 l2input_main_t *l2im = &l2input_main;
402 /* adding a tunnel: tunnel must not already exist */
404 return VNET_API_ERROR_TUNNEL_EXIST;
406 /*if not set explicitly, default to l2 */
407 if (a->decap_next_index == ~0)
408 a->decap_next_index = GTPU_INPUT_NEXT_L2_INPUT;
409 if (!gtpu_decap_next_is_valid (gtm, is_ip6, a->decap_next_index))
410 return VNET_API_ERROR_INVALID_DECAP_NEXT;
412 pool_get_aligned (gtm->tunnels, t, CLIB_CACHE_LINE_BYTES);
413 clib_memset (t, 0, sizeof (*t));
415 /* copy from arg structure */
416 #define _(x) t->x = a->x;
420 ip_udp_gtpu_rewrite (t, is_ip6);
424 hash_set_mem_alloc (>m->gtpu6_tunnel_by_key, &key6,
427 hash_set (gtm->gtpu4_tunnel_by_key, key4.as_u64, t - gtm->tunnels);
429 vnet_hw_interface_t *hi;
430 if (vec_len (gtm->free_gtpu_tunnel_hw_if_indices) > 0)
432 vnet_interface_main_t *im = &vnm->interface_main;
433 hw_if_index = gtm->free_gtpu_tunnel_hw_if_indices
434 [vec_len (gtm->free_gtpu_tunnel_hw_if_indices) - 1];
435 _vec_len (gtm->free_gtpu_tunnel_hw_if_indices) -= 1;
437 hi = vnet_get_hw_interface (vnm, hw_if_index);
438 hi->dev_instance = t - gtm->tunnels;
439 hi->hw_instance = hi->dev_instance;
441 /* clear old stats of freed tunnel before reuse */
442 sw_if_index = hi->sw_if_index;
443 vnet_interface_counter_lock (im);
444 vlib_zero_combined_counter
445 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX],
447 vlib_zero_combined_counter (&im->combined_sw_if_counters
448 [VNET_INTERFACE_COUNTER_RX],
450 vlib_zero_simple_counter (&im->sw_if_counters
451 [VNET_INTERFACE_COUNTER_DROP],
453 vnet_interface_counter_unlock (im);
457 hw_if_index = vnet_register_interface
458 (vnm, gtpu_device_class.index, t - gtm->tunnels,
459 gtpu_hw_class.index, t - gtm->tunnels);
460 hi = vnet_get_hw_interface (vnm, hw_if_index);
463 /* Set gtpu tunnel output node */
464 u32 encap_index = !is_ip6 ?
465 gtpu4_encap_node.index : gtpu6_encap_node.index;
466 vnet_set_interface_output_node (vnm, hw_if_index, encap_index);
468 t->hw_if_index = hw_if_index;
469 t->sw_if_index = sw_if_index = hi->sw_if_index;
471 vec_validate_init_empty (gtm->tunnel_index_by_sw_if_index, sw_if_index,
473 gtm->tunnel_index_by_sw_if_index[sw_if_index] = t - gtm->tunnels;
475 /* setup l2 input config with l2 feature and bd 0 to drop packet */
476 vec_validate (l2im->configs, sw_if_index);
477 l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
478 l2im->configs[sw_if_index].bd_index = 0;
480 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
481 si->flags &= ~VNET_SW_INTERFACE_FLAG_HIDDEN;
482 vnet_sw_interface_set_flags (vnm, sw_if_index,
483 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
485 fib_node_init (&t->node, gtm->fib_node_type);
486 fib_prefix_t tun_dst_pfx;
487 vnet_flood_class_t flood_class = VNET_FLOOD_CLASS_TUNNEL_NORMAL;
489 fib_prefix_from_ip46_addr (&t->dst, &tun_dst_pfx);
490 if (!ip46_address_is_multicast (&t->dst))
493 * Track the FIB entry for the tunnel's destination.
494 * The tunnel will then get poked
495 * when the forwarding for the entry updates, and the tunnel can
496 * re-stack accordingly
498 vtep_addr_ref (&t->src);
499 t->fib_entry_index = fib_entry_track (t->encap_fib_index,
504 gtpu_tunnel_restack_dpo (t);
508 /* Multicast tunnel -
509 * as the same mcast group can be used for multiple mcast tunnels
510 * with different VNIs, create the output adjacency only if
511 * it does not already exist
513 fib_protocol_t fp = fib_ip_proto (is_ip6);
515 if (vtep_addr_ref (&t->dst) == 1)
517 fib_node_index_t mfei;
519 fib_route_path_t path = {
520 .frp_proto = fib_proto_to_dpo (fp),
521 .frp_addr = zero_addr,
522 .frp_sw_if_index = 0xffffffff,
525 .frp_flags = FIB_ROUTE_PATH_LOCAL,
526 .frp_mitf_flags = MFIB_ITF_FLAG_FORWARD,
528 const mfib_prefix_t mpfx = {
530 .fp_len = (is_ip6 ? 128 : 32),
531 .fp_grp_addr = tun_dst_pfx.fp_addr,
535 * Setup the (*,G) to receive traffic on the mcast group
536 * - the forwarding interface is for-us
537 * - the accepting interface is that from the API
539 mfib_table_entry_path_update (t->encap_fib_index,
540 &mpfx, MFIB_SOURCE_GTPU, &path);
542 path.frp_sw_if_index = a->mcast_sw_if_index;
543 path.frp_flags = FIB_ROUTE_PATH_FLAG_NONE;
544 path.frp_mitf_flags = MFIB_ITF_FLAG_ACCEPT;
545 mfei = mfib_table_entry_path_update (t->encap_fib_index,
547 MFIB_SOURCE_GTPU, &path);
550 * Create the mcast adjacency to send traffic to the group
552 ai = adj_mcast_add_or_lock (fp,
553 fib_proto_to_link (fp),
554 a->mcast_sw_if_index);
557 * create a new end-point
559 mcast_shared_add (&t->dst, mfei, ai);
562 dpo_id_t dpo = DPO_INVALID;
563 mcast_shared_t ep = mcast_shared_get (&t->dst);
565 /* Stack shared mcast dst mac addr rewrite on encap */
566 dpo_set (&dpo, DPO_ADJACENCY_MCAST,
567 fib_proto_to_dpo (fp), ep.mcast_adj_index);
569 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
572 flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER;
575 vnet_get_sw_interface (vnet_get_main (), sw_if_index)->flood_class =
580 /* deleting a tunnel: tunnel must exist */
582 return VNET_API_ERROR_NO_SUCH_ENTRY;
584 t = pool_elt_at_index (gtm->tunnels, p[0]);
585 sw_if_index = t->sw_if_index;
587 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */ );
588 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, t->sw_if_index);
589 si->flags |= VNET_SW_INTERFACE_FLAG_HIDDEN;
591 /* make sure tunnel is removed from l2 bd or xconnect */
592 set_int_l2_mode (gtm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0,
593 L2_BD_PORT_TYPE_NORMAL, 0, 0);
594 vec_add1 (gtm->free_gtpu_tunnel_hw_if_indices, t->hw_if_index);
596 gtm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
599 hash_unset (gtm->gtpu4_tunnel_by_key, key4.as_u64);
601 hash_unset_mem_free (>m->gtpu6_tunnel_by_key, &key6);
603 if (!ip46_address_is_multicast (&t->dst))
605 vtep_addr_unref (&t->src);
606 fib_entry_untrack (t->fib_entry_index, t->sibling_index);
608 else if (vtep_addr_unref (&t->dst) == 0)
610 mcast_shared_remove (&t->dst);
613 fib_node_deinit (&t->node);
614 vec_free (t->rewrite);
615 pool_put (gtm->tunnels, t);
619 *sw_if_indexp = sw_if_index;
623 /* register udp ports */
624 if (!is_ip6 && !udp_is_valid_dst_port (UDP_DST_PORT_GTPU, 1))
625 udp_register_dst_port (gtm->vlib_main, UDP_DST_PORT_GTPU,
626 gtpu4_input_node.index, /* is_ip4 */ 1);
627 if (is_ip6 && !udp_is_valid_dst_port (UDP_DST_PORT_GTPU6, 0))
628 udp_register_dst_port (gtm->vlib_main, UDP_DST_PORT_GTPU6,
629 gtpu6_input_node.index, /* is_ip4 */ 0);
636 get_decap_next_for_node (u32 node_index, u32 ipv4_set)
638 gtpu_main_t *gtm = >pu_main;
639 vlib_main_t *vm = gtm->vlib_main;
640 uword input_node = (ipv4_set) ? gtpu4_input_node.index :
641 gtpu6_input_node.index;
643 return vlib_node_add_next (vm, input_node, node_index);
647 unformat_decap_next (unformat_input_t * input, va_list * args)
649 u32 *result = va_arg (*args, u32 *);
650 u32 ipv4_set = va_arg (*args, int);
651 gtpu_main_t *gtm = >pu_main;
652 vlib_main_t *vm = gtm->vlib_main;
656 if (unformat (input, "l2"))
657 *result = GTPU_INPUT_NEXT_L2_INPUT;
658 else if (unformat (input, "ip4"))
659 *result = GTPU_INPUT_NEXT_IP4_INPUT;
660 else if (unformat (input, "ip6"))
661 *result = GTPU_INPUT_NEXT_IP6_INPUT;
662 else if (unformat (input, "node %U", unformat_vlib_node, vm, &node_index))
663 *result = get_decap_next_for_node (node_index, ipv4_set);
664 else if (unformat (input, "%d", &tmp))
672 static clib_error_t *
673 gtpu_add_del_tunnel_command_fn (vlib_main_t * vm,
674 unformat_input_t * input,
675 vlib_cli_command_t * cmd)
677 unformat_input_t _line_input, *line_input = &_line_input;
678 ip46_address_t src, dst;
685 u32 encap_fib_index = 0;
686 u32 mcast_sw_if_index = ~0;
687 u32 decap_next_index = GTPU_INPUT_NEXT_L2_INPUT;
691 vnet_gtpu_add_del_tunnel_args_t _a, *a = &_a;
692 u32 tunnel_sw_if_index;
693 clib_error_t *error = NULL;
695 /* Cant "universally zero init" (={0}) due to GCC bug 53119 */
696 clib_memset (&src, 0, sizeof src);
697 clib_memset (&dst, 0, sizeof dst);
699 /* Get a line of input. */
700 if (!unformat_user (input, unformat_line_input, line_input))
703 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
705 if (unformat (line_input, "del"))
709 else if (unformat (line_input, "src %U",
710 unformat_ip4_address, &src.ip4))
715 else if (unformat (line_input, "dst %U",
716 unformat_ip4_address, &dst.ip4))
721 else if (unformat (line_input, "src %U",
722 unformat_ip6_address, &src.ip6))
727 else if (unformat (line_input, "dst %U",
728 unformat_ip6_address, &dst.ip6))
733 else if (unformat (line_input, "group %U %U",
734 unformat_ip4_address, &dst.ip4,
735 unformat_vnet_sw_interface,
736 vnet_get_main (), &mcast_sw_if_index))
738 grp_set = dst_set = 1;
741 else if (unformat (line_input, "group %U %U",
742 unformat_ip6_address, &dst.ip6,
743 unformat_vnet_sw_interface,
744 vnet_get_main (), &mcast_sw_if_index))
746 grp_set = dst_set = 1;
749 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
751 encap_fib_index = fib_table_find (fib_ip_proto (ipv6_set), tmp);
752 if (encap_fib_index == ~0)
755 clib_error_return (0, "nonexistent encap-vrf-id %d", tmp);
759 else if (unformat (line_input, "decap-next %U", unformat_decap_next,
760 &decap_next_index, ipv4_set))
762 else if (unformat (line_input, "teid %d", &teid))
766 error = clib_error_return (0, "parse error: '%U'",
767 format_unformat_error, line_input);
774 error = clib_error_return (0, "tunnel src address not specified");
780 error = clib_error_return (0, "tunnel dst address not specified");
784 if (grp_set && !ip46_address_is_multicast (&dst))
786 error = clib_error_return (0, "tunnel group address not multicast");
790 if (grp_set == 0 && ip46_address_is_multicast (&dst))
792 error = clib_error_return (0, "dst address must be unicast");
796 if (grp_set && mcast_sw_if_index == ~0)
798 error = clib_error_return (0, "tunnel nonexistent multicast device");
802 if (ipv4_set && ipv6_set)
804 error = clib_error_return (0, "both IPv4 and IPv6 addresses specified");
808 if (ip46_address_cmp (&src, &dst) == 0)
810 error = clib_error_return (0, "src and dst addresses are identical");
814 if (decap_next_index == ~0)
816 error = clib_error_return (0, "next node not found");
820 clib_memset (a, 0, sizeof (*a));
824 #define _(x) a->x = x;
828 rv = vnet_gtpu_add_del_tunnel (a, &tunnel_sw_if_index);
834 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
835 vnet_get_main (), tunnel_sw_if_index);
838 case VNET_API_ERROR_TUNNEL_EXIST:
839 error = clib_error_return (0, "tunnel already exists...");
842 case VNET_API_ERROR_NO_SUCH_ENTRY:
843 error = clib_error_return (0, "tunnel does not exist...");
847 error = clib_error_return
848 (0, "vnet_gtpu_add_del_tunnel returned %d", rv);
853 unformat_free (line_input);
859 * Add or delete a GTPU Tunnel.
861 * GTPU provides the features needed to allow L2 bridge domains (BDs)
862 * to span multiple servers. This is done by building an L2 overlay on
863 * top of an L3 network underlay using GTPU tunnels.
865 * This makes it possible for servers to be co-located in the same data
866 * center or be separated geographically as long as they are reachable
867 * through the underlay L3 network.
869 * You can refer to this kind of L2 overlay bridge domain as a GTPU
870 * (Virtual eXtensible VLAN) segment.
873 * Example of how to create a GTPU Tunnel:
874 * @cliexcmd{create gtpu tunnel src 10.0.3.1 dst 10.0.3.3 teid 13 encap-vrf-id 7}
875 * Example of how to delete a GTPU Tunnel:
876 * @cliexcmd{create gtpu tunnel src 10.0.3.1 dst 10.0.3.3 teid 13 del}
879 VLIB_CLI_COMMAND (create_gtpu_tunnel_command, static) = {
880 .path = "create gtpu tunnel",
882 "create gtpu tunnel src <local-vtep-addr>"
883 " {dst <remote-vtep-addr>|group <mcast-vtep-addr> <intf-name>} teid <nn>"
884 " [encap-vrf-id <nn>] [decap-next [l2|ip4|ip6|node <name>]] [del]",
885 .function = gtpu_add_del_tunnel_command_fn,
889 static clib_error_t *
890 show_gtpu_tunnel_command_fn (vlib_main_t * vm,
891 unformat_input_t * input,
892 vlib_cli_command_t * cmd)
894 gtpu_main_t *gtm = >pu_main;
897 if (pool_elts (gtm->tunnels) == 0)
898 vlib_cli_output (vm, "No gtpu tunnels configured...");
900 pool_foreach (t, gtm->tunnels, (
902 vlib_cli_output (vm, "%U",
903 format_gtpu_tunnel, t);
911 * Display all the GTPU Tunnel entries.
914 * Example of how to display the GTPU Tunnel entries:
915 * @cliexstart{show gtpu tunnel}
916 * [0] src 10.0.3.1 dst 10.0.3.3 teid 13 encap_fib_index 0 sw_if_index 5 decap_next l2
920 VLIB_CLI_COMMAND (show_gtpu_tunnel_command, static) = {
921 .path = "show gtpu tunnel",
922 .short_help = "show gtpu tunnel",
923 .function = show_gtpu_tunnel_command_fn,
928 vnet_int_gtpu_bypass_mode (u32 sw_if_index, u8 is_ip6, u8 is_enable)
931 vnet_feature_enable_disable ("ip6-unicast", "ip6-gtpu-bypass",
932 sw_if_index, is_enable, 0, 0);
934 vnet_feature_enable_disable ("ip4-unicast", "ip4-gtpu-bypass",
935 sw_if_index, is_enable, 0, 0);
938 static clib_error_t *
939 set_ip_gtpu_bypass (u32 is_ip6,
940 unformat_input_t * input, vlib_cli_command_t * cmd)
942 unformat_input_t _line_input, *line_input = &_line_input;
943 vnet_main_t *vnm = vnet_get_main ();
944 clib_error_t *error = 0;
945 u32 sw_if_index, is_enable;
950 if (!unformat_user (input, unformat_line_input, line_input))
953 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
956 (line_input, unformat_vnet_sw_interface, vnm, &sw_if_index))
958 else if (unformat (line_input, "del"))
962 error = unformat_parse_error (line_input);
967 if (~0 == sw_if_index)
969 error = clib_error_return (0, "unknown interface `%U'",
970 format_unformat_error, line_input);
974 vnet_int_gtpu_bypass_mode (sw_if_index, is_ip6, is_enable);
977 unformat_free (line_input);
982 static clib_error_t *
983 set_ip4_gtpu_bypass (vlib_main_t * vm,
984 unformat_input_t * input, vlib_cli_command_t * cmd)
986 return set_ip_gtpu_bypass (0, input, cmd);
990 * This command adds the 'ip4-gtpu-bypass' graph node for a given interface.
991 * By adding the IPv4 gtpu-bypass graph node to an interface, the node checks
992 * for and validate input gtpu packet and bypass ip4-lookup, ip4-local,
993 * ip4-udp-lookup nodes to speedup gtpu packet forwarding. This node will
994 * cause extra overhead to for non-gtpu packets which is kept at a minimum.
998 * Example of graph node before ip4-gtpu-bypass is enabled:
999 * @cliexstart{show vlib graph ip4-gtpu-bypass}
1000 * Name Next Previous
1001 * ip4-gtpu-bypass error-drop [0]
1006 * Example of how to enable ip4-gtpu-bypass on an interface:
1007 * @cliexcmd{set interface ip gtpu-bypass GigabitEthernet2/0/0}
1009 * Example of graph node after ip4-gtpu-bypass is enabled:
1010 * @cliexstart{show vlib graph ip4-gtpu-bypass}
1011 * Name Next Previous
1012 * ip4-gtpu-bypass error-drop [0] ip4-input
1013 * gtpu4-input [1] ip4-input-no-checksum
1017 * Example of how to display the feature enabled on an interface:
1018 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1019 * IP feature paths configured on GigabitEthernet2/0/0...
1027 * Example of how to disable ip4-gtpu-bypass on an interface:
1028 * @cliexcmd{set interface ip gtpu-bypass GigabitEthernet2/0/0 del}
1032 VLIB_CLI_COMMAND (set_interface_ip_gtpu_bypass_command, static) = {
1033 .path = "set interface ip gtpu-bypass",
1034 .function = set_ip4_gtpu_bypass,
1035 .short_help = "set interface ip gtpu-bypass <interface> [del]",
1039 static clib_error_t *
1040 set_ip6_gtpu_bypass (vlib_main_t * vm,
1041 unformat_input_t * input, vlib_cli_command_t * cmd)
1043 return set_ip_gtpu_bypass (1, input, cmd);
1047 * This command adds the 'ip6-gtpu-bypass' graph node for a given interface.
1048 * By adding the IPv6 gtpu-bypass graph node to an interface, the node checks
1049 * for and validate input gtpu packet and bypass ip6-lookup, ip6-local,
1050 * ip6-udp-lookup nodes to speedup gtpu packet forwarding. This node will
1051 * cause extra overhead to for non-gtpu packets which is kept at a minimum.
1055 * Example of graph node before ip6-gtpu-bypass is enabled:
1056 * @cliexstart{show vlib graph ip6-gtpu-bypass}
1057 * Name Next Previous
1058 * ip6-gtpu-bypass error-drop [0]
1063 * Example of how to enable ip6-gtpu-bypass on an interface:
1064 * @cliexcmd{set interface ip6 gtpu-bypass GigabitEthernet2/0/0}
1066 * Example of graph node after ip6-gtpu-bypass is enabled:
1067 * @cliexstart{show vlib graph ip6-gtpu-bypass}
1068 * Name Next Previous
1069 * ip6-gtpu-bypass error-drop [0] ip6-input
1070 * gtpu6-input [1] ip4-input-no-checksum
1074 * Example of how to display the feature enabled on an interface:
1075 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1076 * IP feature paths configured on GigabitEthernet2/0/0...
1084 * Example of how to disable ip6-gtpu-bypass on an interface:
1085 * @cliexcmd{set interface ip6 gtpu-bypass GigabitEthernet2/0/0 del}
1089 VLIB_CLI_COMMAND (set_interface_ip6_gtpu_bypass_command, static) = {
1090 .path = "set interface ip6 gtpu-bypass",
1091 .function = set_ip6_gtpu_bypass,
1092 .short_help = "set interface ip6 gtpu-bypass <interface> [del]",
1097 gtpu_init (vlib_main_t * vm)
1099 gtpu_main_t *gtm = >pu_main;
1101 gtm->vnet_main = vnet_get_main ();
1102 gtm->vlib_main = vm;
1104 /* initialize the ip6 hash */
1105 gtm->gtpu6_tunnel_by_key = hash_create_mem (0,
1106 sizeof (gtpu6_tunnel_key_t),
1108 gtm->vtep6 = hash_create_mem (0, sizeof (ip6_address_t), sizeof (uword));
1109 gtm->mcast_shared = hash_create_mem (0,
1110 sizeof (ip46_address_t),
1111 sizeof (mcast_shared_t));
1113 gtm->fib_node_type = fib_node_register_new_type (>pu_vft);
1118 VLIB_INIT_FUNCTION (gtpu_init);
1121 VLIB_PLUGIN_REGISTER () = {
1122 .version = VPP_BUILD_VER,
1123 .description = "GPRS Tunnelling Protocol, User Data (GTPv1-U)",
1128 * fd.io coding-style-patch-verification: ON
1131 * eval: (c-set-style "gnu")