2 * Copyright (c) 2015 Cisco 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.
17 * @brief Common utility functions for IPv4 and IPv6 VXLAN GPE tunnels
20 #include <vnet/vxlan-gpe/vxlan_gpe.h>
21 #include <vnet/fib/fib.h>
22 #include <vnet/ip/format.h>
23 #include <vnet/fib/fib_entry.h>
24 #include <vnet/fib/fib_table.h>
25 #include <vnet/fib/fib_entry_track.h>
26 #include <vnet/mfib/mfib_table.h>
27 #include <vnet/adj/adj_mcast.h>
28 #include <vnet/interface.h>
29 #include <vnet/udp/udp_local.h>
30 #include <vlib/vlib.h>
36 * VXLAN-GPE provides the features needed to allow L2 bridge domains (BDs)
37 * to span multiple servers. This is done by building an L2 overlay on
38 * top of an L3 network underlay using VXLAN-GPE tunnels.
40 * This makes it possible for servers to be co-located in the same data
41 * center or be separated geographically as long as they are reachable
42 * through the underlay L3 network.
44 * You can refer to this kind of L2 overlay bridge domain as a VXLAN-GPE segment.
47 vxlan_gpe_main_t vxlan_gpe_main;
50 format_decap_next (u8 * s, va_list * args)
52 vxlan_gpe_tunnel_t *t = va_arg (*args, vxlan_gpe_tunnel_t *);
56 case VXLAN_GPE_PROTOCOL_IP4:
57 s = format (s, "protocol ip4 fib-idx %d", t->decap_fib_index);
59 case VXLAN_GPE_PROTOCOL_IP6:
60 s = format (s, "protocol ip6 fib-idx %d", t->decap_fib_index);
62 case VXLAN_GPE_PROTOCOL_ETHERNET:
63 s = format (s, "protocol ethernet");
65 case VXLAN_GPE_PROTOCOL_NSH:
66 s = format (s, "protocol nsh");
69 s = format (s, "protocol unknown %d", t->protocol);
76 * @brief Format function for VXLAN GPE tunnel
78 * @param *s formatting string
81 * @return *s formatted string
85 format_vxlan_gpe_tunnel (u8 * s, va_list * args)
87 vxlan_gpe_tunnel_t *t = va_arg (*args, vxlan_gpe_tunnel_t *);
88 vxlan_gpe_main_t *ngm = &vxlan_gpe_main;
90 s = format (s, "[%d] lcl %U rmt %U vni %d fib-idx %d sw-if-idx %d ",
92 format_ip46_address, &t->local, IP46_TYPE_ANY,
93 format_ip46_address, &t->remote, IP46_TYPE_ANY,
94 t->vni, t->encap_fib_index, t->sw_if_index);
97 /* next_dpo not yet used by vxlan-gpe-encap node */
98 s = format (s, "encap-dpo-idx %d ", t->next_dpo.dpoi_index);
101 s = format (s, "decap-next-%U ", format_decap_next, t);
103 if (PREDICT_FALSE (ip46_address_is_multicast (&t->remote)))
104 s = format (s, "mcast-sw-if-idx %d ", t->mcast_sw_if_index);
110 * @brief Naming for VXLAN GPE tunnel
112 * @param *s formatting string
115 * @return *s formatted string
119 format_vxlan_gpe_name (u8 * s, va_list * args)
121 u32 dev_instance = va_arg (*args, u32);
122 return format (s, "vxlan_gpe_tunnel%d", dev_instance);
126 * @brief CLI function for VXLAN GPE admin up/down
135 static clib_error_t *
136 vxlan_gpe_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
139 u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
140 VNET_HW_INTERFACE_FLAG_LINK_UP : 0;
141 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
147 VNET_DEVICE_CLASS (vxlan_gpe_device_class,static) = {
149 .format_device_name = format_vxlan_gpe_name,
150 .format_tx_trace = format_vxlan_gpe_encap_trace,
151 .admin_up_down_function = vxlan_gpe_interface_admin_up_down,
157 * @brief Formatting function for tracing VXLAN GPE with length
166 format_vxlan_gpe_header_with_length (u8 * s, va_list * args)
168 u32 dev_instance = va_arg (*args, u32);
169 s = format (s, "unimplemented dev %u", dev_instance);
174 VNET_HW_INTERFACE_CLASS (vxlan_gpe_hw_class) = {
176 .format_header = format_vxlan_gpe_header_with_length,
177 .build_rewrite = default_build_rewrite,
182 vxlan_gpe_tunnel_restack_dpo (vxlan_gpe_tunnel_t * t)
184 dpo_id_t dpo = DPO_INVALID;
185 u32 encap_index = vxlan_gpe_encap_node.index;
186 fib_forward_chain_type_t forw_type = ip46_address_is_ip4 (&t->remote) ?
187 FIB_FORW_CHAIN_TYPE_UNICAST_IP4 : FIB_FORW_CHAIN_TYPE_UNICAST_IP6;
189 fib_entry_contribute_forwarding (t->fib_entry_index, forw_type, &dpo);
190 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
194 static vxlan_gpe_tunnel_t *
195 vxlan_gpe_tunnel_from_fib_node (fib_node_t * node)
197 ASSERT (FIB_NODE_TYPE_VXLAN_GPE_TUNNEL == node->fn_type);
198 return ((vxlan_gpe_tunnel_t *) (((char *) node) -
199 STRUCT_OFFSET_OF (vxlan_gpe_tunnel_t,
204 * Function definition to backwalk a FIB node -
205 * Here we will restack the new dpo of VXLAN_GPE DIP to encap node.
207 static fib_node_back_walk_rc_t
208 vxlan_gpe_tunnel_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
210 vxlan_gpe_tunnel_restack_dpo (vxlan_gpe_tunnel_from_fib_node (node));
211 return (FIB_NODE_BACK_WALK_CONTINUE);
215 * Function definition to get a FIB node from its index
218 vxlan_gpe_tunnel_fib_node_get (fib_node_index_t index)
220 vxlan_gpe_tunnel_t *t;
221 vxlan_gpe_main_t *ngm = &vxlan_gpe_main;
223 t = pool_elt_at_index (ngm->tunnels, index);
229 * Function definition to inform the FIB node that its last lock has gone.
232 vxlan_gpe_tunnel_last_lock_gone (fib_node_t * node)
235 * The VXLAN_GPE tunnel is a root of the graph. As such
236 * it never has children and thus is never locked.
242 * Virtual function table registered by VXLAN_GPE tunnels
243 * for participation in the FIB object graph.
245 const static fib_node_vft_t vxlan_gpe_vft = {
246 .fnv_get = vxlan_gpe_tunnel_fib_node_get,
247 .fnv_last_lock = vxlan_gpe_tunnel_last_lock_gone,
248 .fnv_back_walk = vxlan_gpe_tunnel_back_walk,
251 #define foreach_gpe_copy_field \
254 _(mcast_sw_if_index) \
258 #define foreach_copy_ipv4 { \
259 _(local.ip4.as_u32) \
260 _(remote.ip4.as_u32) \
263 #define foreach_copy_ipv6 { \
264 _(local.ip6.as_u64[0]) \
265 _(local.ip6.as_u64[1]) \
266 _(remote.ip6.as_u64[0]) \
267 _(remote.ip6.as_u64[1]) \
272 * @brief Calculate IPv4 VXLAN GPE rewrite header
280 vxlan4_gpe_rewrite (vxlan_gpe_tunnel_t * t, u32 extension_size,
281 u8 protocol_override, uword encap_next_node)
285 ip4_vxlan_gpe_header_t *h0;
288 len = sizeof (*h0) + extension_size;
290 vec_free (t->rewrite);
291 vec_validate_aligned (rw, len - 1, CLIB_CACHE_LINE_BYTES);
293 h0 = (ip4_vxlan_gpe_header_t *) rw;
295 /* Fixed portion of the (outer) ip4 header */
297 ip0->ip_version_and_header_length = 0x45;
299 ip0->protocol = IP_PROTOCOL_UDP;
301 /* we fix up the ip4 header length and checksum after-the-fact */
302 ip0->src_address.as_u32 = t->local.ip4.as_u32;
303 ip0->dst_address.as_u32 = t->remote.ip4.as_u32;
304 ip0->checksum = ip4_header_checksum (ip0);
306 /* UDP header, randomize src port on something, maybe? */
307 h0->udp.src_port = clib_host_to_net_u16 (4790);
308 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_VXLAN_GPE);
310 /* VXLAN header. Are we having fun yet? */
311 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
312 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
313 if (protocol_override)
315 h0->vxlan.protocol = protocol_override;
319 h0->vxlan.protocol = t->protocol;
321 t->rewrite_size = sizeof (ip4_vxlan_gpe_header_t) + extension_size;
322 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni << 8);
325 t->encap_next_node = encap_next_node;
330 * @brief Calculate IPv6 VXLAN GPE rewrite header
338 vxlan6_gpe_rewrite (vxlan_gpe_tunnel_t * t, u32 extension_size,
339 u8 protocol_override, uword encap_next_node)
343 ip6_vxlan_gpe_header_t *h0;
346 len = sizeof (*h0) + extension_size;
348 vec_free (t->rewrite);
349 vec_validate_aligned (rw, len - 1, CLIB_CACHE_LINE_BYTES);
351 h0 = (ip6_vxlan_gpe_header_t *) rw;
353 /* Fixed portion of the (outer) ip4 header */
355 ip0->ip_version_traffic_class_and_flow_label =
356 clib_host_to_net_u32 (6 << 28);
357 ip0->hop_limit = 255;
358 ip0->protocol = IP_PROTOCOL_UDP;
360 ip0->src_address.as_u64[0] = t->local.ip6.as_u64[0];
361 ip0->src_address.as_u64[1] = t->local.ip6.as_u64[1];
362 ip0->dst_address.as_u64[0] = t->remote.ip6.as_u64[0];
363 ip0->dst_address.as_u64[1] = t->remote.ip6.as_u64[1];
365 /* UDP header, randomize src port on something, maybe? */
366 h0->udp.src_port = clib_host_to_net_u16 (4790);
367 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_VXLAN_GPE);
369 /* VXLAN header. Are we having fun yet? */
370 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
371 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
372 if (protocol_override)
374 h0->vxlan.protocol = t->protocol;
378 h0->vxlan.protocol = protocol_override;
380 t->rewrite_size = sizeof (ip4_vxlan_gpe_header_t) + extension_size;
381 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni << 8);
384 t->encap_next_node = encap_next_node;
389 typedef CLIB_PACKED(union {
391 fib_node_index_t mfib_entry_index;
392 adj_index_t mcast_adj_index;
398 static inline mcast_shared_t
399 mcast_shared_get (ip46_address_t * ip)
401 ASSERT (ip46_address_is_multicast (ip));
402 uword *p = hash_get_mem (vxlan_gpe_main.mcast_shared, ip);
404 return (mcast_shared_t)
410 mcast_shared_add (ip46_address_t * remote,
411 fib_node_index_t mfei, adj_index_t ai)
413 mcast_shared_t new_ep = {
414 .mcast_adj_index = ai,
415 .mfib_entry_index = mfei,
418 hash_set_mem_alloc (&vxlan_gpe_main.mcast_shared, remote, new_ep.as_u64);
422 mcast_shared_remove (ip46_address_t * remote)
424 mcast_shared_t ep = mcast_shared_get (remote);
426 adj_unlock (ep.mcast_adj_index);
427 mfib_table_entry_delete_index (ep.mfib_entry_index, MFIB_SOURCE_VXLAN_GPE);
429 hash_unset_mem_free (&vxlan_gpe_main.mcast_shared, remote);
433 * @brief Add or Del a VXLAN GPE tunnel
436 * @param *sw_if_index
441 int vnet_vxlan_gpe_add_del_tunnel
442 (vnet_vxlan_gpe_add_del_tunnel_args_t * a, u32 * sw_if_indexp)
444 vxlan_gpe_main_t *ngm = &vxlan_gpe_main;
445 vxlan_gpe_tunnel_t *t = 0;
446 vnet_main_t *vnm = ngm->vnet_main;
447 vnet_hw_interface_t *hi;
449 u32 hw_if_index = ~0;
450 u32 sw_if_index = ~0;
452 vxlan4_gpe_tunnel_key_t key4, *key4_copy;
453 vxlan6_gpe_tunnel_key_t key6, *key6_copy;
454 u32 is_ip6 = a->is_ip6;
458 key4.local = a->local.ip4.as_u32;
459 key4.remote = a->remote.ip4.as_u32;
460 key4.vni = clib_host_to_net_u32 (a->vni << 8);
463 p = hash_get_mem (ngm->vxlan4_gpe_tunnel_by_key, &key4);
467 key6.local.as_u64[0] = a->local.ip6.as_u64[0];
468 key6.local.as_u64[1] = a->local.ip6.as_u64[1];
469 key6.remote.as_u64[0] = a->remote.ip6.as_u64[0];
470 key6.remote.as_u64[1] = a->remote.ip6.as_u64[1];
471 key6.vni = clib_host_to_net_u32 (a->vni << 8);
473 p = hash_get_mem (ngm->vxlan6_gpe_tunnel_by_key, &key6);
478 l2input_main_t *l2im = &l2input_main;
480 /* adding a tunnel: tunnel must not already exist */
482 return VNET_API_ERROR_TUNNEL_EXIST;
484 pool_get_aligned (ngm->tunnels, t, CLIB_CACHE_LINE_BYTES);
485 clib_memset (t, 0, sizeof (*t));
487 /* copy from arg structure */
489 #define _(x) t->x = a->x;
490 foreach_gpe_copy_field;
499 t->flags |= VXLAN_GPE_TUNNEL_IS_IPV4;
503 rv = vxlan4_gpe_rewrite (t, 0, 0, VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP);
507 rv = vxlan6_gpe_rewrite (t, 0, 0, VXLAN_GPE_ENCAP_NEXT_IP6_LOOKUP);
512 pool_put (ngm->tunnels, t);
518 key4_copy = clib_mem_alloc (sizeof (*key4_copy));
519 clib_memcpy_fast (key4_copy, &key4, sizeof (*key4_copy));
520 hash_set_mem (ngm->vxlan4_gpe_tunnel_by_key, key4_copy,
525 key6_copy = clib_mem_alloc (sizeof (*key6_copy));
526 clib_memcpy_fast (key6_copy, &key6, sizeof (*key6_copy));
527 hash_set_mem (ngm->vxlan6_gpe_tunnel_by_key, key6_copy,
531 if (vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices) > 0)
533 vnet_interface_main_t *im = &vnm->interface_main;
534 hw_if_index = ngm->free_vxlan_gpe_tunnel_hw_if_indices
535 [vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices) - 1];
536 _vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices) -= 1;
538 hi = vnet_get_hw_interface (vnm, hw_if_index);
539 hi->dev_instance = t - ngm->tunnels;
540 hi->hw_instance = hi->dev_instance;
541 /* clear old stats of freed tunnel before reuse */
542 sw_if_index = hi->sw_if_index;
543 vnet_interface_counter_lock (im);
544 vlib_zero_combined_counter
545 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX],
547 vlib_zero_combined_counter (&im->combined_sw_if_counters
548 [VNET_INTERFACE_COUNTER_RX],
550 vlib_zero_simple_counter (&im->sw_if_counters
551 [VNET_INTERFACE_COUNTER_DROP],
553 vnet_interface_counter_unlock (im);
557 hw_if_index = vnet_register_interface
558 (vnm, vxlan_gpe_device_class.index, t - ngm->tunnels,
559 vxlan_gpe_hw_class.index, t - ngm->tunnels);
560 hi = vnet_get_hw_interface (vnm, hw_if_index);
563 /* Set vxlan-gpe tunnel output node */
564 u32 encap_index = vxlan_gpe_encap_node.index;
565 vnet_set_interface_output_node (vnm, hw_if_index, encap_index);
567 t->hw_if_index = hw_if_index;
568 t->sw_if_index = sw_if_index = hi->sw_if_index;
569 vec_validate_init_empty (ngm->tunnel_index_by_sw_if_index, sw_if_index,
571 ngm->tunnel_index_by_sw_if_index[sw_if_index] = t - ngm->tunnels;
573 /* setup l2 input config with l2 feature and bd 0 to drop packet */
574 vec_validate (l2im->configs, sw_if_index);
575 l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
576 l2im->configs[sw_if_index].bd_index = 0;
578 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
579 si->flags &= ~VNET_SW_INTERFACE_FLAG_HIDDEN;
580 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
581 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
582 fib_node_init (&t->node, FIB_NODE_TYPE_VXLAN_GPE_TUNNEL);
583 fib_prefix_t tun_remote_pfx;
584 vnet_flood_class_t flood_class = VNET_FLOOD_CLASS_TUNNEL_NORMAL;
586 fib_prefix_from_ip46_addr (&t->remote, &tun_remote_pfx);
587 if (!ip46_address_is_multicast (&t->remote))
590 * source the FIB entry for the tunnel's destination
591 * and become a child thereof. The tunnel will then get poked
592 * when the forwarding for the entry updates, and the tunnel can
593 * re-stack accordingly
595 vtep_addr_ref (&ngm->vtep_table, t->encap_fib_index, &t->local);
596 t->fib_entry_index = fib_entry_track (t->encap_fib_index,
598 FIB_NODE_TYPE_VXLAN_GPE_TUNNEL,
601 vxlan_gpe_tunnel_restack_dpo (t);
605 /* Multicast tunnel -
606 * as the same mcast group can be used for multiple mcast tunnels
607 * with different VNIs, create the output fib adjacency only if
608 * it does not already exist
610 fib_protocol_t fp = fib_ip_proto (is_ip6);
612 if (vtep_addr_ref (&ngm->vtep_table,
613 t->encap_fib_index, &t->remote) == 1)
615 fib_node_index_t mfei;
617 fib_route_path_t path = {
618 .frp_proto = fib_proto_to_dpo (fp),
619 .frp_addr = zero_addr,
620 .frp_sw_if_index = 0xffffffff,
623 .frp_flags = FIB_ROUTE_PATH_LOCAL,
624 .frp_mitf_flags = MFIB_ITF_FLAG_FORWARD,
626 const mfib_prefix_t mpfx = {
628 .fp_len = (is_ip6 ? 128 : 32),
629 .fp_grp_addr = tun_remote_pfx.fp_addr,
633 * Setup the (*,G) to receive traffic on the mcast group
634 * - the forwarding interface is for-us
635 * - the accepting interface is that from the API
637 mfib_table_entry_path_update (t->encap_fib_index,
639 MFIB_SOURCE_VXLAN_GPE, &path);
641 path.frp_sw_if_index = a->mcast_sw_if_index;
642 path.frp_flags = FIB_ROUTE_PATH_FLAG_NONE;
643 path.frp_mitf_flags = MFIB_ITF_FLAG_ACCEPT;
644 mfei = mfib_table_entry_path_update (t->encap_fib_index,
646 MFIB_SOURCE_VXLAN_GPE,
650 * Create the mcast adjacency to send traffic to the group
652 ai = adj_mcast_add_or_lock (fp,
653 fib_proto_to_link (fp),
654 a->mcast_sw_if_index);
657 * create a new end-point
659 mcast_shared_add (&t->remote, mfei, ai);
662 dpo_id_t dpo = DPO_INVALID;
663 mcast_shared_t ep = mcast_shared_get (&t->remote);
665 /* Stack shared mcast remote mac addr rewrite on encap */
666 dpo_set (&dpo, DPO_ADJACENCY_MCAST,
667 fib_proto_to_dpo (fp), ep.mcast_adj_index);
669 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
671 flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER;
674 vnet_get_sw_interface (vnet_get_main (), sw_if_index)->flood_class =
679 /* deleting a tunnel: tunnel must exist */
681 return VNET_API_ERROR_NO_SUCH_ENTRY;
683 t = pool_elt_at_index (ngm->tunnels, p[0]);
685 sw_if_index = t->sw_if_index;
686 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */ );
687 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, t->sw_if_index);
688 si->flags |= VNET_SW_INTERFACE_FLAG_HIDDEN;
689 set_int_l2_mode (ngm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0,
690 L2_BD_PORT_TYPE_NORMAL, 0, 0);
691 vec_add1 (ngm->free_vxlan_gpe_tunnel_hw_if_indices, t->hw_if_index);
693 ngm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
696 hash_unset (ngm->vxlan4_gpe_tunnel_by_key, key4.as_u64);
698 hash_unset_mem_free (&ngm->vxlan6_gpe_tunnel_by_key, &key6);
700 if (!ip46_address_is_multicast (&t->remote))
702 vtep_addr_unref (&ngm->vtep_table, t->encap_fib_index, &t->local);
703 fib_entry_untrack (t->fib_entry_index, t->sibling_index);
705 else if (vtep_addr_unref (&ngm->vtep_table,
706 t->encap_fib_index, &t->remote) == 0)
708 mcast_shared_remove (&t->remote);
711 fib_node_deinit (&t->node);
712 vec_free (t->rewrite);
713 pool_put (ngm->tunnels, t);
717 *sw_if_indexp = sw_if_index;
721 /* register udp ports */
722 if (!is_ip6 && !udp_is_valid_dst_port (UDP_DST_PORT_VXLAN_GPE, 1))
723 udp_register_dst_port (ngm->vlib_main, UDP_DST_PORT_VXLAN_GPE,
724 vxlan4_gpe_input_node.index, 1 /* is_ip4 */ );
725 if (is_ip6 && !udp_is_valid_dst_port (UDP_DST_PORT_VXLAN6_GPE, 0))
726 udp_register_dst_port (ngm->vlib_main, UDP_DST_PORT_VXLAN6_GPE,
727 vxlan6_gpe_input_node.index, 0 /* is_ip4 */ );
733 static clib_error_t *
734 vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm,
735 unformat_input_t * input,
736 vlib_cli_command_t * cmd)
738 unformat_input_t _line_input, *line_input = &_line_input;
740 ip46_address_t local, remote;
746 u32 mcast_sw_if_index = ~0;
747 u32 encap_fib_index = 0;
748 u32 decap_fib_index = 0;
749 u8 protocol = VXLAN_GPE_PROTOCOL_IP4;
754 vnet_vxlan_gpe_add_del_tunnel_args_t _a, *a = &_a;
756 clib_error_t *error = NULL;
758 /* Get a line of input. */
759 if (!unformat_user (input, unformat_line_input, line_input))
762 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
764 if (unformat (line_input, "del"))
766 else if (unformat (line_input, "local %U",
767 unformat_ip4_address, &local.ip4))
772 else if (unformat (line_input, "remote %U",
773 unformat_ip4_address, &remote.ip4))
778 else if (unformat (line_input, "local %U",
779 unformat_ip6_address, &local.ip6))
784 else if (unformat (line_input, "remote %U",
785 unformat_ip6_address, &remote.ip6))
790 else if (unformat (line_input, "group %U %U",
791 unformat_ip4_address, &remote.ip4,
792 unformat_vnet_sw_interface,
793 vnet_get_main (), &mcast_sw_if_index))
795 grp_set = remote_set = 1;
798 else if (unformat (line_input, "group %U %U",
799 unformat_ip6_address, &remote.ip6,
800 unformat_vnet_sw_interface,
801 vnet_get_main (), &mcast_sw_if_index))
803 grp_set = remote_set = 1;
806 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
809 encap_fib_index = fib_table_find (FIB_PROTOCOL_IP6, tmp);
811 encap_fib_index = fib_table_find (FIB_PROTOCOL_IP4, tmp);
813 if (encap_fib_index == ~0)
816 clib_error_return (0, "nonexistent encap fib id %d", tmp);
820 else if (unformat (line_input, "decap-vrf-id %d", &tmp))
823 decap_fib_index = fib_table_find (FIB_PROTOCOL_IP6, tmp);
825 decap_fib_index = fib_table_find (FIB_PROTOCOL_IP4, tmp);
827 if (decap_fib_index == ~0)
830 clib_error_return (0, "nonexistent decap fib id %d", tmp);
834 else if (unformat (line_input, "vni %d", &vni))
836 else if (unformat (line_input, "next-ip4"))
837 protocol = VXLAN_GPE_PROTOCOL_IP4;
838 else if (unformat (line_input, "next-ip6"))
839 protocol = VXLAN_GPE_PROTOCOL_IP6;
840 else if (unformat (line_input, "next-ethernet"))
841 protocol = VXLAN_GPE_PROTOCOL_ETHERNET;
842 else if (unformat (line_input, "next-nsh"))
843 protocol = VXLAN_GPE_PROTOCOL_NSH;
846 error = clib_error_return (0, "parse error: '%U'",
847 format_unformat_error, line_input);
854 error = clib_error_return (0, "tunnel local address not specified");
860 error = clib_error_return (0, "tunnel remote address not specified");
864 if (grp_set && !ip46_address_is_multicast (&remote))
866 error = clib_error_return (0, "tunnel group address not multicast");
870 if (grp_set == 0 && ip46_address_is_multicast (&remote))
872 error = clib_error_return (0, "remote address must be unicast");
876 if (grp_set && mcast_sw_if_index == ~0)
878 error = clib_error_return (0, "tunnel nonexistent multicast device");
881 if (ipv4_set && ipv6_set)
883 error = clib_error_return (0, "both IPv4 and IPv6 addresses specified");
887 if ((ipv4_set && memcmp (&local.ip4, &remote.ip4, sizeof (local.ip4)) == 0)
889 && memcmp (&local.ip6, &remote.ip6, sizeof (local.ip6)) == 0))
891 error = clib_error_return (0, "src and remote addresses are identical");
897 error = clib_error_return (0, "vni not specified");
901 clib_memset (a, 0, sizeof (*a));
904 a->is_ip6 = ipv6_set;
907 #define _(x) a->x = x;
908 foreach_gpe_copy_field;
916 rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
921 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
922 vnet_get_main (), sw_if_index);
924 case VNET_API_ERROR_INVALID_DECAP_NEXT:
925 error = clib_error_return (0, "invalid decap-next...");
928 case VNET_API_ERROR_TUNNEL_EXIST:
929 error = clib_error_return (0, "tunnel already exists...");
932 case VNET_API_ERROR_NO_SUCH_ENTRY:
933 error = clib_error_return (0, "tunnel does not exist...");
937 error = clib_error_return
938 (0, "vnet_vxlan_gpe_add_del_tunnel returned %d", rv);
943 unformat_free (line_input);
949 * Add or delete a VXLAN-GPE Tunnel.
951 * VXLAN-GPE provides the features needed to allow L2 bridge domains (BDs)
952 * to span multiple servers. This is done by building an L2 overlay on
953 * top of an L3 network underlay using VXLAN-GPE tunnels.
955 * This makes it possible for servers to be co-located in the same data
956 * center or be separated geographically as long as they are reachable
957 * through the underlay L3 network.
959 * You can refer to this kind of L2 overlay bridge domain as a VXLAN-GPE segment.
962 * Example of how to create a VXLAN-GPE Tunnel:
963 * @cliexcmd{create vxlan-gpe tunnel local 10.0.3.1 remote 10.0.3.3 vni 13 encap-vrf-id 7}
964 * Example of how to delete a VXLAN-GPE Tunnel:
965 * @cliexcmd{create vxlan-gpe tunnel local 10.0.3.1 remote 10.0.3.3 vni 13 del}
968 VLIB_CLI_COMMAND (create_vxlan_gpe_tunnel_command, static) = {
969 .path = "create vxlan-gpe tunnel",
971 "create vxlan-gpe tunnel local <local-addr> "
972 " {remote <remote-addr>|group <mcast-addr> <intf-name>}"
973 " vni <nn> [next-ip4][next-ip6][next-ethernet][next-nsh]"
974 " [encap-vrf-id <nn>] [decap-vrf-id <nn>] [del]\n",
975 .function = vxlan_gpe_add_del_tunnel_command_fn,
980 * @brief CLI function for showing VXLAN GPE tunnels
989 static clib_error_t *
990 show_vxlan_gpe_tunnel_command_fn (vlib_main_t * vm,
991 unformat_input_t * input,
992 vlib_cli_command_t * cmd)
994 vxlan_gpe_main_t *ngm = &vxlan_gpe_main;
995 vxlan_gpe_tunnel_t *t;
997 if (pool_elts (ngm->tunnels) == 0)
998 vlib_cli_output (vm, "No vxlan-gpe tunnels configured.");
1001 pool_foreach (t, ngm->tunnels,
1003 vlib_cli_output (vm, "%U", format_vxlan_gpe_tunnel, t);
1011 * Display all the VXLAN-GPE Tunnel entries.
1014 * Example of how to display the VXLAN-GPE Tunnel entries:
1015 * @cliexstart{show vxlan-gpe tunnel}
1016 * [0] local 10.0.3.1 remote 10.0.3.3 vni 13 encap_fib_index 0 sw_if_index 5 decap_next l2
1020 VLIB_CLI_COMMAND (show_vxlan_gpe_tunnel_command, static) = {
1021 .path = "show vxlan-gpe",
1022 .function = show_vxlan_gpe_tunnel_command_fn,
1027 vnet_int_vxlan_gpe_bypass_mode (u32 sw_if_index, u8 is_ip6, u8 is_enable)
1030 vnet_feature_enable_disable ("ip6-unicast", "ip6-vxlan-gpe-bypass",
1031 sw_if_index, is_enable, 0, 0);
1033 vnet_feature_enable_disable ("ip4-unicast", "ip4-vxlan-gpe-bypass",
1034 sw_if_index, is_enable, 0, 0);
1038 static clib_error_t *
1039 set_ip_vxlan_gpe_bypass (u32 is_ip6,
1040 unformat_input_t * input, vlib_cli_command_t * cmd)
1042 unformat_input_t _line_input, *line_input = &_line_input;
1043 vnet_main_t *vnm = vnet_get_main ();
1044 clib_error_t *error = 0;
1045 u32 sw_if_index, is_enable;
1050 if (!unformat_user (input, unformat_line_input, line_input))
1053 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1056 (line_input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1058 else if (unformat (line_input, "del"))
1062 error = unformat_parse_error (line_input);
1067 if (~0 == sw_if_index)
1069 error = clib_error_return (0, "unknown interface `%U'",
1070 format_unformat_error, line_input);
1074 vnet_int_vxlan_gpe_bypass_mode (sw_if_index, is_ip6, is_enable);
1077 unformat_free (line_input);
1082 static clib_error_t *
1083 set_ip4_vxlan_gpe_bypass (vlib_main_t * vm,
1084 unformat_input_t * input, vlib_cli_command_t * cmd)
1086 return set_ip_vxlan_gpe_bypass (0, input, cmd);
1090 * This command adds the 'ip4-vxlan-gpe-bypass' graph node for a given interface.
1091 * By adding the IPv4 vxlan-gpe-bypass graph node to an interface, the node checks
1092 * for and validate input vxlan_gpe packet and bypass ip4-lookup, ip4-local,
1093 * ip4-udp-lookup nodes to speedup vxlan_gpe packet forwarding. This node will
1094 * cause extra overhead to for non-vxlan_gpe packets which is kept at a minimum.
1098 * Example of graph node before ip4-vxlan-gpe-bypass is enabled:
1099 * @cliexstart{show vlib graph ip4-vxlan-gpe-bypass}
1100 * Name Next Previous
1101 * ip4-vxlan-gpe-bypass error-drop [0]
1102 * vxlan4-gpe-input [1]
1106 * Example of how to enable ip4-vxlan-gpe-bypass on an interface:
1107 * @cliexcmd{set interface ip vxlan-gpe-bypass GigabitEthernet2/0/0}
1109 * Example of graph node after ip4-vxlan-gpe-bypass is enabled:
1110 * @cliexstart{show vlib graph ip4-vxlan-gpe-bypass}
1111 * Name Next Previous
1112 * ip4-vxlan-gpe-bypass error-drop [0] ip4-input
1113 * vxlan4-gpe-input [1] ip4-input-no-checksum
1117 * Example of how to display the feature enabled on an interface:
1118 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1119 * IP feature paths configured on GigabitEthernet2/0/0...
1122 * ip4-vxlan-gpe-bypass
1127 * Example of how to disable ip4-vxlan-gpe-bypass on an interface:
1128 * @cliexcmd{set interface ip vxlan-gpe-bypass GigabitEthernet2/0/0 del}
1132 VLIB_CLI_COMMAND (set_interface_ip_vxlan_gpe_bypass_command, static) = {
1133 .path = "set interface ip vxlan-gpe-bypass",
1134 .function = set_ip4_vxlan_gpe_bypass,
1135 .short_help = "set interface ip vxlan-gpe-bypass <interface> [del]",
1139 static clib_error_t *
1140 set_ip6_vxlan_gpe_bypass (vlib_main_t * vm,
1141 unformat_input_t * input, vlib_cli_command_t * cmd)
1143 return set_ip_vxlan_gpe_bypass (1, input, cmd);
1147 * This command adds the 'ip6-vxlan-gpe-bypass' graph node for a given interface.
1148 * By adding the IPv6 vxlan-gpe-bypass graph node to an interface, the node checks
1149 * for and validate input vxlan_gpe packet and bypass ip6-lookup, ip6-local,
1150 * ip6-udp-lookup nodes to speedup vxlan_gpe packet forwarding. This node will
1151 * cause extra overhead to for non-vxlan_gpe packets which is kept at a minimum.
1155 * Example of graph node before ip6-vxlan-gpe-bypass is enabled:
1156 * @cliexstart{show vlib graph ip6-vxlan-gpe-bypass}
1157 * Name Next Previous
1158 * ip6-vxlan-gpe-bypass error-drop [0]
1159 * vxlan6-gpe-input [1]
1163 * Example of how to enable ip6-vxlan-gpe-bypass on an interface:
1164 * @cliexcmd{set interface ip6 vxlan-gpe-bypass GigabitEthernet2/0/0}
1166 * Example of graph node after ip6-vxlan-gpe-bypass is enabled:
1167 * @cliexstart{show vlib graph ip6-vxlan-gpe-bypass}
1168 * Name Next Previous
1169 * ip6-vxlan-gpe-bypass error-drop [0] ip6-input
1170 * vxlan6-gpe-input [1] ip4-input-no-checksum
1174 * Example of how to display the feature enabled on an interface:
1175 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1176 * IP feature paths configured on GigabitEthernet2/0/0...
1179 * ip6-vxlan-gpe-bypass
1184 * Example of how to disable ip6-vxlan-gpe-bypass on an interface:
1185 * @cliexcmd{set interface ip6 vxlan-gpe-bypass GigabitEthernet2/0/0 del}
1189 VLIB_CLI_COMMAND (set_interface_ip6_vxlan_gpe_bypass_command, static) = {
1190 .path = "set interface ip6 vxlan-gpe-bypass",
1191 .function = set_ip6_vxlan_gpe_bypass,
1192 .short_help = "set interface ip6 vxlan-gpe-bypass <interface> [del]",
1197 VNET_FEATURE_INIT (ip4_vxlan_gpe_bypass, static) =
1199 .arc_name = "ip4-unicast",
1200 .node_name = "ip4-vxlan-gpe-bypass",
1201 .runs_before = VNET_FEATURES ("ip4-lookup"),
1204 VNET_FEATURE_INIT (ip6_vxlan_gpe_bypass, static) =
1206 .arc_name = "ip6-unicast",
1207 .node_name = "ip6-vxlan-gpe-bypass",
1208 .runs_before = VNET_FEATURES ("ip6-lookup"),
1213 * @brief Feature init function for VXLAN GPE
1221 vxlan_gpe_init (vlib_main_t * vm)
1223 vxlan_gpe_main_t *ngm = &vxlan_gpe_main;
1225 ngm->vnet_main = vnet_get_main ();
1226 ngm->vlib_main = vm;
1228 ngm->vxlan4_gpe_tunnel_by_key
1229 = hash_create_mem (0, sizeof (vxlan4_gpe_tunnel_key_t), sizeof (uword));
1231 ngm->vxlan6_gpe_tunnel_by_key
1232 = hash_create_mem (0, sizeof (vxlan6_gpe_tunnel_key_t), sizeof (uword));
1235 ngm->mcast_shared = hash_create_mem (0,
1236 sizeof (ip46_address_t),
1237 sizeof (mcast_shared_t));
1238 ngm->vtep_table = vtep_table_create ();
1240 /* Register the list of standard decap protocols supported */
1241 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_IP4,
1242 VXLAN_GPE_INPUT_NEXT_IP4_INPUT);
1243 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_IP6,
1244 VXLAN_GPE_INPUT_NEXT_IP6_INPUT);
1245 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_ETHERNET,
1246 VXLAN_GPE_INPUT_NEXT_L2_INPUT);
1248 fib_node_register_type (FIB_NODE_TYPE_VXLAN_GPE_TUNNEL, &vxlan_gpe_vft);
1253 VLIB_INIT_FUNCTION (vxlan_gpe_init);
1257 * fd.io coding-style-patch-verification: ON
1260 * eval: (c-set-style "gnu")