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/mfib/mfib_table.h>
26 #include <vnet/adj/adj_mcast.h>
27 #include <vnet/interface.h>
28 #include <vlib/vlib.h>
34 * VXLAN-GPE provides the features needed to allow L2 bridge domains (BDs)
35 * to span multiple servers. This is done by building an L2 overlay on
36 * top of an L3 network underlay using VXLAN-GPE tunnels.
38 * This makes it possible for servers to be co-located in the same data
39 * center or be separated geographically as long as they are reachable
40 * through the underlay L3 network.
42 * You can refer to this kind of L2 overlay bridge domain as a VXLAN-GPE segment.
45 vxlan_gpe_main_t vxlan_gpe_main;
48 format_decap_next (u8 * s, va_list * args)
50 vxlan_gpe_tunnel_t *t = va_arg (*args, vxlan_gpe_tunnel_t *);
54 case VXLAN_GPE_PROTOCOL_IP4:
55 s = format (s, "protocol ip4 fib-idx %d", t->decap_fib_index);
57 case VXLAN_GPE_PROTOCOL_IP6:
58 s = format (s, "protocol ip6 fib-idx %d", t->decap_fib_index);
60 case VXLAN_GPE_PROTOCOL_ETHERNET:
61 s = format (s, "protocol ethernet");
63 case VXLAN_GPE_PROTOCOL_NSH:
64 s = format (s, "protocol nsh");
67 s = format (s, "protocol unknown %d", t->protocol);
74 * @brief Format function for VXLAN GPE tunnel
76 * @param *s formatting string
79 * @return *s formatted string
83 format_vxlan_gpe_tunnel (u8 * s, va_list * args)
85 vxlan_gpe_tunnel_t *t = va_arg (*args, vxlan_gpe_tunnel_t *);
86 vxlan_gpe_main_t *ngm = &vxlan_gpe_main;
88 s = format (s, "[%d] lcl %U rmt %U vni %d fib-idx %d sw-if-idx %d ",
90 format_ip46_address, &t->local, IP46_TYPE_ANY,
91 format_ip46_address, &t->remote, IP46_TYPE_ANY,
92 t->vni, t->encap_fib_index, t->sw_if_index);
95 /* next_dpo not yet used by vxlan-gpe-encap node */
96 s = format (s, "encap-dpo-idx %d ", t->next_dpo.dpoi_index);
99 s = format (s, "decap-next-%U ", format_decap_next, t);
101 if (PREDICT_FALSE (ip46_address_is_multicast (&t->remote)))
102 s = format (s, "mcast-sw-if-idx %d ", t->mcast_sw_if_index);
108 * @brief Naming for VXLAN GPE tunnel
110 * @param *s formatting string
113 * @return *s formatted string
117 format_vxlan_gpe_name (u8 * s, va_list * args)
119 u32 dev_instance = va_arg (*args, u32);
120 return format (s, "vxlan_gpe_tunnel%d", dev_instance);
124 dummy_interface_tx (vlib_main_t * vm,
125 vlib_node_runtime_t * node, vlib_frame_t * frame)
127 clib_warning ("you shouldn't be here, leaking buffers...");
128 return frame->n_vectors;
132 * @brief CLI function for VXLAN GPE admin up/down
141 static clib_error_t *
142 vxlan_gpe_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
145 u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
146 VNET_HW_INTERFACE_FLAG_LINK_UP : 0;
147 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
153 VNET_DEVICE_CLASS (vxlan_gpe_device_class,static) = {
155 .format_device_name = format_vxlan_gpe_name,
156 .format_tx_trace = format_vxlan_gpe_encap_trace,
157 .tx_function = dummy_interface_tx,
158 .admin_up_down_function = vxlan_gpe_interface_admin_up_down,
164 * @brief Formatting function for tracing VXLAN GPE with length
173 format_vxlan_gpe_header_with_length (u8 * s, va_list * args)
175 u32 dev_instance = va_arg (*args, u32);
176 s = format (s, "unimplemented dev %u", dev_instance);
181 VNET_HW_INTERFACE_CLASS (vxlan_gpe_hw_class) = {
183 .format_header = format_vxlan_gpe_header_with_length,
184 .build_rewrite = default_build_rewrite,
189 vxlan_gpe_tunnel_restack_dpo (vxlan_gpe_tunnel_t * t)
191 dpo_id_t dpo = DPO_INVALID;
192 u32 encap_index = vxlan_gpe_encap_node.index;
193 fib_forward_chain_type_t forw_type = ip46_address_is_ip4 (&t->remote) ?
194 FIB_FORW_CHAIN_TYPE_UNICAST_IP4 : FIB_FORW_CHAIN_TYPE_UNICAST_IP6;
196 fib_entry_contribute_forwarding (t->fib_entry_index, forw_type, &dpo);
197 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
201 static vxlan_gpe_tunnel_t *
202 vxlan_gpe_tunnel_from_fib_node (fib_node_t * node)
204 ASSERT (FIB_NODE_TYPE_VXLAN_GPE_TUNNEL == node->fn_type);
205 return ((vxlan_gpe_tunnel_t *) (((char *) node) -
206 STRUCT_OFFSET_OF (vxlan_gpe_tunnel_t,
211 * Function definition to backwalk a FIB node -
212 * Here we will restack the new dpo of VXLAN_GPE DIP to encap node.
214 static fib_node_back_walk_rc_t
215 vxlan_gpe_tunnel_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
217 vxlan_gpe_tunnel_restack_dpo (vxlan_gpe_tunnel_from_fib_node (node));
218 return (FIB_NODE_BACK_WALK_CONTINUE);
222 * Function definition to get a FIB node from its index
225 vxlan_gpe_tunnel_fib_node_get (fib_node_index_t index)
227 vxlan_gpe_tunnel_t *t;
228 vxlan_gpe_main_t *ngm = &vxlan_gpe_main;
230 t = pool_elt_at_index (ngm->tunnels, index);
236 * Function definition to inform the FIB node that its last lock has gone.
239 vxlan_gpe_tunnel_last_lock_gone (fib_node_t * node)
242 * The VXLAN_GPE tunnel is a root of the graph. As such
243 * it never has children and thus is never locked.
249 * Virtual function table registered by VXLAN_GPE tunnels
250 * for participation in the FIB object graph.
252 const static fib_node_vft_t vxlan_gpe_vft = {
253 .fnv_get = vxlan_gpe_tunnel_fib_node_get,
254 .fnv_last_lock = vxlan_gpe_tunnel_last_lock_gone,
255 .fnv_back_walk = vxlan_gpe_tunnel_back_walk,
258 #define foreach_gpe_copy_field \
261 _(mcast_sw_if_index) \
265 #define foreach_copy_ipv4 { \
266 _(local.ip4.as_u32) \
267 _(remote.ip4.as_u32) \
270 #define foreach_copy_ipv6 { \
271 _(local.ip6.as_u64[0]) \
272 _(local.ip6.as_u64[1]) \
273 _(remote.ip6.as_u64[0]) \
274 _(remote.ip6.as_u64[1]) \
279 * @brief Calculate IPv4 VXLAN GPE rewrite header
287 vxlan4_gpe_rewrite (vxlan_gpe_tunnel_t * t, u32 extension_size,
288 u8 protocol_override, uword encap_next_node)
292 ip4_vxlan_gpe_header_t *h0;
295 len = sizeof (*h0) + extension_size;
297 vec_free (t->rewrite);
298 vec_validate_aligned (rw, len - 1, CLIB_CACHE_LINE_BYTES);
300 h0 = (ip4_vxlan_gpe_header_t *) rw;
302 /* Fixed portion of the (outer) ip4 header */
304 ip0->ip_version_and_header_length = 0x45;
306 ip0->protocol = IP_PROTOCOL_UDP;
308 /* we fix up the ip4 header length and checksum after-the-fact */
309 ip0->src_address.as_u32 = t->local.ip4.as_u32;
310 ip0->dst_address.as_u32 = t->remote.ip4.as_u32;
311 ip0->checksum = ip4_header_checksum (ip0);
313 /* UDP header, randomize src port on something, maybe? */
314 h0->udp.src_port = clib_host_to_net_u16 (4790);
315 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_VXLAN_GPE);
317 /* VXLAN header. Are we having fun yet? */
318 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
319 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
320 if (protocol_override)
322 h0->vxlan.protocol = protocol_override;
326 h0->vxlan.protocol = t->protocol;
328 t->rewrite_size = sizeof (ip4_vxlan_gpe_header_t) + extension_size;
329 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni << 8);
332 t->encap_next_node = encap_next_node;
337 * @brief Calculate IPv6 VXLAN GPE rewrite header
345 vxlan6_gpe_rewrite (vxlan_gpe_tunnel_t * t, u32 extension_size,
346 u8 protocol_override, uword encap_next_node)
350 ip6_vxlan_gpe_header_t *h0;
353 len = sizeof (*h0) + extension_size;
355 vec_free (t->rewrite);
356 vec_validate_aligned (rw, len - 1, CLIB_CACHE_LINE_BYTES);
358 h0 = (ip6_vxlan_gpe_header_t *) rw;
360 /* Fixed portion of the (outer) ip4 header */
362 ip0->ip_version_traffic_class_and_flow_label =
363 clib_host_to_net_u32 (6 << 28);
364 ip0->hop_limit = 255;
365 ip0->protocol = IP_PROTOCOL_UDP;
367 ip0->src_address.as_u64[0] = t->local.ip6.as_u64[0];
368 ip0->src_address.as_u64[1] = t->local.ip6.as_u64[1];
369 ip0->dst_address.as_u64[0] = t->remote.ip6.as_u64[0];
370 ip0->dst_address.as_u64[1] = t->remote.ip6.as_u64[1];
372 /* UDP header, randomize src port on something, maybe? */
373 h0->udp.src_port = clib_host_to_net_u16 (4790);
374 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_VXLAN_GPE);
376 /* VXLAN header. Are we having fun yet? */
377 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
378 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
379 if (protocol_override)
381 h0->vxlan.protocol = t->protocol;
385 h0->vxlan.protocol = protocol_override;
387 t->rewrite_size = sizeof (ip4_vxlan_gpe_header_t) + extension_size;
388 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni << 8);
391 t->encap_next_node = encap_next_node;
396 vtep_addr_ref (ip46_address_t * ip)
398 uword *vtep = ip46_address_is_ip4 (ip) ?
399 hash_get (vxlan_gpe_main.vtep4, ip->ip4.as_u32) :
400 hash_get_mem (vxlan_gpe_main.vtep6, &ip->ip6);
403 ip46_address_is_ip4 (ip) ?
404 hash_set (vxlan_gpe_main.vtep4, ip->ip4.as_u32, 1) :
405 hash_set_mem_alloc (&vxlan_gpe_main.vtep6, &ip->ip6, 1);
410 vtep_addr_unref (ip46_address_t * ip)
412 uword *vtep = ip46_address_is_ip4 (ip) ?
413 hash_get (vxlan_gpe_main.vtep4, ip->ip4.as_u32) :
414 hash_get_mem (vxlan_gpe_main.vtep6, &ip->ip6);
418 ip46_address_is_ip4 (ip) ?
419 hash_unset (vxlan_gpe_main.vtep4, ip->ip4.as_u32) :
420 hash_unset_mem_free (&vxlan_gpe_main.vtep6, &ip->ip6);
425 typedef CLIB_PACKED(union {
427 fib_node_index_t mfib_entry_index;
428 adj_index_t mcast_adj_index;
434 static inline mcast_shared_t
435 mcast_shared_get (ip46_address_t * ip)
437 ASSERT (ip46_address_is_multicast (ip));
438 uword *p = hash_get_mem (vxlan_gpe_main.mcast_shared, ip);
440 return (mcast_shared_t)
446 mcast_shared_add (ip46_address_t * remote,
447 fib_node_index_t mfei, adj_index_t ai)
449 mcast_shared_t new_ep = {
450 .mcast_adj_index = ai,
451 .mfib_entry_index = mfei,
454 hash_set_mem_alloc (&vxlan_gpe_main.mcast_shared, remote, new_ep.as_u64);
458 mcast_shared_remove (ip46_address_t * remote)
460 mcast_shared_t ep = mcast_shared_get (remote);
462 adj_unlock (ep.mcast_adj_index);
463 mfib_table_entry_delete_index (ep.mfib_entry_index, MFIB_SOURCE_VXLAN_GPE);
465 hash_unset_mem_free (&vxlan_gpe_main.mcast_shared, remote);
468 static inline fib_protocol_t
469 fib_ip_proto (bool is_ip6)
471 return (is_ip6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
475 * @brief Add or Del a VXLAN GPE tunnel
478 * @param *sw_if_index
483 int vnet_vxlan_gpe_add_del_tunnel
484 (vnet_vxlan_gpe_add_del_tunnel_args_t * a, u32 * sw_if_indexp)
486 vxlan_gpe_main_t *ngm = &vxlan_gpe_main;
487 vxlan_gpe_tunnel_t *t = 0;
488 vnet_main_t *vnm = ngm->vnet_main;
489 vnet_hw_interface_t *hi;
491 u32 hw_if_index = ~0;
492 u32 sw_if_index = ~0;
494 vxlan4_gpe_tunnel_key_t key4, *key4_copy;
495 vxlan6_gpe_tunnel_key_t key6, *key6_copy;
496 u32 is_ip6 = a->is_ip6;
500 key4.local = a->local.ip4.as_u32;
501 key4.remote = a->remote.ip4.as_u32;
502 key4.vni = clib_host_to_net_u32 (a->vni << 8);
505 p = hash_get_mem (ngm->vxlan4_gpe_tunnel_by_key, &key4);
509 key6.local.as_u64[0] = a->local.ip6.as_u64[0];
510 key6.local.as_u64[1] = a->local.ip6.as_u64[1];
511 key6.remote.as_u64[0] = a->remote.ip6.as_u64[0];
512 key6.remote.as_u64[1] = a->remote.ip6.as_u64[1];
513 key6.vni = clib_host_to_net_u32 (a->vni << 8);
515 p = hash_get_mem (ngm->vxlan6_gpe_tunnel_by_key, &key6);
520 l2input_main_t *l2im = &l2input_main;
522 /* adding a tunnel: tunnel must not already exist */
524 return VNET_API_ERROR_TUNNEL_EXIST;
526 pool_get_aligned (ngm->tunnels, t, CLIB_CACHE_LINE_BYTES);
527 memset (t, 0, sizeof (*t));
529 /* copy from arg structure */
531 #define _(x) t->x = a->x;
532 foreach_gpe_copy_field;
541 t->flags |= VXLAN_GPE_TUNNEL_IS_IPV4;
545 rv = vxlan4_gpe_rewrite (t, 0, 0, VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP);
549 rv = vxlan6_gpe_rewrite (t, 0, 0, VXLAN_GPE_ENCAP_NEXT_IP6_LOOKUP);
554 pool_put (ngm->tunnels, t);
560 key4_copy = clib_mem_alloc (sizeof (*key4_copy));
561 clib_memcpy (key4_copy, &key4, sizeof (*key4_copy));
562 hash_set_mem (ngm->vxlan4_gpe_tunnel_by_key, key4_copy,
567 key6_copy = clib_mem_alloc (sizeof (*key6_copy));
568 clib_memcpy (key6_copy, &key6, sizeof (*key6_copy));
569 hash_set_mem (ngm->vxlan6_gpe_tunnel_by_key, key6_copy,
573 if (vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices) > 0)
575 vnet_interface_main_t *im = &vnm->interface_main;
576 hw_if_index = ngm->free_vxlan_gpe_tunnel_hw_if_indices
577 [vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices) - 1];
578 _vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices) -= 1;
580 hi = vnet_get_hw_interface (vnm, hw_if_index);
581 hi->dev_instance = t - ngm->tunnels;
582 hi->hw_instance = hi->dev_instance;
583 /* clear old stats of freed tunnel before reuse */
584 sw_if_index = hi->sw_if_index;
585 vnet_interface_counter_lock (im);
586 vlib_zero_combined_counter
587 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX],
589 vlib_zero_combined_counter (&im->combined_sw_if_counters
590 [VNET_INTERFACE_COUNTER_RX],
592 vlib_zero_simple_counter (&im->sw_if_counters
593 [VNET_INTERFACE_COUNTER_DROP],
595 vnet_interface_counter_unlock (im);
599 hw_if_index = vnet_register_interface
600 (vnm, vxlan_gpe_device_class.index, t - ngm->tunnels,
601 vxlan_gpe_hw_class.index, t - ngm->tunnels);
602 hi = vnet_get_hw_interface (vnm, hw_if_index);
603 hi->output_node_index = vxlan_gpe_encap_node.index;
606 t->hw_if_index = hw_if_index;
607 t->sw_if_index = sw_if_index = hi->sw_if_index;
608 vec_validate_init_empty (ngm->tunnel_index_by_sw_if_index, sw_if_index,
610 ngm->tunnel_index_by_sw_if_index[sw_if_index] = t - ngm->tunnels;
612 /* setup l2 input config with l2 feature and bd 0 to drop packet */
613 vec_validate (l2im->configs, sw_if_index);
614 l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
615 l2im->configs[sw_if_index].bd_index = 0;
617 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
618 si->flags &= ~VNET_SW_INTERFACE_FLAG_HIDDEN;
619 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
620 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
621 fib_node_init (&t->node, FIB_NODE_TYPE_VXLAN_GPE_TUNNEL);
622 fib_prefix_t tun_remote_pfx;
623 u32 encap_index = vxlan_gpe_encap_node.index;
624 vnet_flood_class_t flood_class = VNET_FLOOD_CLASS_TUNNEL_NORMAL;
626 fib_prefix_from_ip46_addr (&t->remote, &tun_remote_pfx);
627 if (!ip46_address_is_multicast (&t->remote))
630 * source the FIB entry for the tunnel's destination
631 * and become a child thereof. The tunnel will then get poked
632 * when the forwarding for the entry updates, and the tunnel can
633 * re-stack accordingly
635 vtep_addr_ref (&t->local);
636 t->fib_entry_index = fib_table_entry_special_add
637 (t->encap_fib_index, &tun_remote_pfx, FIB_SOURCE_RR,
638 FIB_ENTRY_FLAG_NONE);
639 t->sibling_index = fib_entry_child_add
640 (t->fib_entry_index, FIB_NODE_TYPE_VXLAN_GPE_TUNNEL,
642 vxlan_gpe_tunnel_restack_dpo (t);
646 /* Multicast tunnel -
647 * as the same mcast group can be used for mutiple mcast tunnels
648 * with different VNIs, create the output fib adjecency only if
649 * it does not already exist
651 fib_protocol_t fp = fib_ip_proto (is_ip6);
653 if (vtep_addr_ref (&t->remote) == 1)
655 fib_node_index_t mfei;
657 fib_route_path_t path = {
658 .frp_proto = fib_proto_to_dpo (fp),
659 .frp_addr = zero_addr,
660 .frp_sw_if_index = 0xffffffff,
663 .frp_flags = FIB_ROUTE_PATH_LOCAL,
665 const mfib_prefix_t mpfx = {
667 .fp_len = (is_ip6 ? 128 : 32),
668 .fp_grp_addr = tun_remote_pfx.fp_addr,
672 * Setup the (*,G) to receive traffic on the mcast group
673 * - the forwarding interface is for-us
674 * - the accepting interface is that from the API
676 mfib_table_entry_path_update (t->encap_fib_index,
678 MFIB_SOURCE_VXLAN_GPE,
679 &path, MFIB_ITF_FLAG_FORWARD);
681 path.frp_sw_if_index = a->mcast_sw_if_index;
682 path.frp_flags = FIB_ROUTE_PATH_FLAG_NONE;
683 mfei = mfib_table_entry_path_update (t->encap_fib_index,
685 MFIB_SOURCE_VXLAN_GPE,
687 MFIB_ITF_FLAG_ACCEPT);
690 * Create the mcast adjacency to send traffic to the group
692 ai = adj_mcast_add_or_lock (fp,
693 fib_proto_to_link (fp),
694 a->mcast_sw_if_index);
697 * create a new end-point
699 mcast_shared_add (&t->remote, mfei, ai);
702 dpo_id_t dpo = DPO_INVALID;
703 mcast_shared_t ep = mcast_shared_get (&t->remote);
705 /* Stack shared mcast remote mac addr rewrite on encap */
706 dpo_set (&dpo, DPO_ADJACENCY_MCAST,
707 fib_proto_to_dpo (fp), ep.mcast_adj_index);
709 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
711 flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER;
714 /* Set vxlan tunnel output node */
715 hi->output_node_index = encap_index;
717 vnet_get_sw_interface (vnet_get_main (), sw_if_index)->flood_class =
722 /* deleting a tunnel: tunnel must exist */
724 return VNET_API_ERROR_NO_SUCH_ENTRY;
726 t = pool_elt_at_index (ngm->tunnels, p[0]);
728 sw_if_index = t->sw_if_index;
729 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */ );
730 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, t->sw_if_index);
731 si->flags |= VNET_SW_INTERFACE_FLAG_HIDDEN;
732 set_int_l2_mode (ngm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0,
734 vec_add1 (ngm->free_vxlan_gpe_tunnel_hw_if_indices, t->hw_if_index);
736 ngm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
739 hash_unset (ngm->vxlan4_gpe_tunnel_by_key, key4.as_u64);
741 hash_unset_mem_free (&ngm->vxlan6_gpe_tunnel_by_key, &key6);
743 if (!ip46_address_is_multicast (&t->remote))
745 vtep_addr_unref (&t->local);
746 fib_entry_child_remove (t->fib_entry_index, t->sibling_index);
747 fib_table_entry_delete_index (t->fib_entry_index, FIB_SOURCE_RR);
749 else if (vtep_addr_unref (&t->remote) == 0)
751 mcast_shared_remove (&t->remote);
754 fib_node_deinit (&t->node);
755 vec_free (t->rewrite);
756 pool_put (ngm->tunnels, t);
760 *sw_if_indexp = sw_if_index;
765 static clib_error_t *
766 vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm,
767 unformat_input_t * input,
768 vlib_cli_command_t * cmd)
770 unformat_input_t _line_input, *line_input = &_line_input;
772 ip46_address_t local, remote;
778 u32 mcast_sw_if_index = ~0;
779 u32 encap_fib_index = 0;
780 u32 decap_fib_index = 0;
781 u8 protocol = VXLAN_GPE_PROTOCOL_IP4;
786 vnet_vxlan_gpe_add_del_tunnel_args_t _a, *a = &_a;
788 clib_error_t *error = NULL;
790 /* Get a line of input. */
791 if (!unformat_user (input, unformat_line_input, line_input))
794 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
796 if (unformat (line_input, "del"))
798 else if (unformat (line_input, "local %U",
799 unformat_ip4_address, &local.ip4))
804 else if (unformat (line_input, "remote %U",
805 unformat_ip4_address, &remote.ip4))
810 else if (unformat (line_input, "local %U",
811 unformat_ip6_address, &local.ip6))
816 else if (unformat (line_input, "remote %U",
817 unformat_ip6_address, &remote.ip6))
822 else if (unformat (line_input, "group %U %U",
823 unformat_ip4_address, &remote.ip4,
824 unformat_vnet_sw_interface,
825 vnet_get_main (), &mcast_sw_if_index))
827 grp_set = remote_set = 1;
830 else if (unformat (line_input, "group %U %U",
831 unformat_ip6_address, &remote.ip6,
832 unformat_vnet_sw_interface,
833 vnet_get_main (), &mcast_sw_if_index))
835 grp_set = remote_set = 1;
838 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
841 encap_fib_index = fib_table_find (FIB_PROTOCOL_IP6, tmp);
843 encap_fib_index = fib_table_find (FIB_PROTOCOL_IP4, tmp);
845 if (encap_fib_index == ~0)
848 clib_error_return (0, "nonexistent encap fib id %d", tmp);
852 else if (unformat (line_input, "decap-vrf-id %d", &tmp))
855 decap_fib_index = fib_table_find (FIB_PROTOCOL_IP6, tmp);
857 decap_fib_index = fib_table_find (FIB_PROTOCOL_IP4, tmp);
859 if (decap_fib_index == ~0)
862 clib_error_return (0, "nonexistent decap fib id %d", tmp);
866 else if (unformat (line_input, "vni %d", &vni))
868 else if (unformat (line_input, "next-ip4"))
869 protocol = VXLAN_GPE_PROTOCOL_IP4;
870 else if (unformat (line_input, "next-ip6"))
871 protocol = VXLAN_GPE_PROTOCOL_IP6;
872 else if (unformat (line_input, "next-ethernet"))
873 protocol = VXLAN_GPE_PROTOCOL_ETHERNET;
874 else if (unformat (line_input, "next-nsh"))
875 protocol = VXLAN_GPE_PROTOCOL_NSH;
878 error = clib_error_return (0, "parse error: '%U'",
879 format_unformat_error, line_input);
886 error = clib_error_return (0, "tunnel local address not specified");
892 error = clib_error_return (0, "tunnel remote address not specified");
896 if (grp_set && !ip46_address_is_multicast (&remote))
898 error = clib_error_return (0, "tunnel group address not multicast");
902 if (grp_set == 0 && ip46_address_is_multicast (&remote))
904 error = clib_error_return (0, "remote address must be unicast");
908 if (grp_set && mcast_sw_if_index == ~0)
910 error = clib_error_return (0, "tunnel nonexistent multicast device");
913 if (ipv4_set && ipv6_set)
915 error = clib_error_return (0, "both IPv4 and IPv6 addresses specified");
919 if ((ipv4_set && memcmp (&local.ip4, &remote.ip4, sizeof (local.ip4)) == 0)
921 && memcmp (&local.ip6, &remote.ip6, sizeof (local.ip6)) == 0))
923 error = clib_error_return (0, "src and remote addresses are identical");
929 error = clib_error_return (0, "vni not specified");
933 memset (a, 0, sizeof (*a));
936 a->is_ip6 = ipv6_set;
939 #define _(x) a->x = x;
940 foreach_gpe_copy_field;
948 rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
953 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
954 vnet_get_main (), sw_if_index);
956 case VNET_API_ERROR_INVALID_DECAP_NEXT:
957 error = clib_error_return (0, "invalid decap-next...");
960 case VNET_API_ERROR_TUNNEL_EXIST:
961 error = clib_error_return (0, "tunnel already exists...");
964 case VNET_API_ERROR_NO_SUCH_ENTRY:
965 error = clib_error_return (0, "tunnel does not exist...");
969 error = clib_error_return
970 (0, "vnet_vxlan_gpe_add_del_tunnel returned %d", rv);
975 unformat_free (line_input);
981 * Add or delete a VXLAN-GPE Tunnel.
983 * VXLAN-GPE provides the features needed to allow L2 bridge domains (BDs)
984 * to span multiple servers. This is done by building an L2 overlay on
985 * top of an L3 network underlay using VXLAN-GPE tunnels.
987 * This makes it possible for servers to be co-located in the same data
988 * center or be separated geographically as long as they are reachable
989 * through the underlay L3 network.
991 * You can refer to this kind of L2 overlay bridge domain as a VXLAN-GPE sengment.
994 * Example of how to create a VXLAN-GPE Tunnel:
995 * @cliexcmd{create vxlan-gpe tunnel local 10.0.3.1 local 10.0.3.3 vni 13 encap-vrf-id 7}
996 * Example of how to delete a VXLAN Tunnel:
997 * @cliexcmd{create vxlan tunnel src 10.0.3.1 remote 10.0.3.3 vni 13 del}
1000 VLIB_CLI_COMMAND (create_vxlan_gpe_tunnel_command, static) = {
1001 .path = "create vxlan-gpe tunnel",
1003 "create vxlan-gpe tunnel local <local-addr> "
1004 " {remote <remote-addr>|group <mcast-addr> <intf-name>}"
1005 " vni <nn> [next-ip4][next-ip6][next-ethernet][next-nsh]"
1006 " [encap-vrf-id <nn>] [decap-vrf-id <nn>] [del]\n",
1007 .function = vxlan_gpe_add_del_tunnel_command_fn,
1012 * @brief CLI function for showing VXLAN GPE tunnels
1021 static clib_error_t *
1022 show_vxlan_gpe_tunnel_command_fn (vlib_main_t * vm,
1023 unformat_input_t * input,
1024 vlib_cli_command_t * cmd)
1026 vxlan_gpe_main_t *ngm = &vxlan_gpe_main;
1027 vxlan_gpe_tunnel_t *t;
1029 if (pool_elts (ngm->tunnels) == 0)
1030 vlib_cli_output (vm, "No vxlan-gpe tunnels configured.");
1033 pool_foreach (t, ngm->tunnels,
1035 vlib_cli_output (vm, "%U", format_vxlan_gpe_tunnel, t);
1043 * Display all the VXLAN-GPE Tunnel entries.
1046 * Example of how to display the VXLAN-GPE Tunnel entries:
1047 * @cliexstart{show vxlan-gpe tunnel}
1048 * [0] local 10.0.3.1 remote 10.0.3.3 vni 13 encap_fib_index 0 sw_if_index 5 decap_next l2
1052 VLIB_CLI_COMMAND (show_vxlan_gpe_tunnel_command, static) = {
1053 .path = "show vxlan-gpe",
1054 .function = show_vxlan_gpe_tunnel_command_fn,
1059 vnet_int_vxlan_gpe_bypass_mode (u32 sw_if_index, u8 is_ip6, u8 is_enable)
1062 vnet_feature_enable_disable ("ip6-unicast", "ip6-vxlan-gpe-bypass",
1063 sw_if_index, is_enable, 0, 0);
1065 vnet_feature_enable_disable ("ip4-unicast", "ip4-vxlan-gpe-bypass",
1066 sw_if_index, is_enable, 0, 0);
1070 static clib_error_t *
1071 set_ip_vxlan_gpe_bypass (u32 is_ip6,
1072 unformat_input_t * input, vlib_cli_command_t * cmd)
1074 unformat_input_t _line_input, *line_input = &_line_input;
1075 vnet_main_t *vnm = vnet_get_main ();
1076 clib_error_t *error = 0;
1077 u32 sw_if_index, is_enable;
1082 if (!unformat_user (input, unformat_line_input, line_input))
1085 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1088 (line_input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1090 else if (unformat (line_input, "del"))
1094 error = unformat_parse_error (line_input);
1099 if (~0 == sw_if_index)
1101 error = clib_error_return (0, "unknown interface `%U'",
1102 format_unformat_error, line_input);
1106 vnet_int_vxlan_gpe_bypass_mode (sw_if_index, is_ip6, is_enable);
1109 unformat_free (line_input);
1114 static clib_error_t *
1115 set_ip4_vxlan_gpe_bypass (vlib_main_t * vm,
1116 unformat_input_t * input, vlib_cli_command_t * cmd)
1118 return set_ip_vxlan_gpe_bypass (0, input, cmd);
1122 * This command adds the 'ip4-vxlan-gpe-bypass' graph node for a given interface.
1123 * By adding the IPv4 vxlan-gpe-bypass graph node to an interface, the node checks
1124 * for and validate input vxlan_gpe packet and bypass ip4-lookup, ip4-local,
1125 * ip4-udp-lookup nodes to speedup vxlan_gpe packet forwarding. This node will
1126 * cause extra overhead to for non-vxlan_gpe packets which is kept at a minimum.
1130 * Example of graph node before ip4-vxlan-gpe-bypass is enabled:
1131 * @cliexstart{show vlib graph ip4-vxlan-gpe-bypass}
1132 * Name Next Previous
1133 * ip4-vxlan-gpe-bypass error-drop [0]
1134 * vxlan4-gpe-input [1]
1138 * Example of how to enable ip4-vxlan-gpe-bypass on an interface:
1139 * @cliexcmd{set interface ip vxlan-gpe-bypass GigabitEthernet2/0/0}
1141 * Example of graph node after ip4-vxlan-gpe-bypass is enabled:
1142 * @cliexstart{show vlib graph ip4-vxlan-gpe-bypass}
1143 * Name Next Previous
1144 * ip4-vxlan-gpe-bypass error-drop [0] ip4-input
1145 * vxlan4-gpe-input [1] ip4-input-no-checksum
1149 * Example of how to display the feature enabed on an interface:
1150 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1151 * IP feature paths configured on GigabitEthernet2/0/0...
1154 * ip4-vxlan-gpe-bypass
1159 * Example of how to disable ip4-vxlan-gpe-bypass on an interface:
1160 * @cliexcmd{set interface ip vxlan-gpe-bypass GigabitEthernet2/0/0 del}
1164 VLIB_CLI_COMMAND (set_interface_ip_vxlan_gpe_bypass_command, static) = {
1165 .path = "set interface ip vxlan-gpe-bypass",
1166 .function = set_ip4_vxlan_gpe_bypass,
1167 .short_help = "set interface ip vxlan-gpe-bypass <interface> [del]",
1171 static clib_error_t *
1172 set_ip6_vxlan_gpe_bypass (vlib_main_t * vm,
1173 unformat_input_t * input, vlib_cli_command_t * cmd)
1175 return set_ip_vxlan_gpe_bypass (1, input, cmd);
1179 * This command adds the 'ip6-vxlan-gpe-bypass' graph node for a given interface.
1180 * By adding the IPv6 vxlan-gpe-bypass graph node to an interface, the node checks
1181 * for and validate input vxlan_gpe packet and bypass ip6-lookup, ip6-local,
1182 * ip6-udp-lookup nodes to speedup vxlan_gpe packet forwarding. This node will
1183 * cause extra overhead to for non-vxlan_gpe packets which is kept at a minimum.
1187 * Example of graph node before ip6-vxlan-gpe-bypass is enabled:
1188 * @cliexstart{show vlib graph ip6-vxlan-gpe-bypass}
1189 * Name Next Previous
1190 * ip6-vxlan-gpe-bypass error-drop [0]
1191 * vxlan6-gpe-input [1]
1195 * Example of how to enable ip6-vxlan-gpe-bypass on an interface:
1196 * @cliexcmd{set interface ip6 vxlan-gpe-bypass GigabitEthernet2/0/0}
1198 * Example of graph node after ip6-vxlan-gpe-bypass is enabled:
1199 * @cliexstart{show vlib graph ip6-vxlan-gpe-bypass}
1200 * Name Next Previous
1201 * ip6-vxlan-gpe-bypass error-drop [0] ip6-input
1202 * vxlan6-gpe-input [1] ip4-input-no-checksum
1206 * Example of how to display the feature enabed on an interface:
1207 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1208 * IP feature paths configured on GigabitEthernet2/0/0...
1211 * ip6-vxlan-gpe-bypass
1216 * Example of how to disable ip6-vxlan-gpe-bypass on an interface:
1217 * @cliexcmd{set interface ip6 vxlan-gpe-bypass GigabitEthernet2/0/0 del}
1221 VLIB_CLI_COMMAND (set_interface_ip6_vxlan_gpe_bypass_command, static) = {
1222 .path = "set interface ip6 vxlan-gpe-bypass",
1223 .function = set_ip6_vxlan_gpe_bypass,
1224 .short_help = "set interface ip vxlan-gpe-bypass <interface> [del]",
1229 VNET_FEATURE_INIT (ip4_vxlan_gpe_bypass, static) =
1231 .arc_name = "ip4-unicast",
1232 .node_name = "ip4-vxlan-gpe-bypass",
1233 .runs_before = VNET_FEATURES ("ip4-lookup"),
1236 VNET_FEATURE_INIT (ip6_vxlan_gpe_bypass, static) =
1238 .arc_name = "ip6-unicast",
1239 .node_name = "ip6-vxlan-gpe-bypass",
1240 .runs_before = VNET_FEATURES ("ip6-lookup"),
1245 * @brief Feature init function for VXLAN GPE
1253 vxlan_gpe_init (vlib_main_t * vm)
1255 vxlan_gpe_main_t *ngm = &vxlan_gpe_main;
1257 ngm->vnet_main = vnet_get_main ();
1258 ngm->vlib_main = vm;
1260 ngm->vxlan4_gpe_tunnel_by_key
1261 = hash_create_mem (0, sizeof (vxlan4_gpe_tunnel_key_t), sizeof (uword));
1263 ngm->vxlan6_gpe_tunnel_by_key
1264 = hash_create_mem (0, sizeof (vxlan6_gpe_tunnel_key_t), sizeof (uword));
1267 ngm->mcast_shared = hash_create_mem (0,
1268 sizeof (ip46_address_t),
1269 sizeof (mcast_shared_t));
1270 ngm->vtep6 = hash_create_mem (0, sizeof (ip6_address_t), sizeof (uword));
1272 udp_register_dst_port (vm, UDP_DST_PORT_VXLAN_GPE,
1273 vxlan4_gpe_input_node.index, 1 /* is_ip4 */ );
1274 udp_register_dst_port (vm, UDP_DST_PORT_VXLAN6_GPE,
1275 vxlan6_gpe_input_node.index, 0 /* is_ip4 */ );
1277 /* Register the list of standard decap protocols supported */
1278 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_IP4,
1279 VXLAN_GPE_INPUT_NEXT_IP4_INPUT);
1280 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_IP6,
1281 VXLAN_GPE_INPUT_NEXT_IP6_INPUT);
1282 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_ETHERNET,
1283 VXLAN_GPE_INPUT_NEXT_L2_INPUT);
1285 fib_node_register_type (FIB_NODE_TYPE_VXLAN_GPE_TUNNEL, &vxlan_gpe_vft);
1290 VLIB_INIT_FUNCTION (vxlan_gpe_init);
1294 * fd.io coding-style-patch-verification: ON
1297 * eval: (c-set-style "gnu")