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 * @brief Tracing function for VXLAN GPE tunnel packets
50 * @param *s formatting string
53 * @return *s formatted string
56 u8 * format_vxlan_gpe_tunnel (u8 * s, va_list * args)
58 vxlan_gpe_tunnel_t * t = va_arg (*args, vxlan_gpe_tunnel_t *);
59 vxlan_gpe_main_t * ngm = &vxlan_gpe_main;
61 s = format (s, "[%d] local: %U remote: %U ",
63 format_ip46_address, &t->local, IP46_TYPE_ANY,
64 format_ip46_address, &t->remote, IP46_TYPE_ANY);
66 s = format (s, " vxlan VNI %d ", t->vni);
70 case VXLAN_GPE_PROTOCOL_IP4:
71 s = format (s, "next-protocol ip4");
73 case VXLAN_GPE_PROTOCOL_IP6:
74 s = format (s, "next-protocol ip6");
76 case VXLAN_GPE_PROTOCOL_ETHERNET:
77 s = format (s, "next-protocol ethernet");
79 case VXLAN_GPE_PROTOCOL_NSH:
80 s = format (s, "next-protocol nsh");
83 s = format (s, "next-protocol unknown %d", t->protocol);
86 if (ip46_address_is_multicast (&t->remote))
87 s = format (s, "mcast_sw_if_index %d ", t->mcast_sw_if_index);
89 s = format (s, " fibs: (encap %d, decap %d)",
97 * @brief Naming for VXLAN GPE tunnel
99 * @param *s formatting string
102 * @return *s formatted string
105 static u8 * format_vxlan_gpe_name (u8 * s, va_list * args)
107 u32 dev_instance = va_arg (*args, u32);
108 return format (s, "vxlan_gpe_tunnel%d", dev_instance);
111 static uword dummy_interface_tx (vlib_main_t * vm,
112 vlib_node_runtime_t * node,
113 vlib_frame_t * frame)
115 clib_warning ("you shouldn't be here, leaking buffers...");
116 return frame->n_vectors;
120 * @brief CLI function for VXLAN GPE admin up/down
129 static clib_error_t *
130 vxlan_gpe_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
132 u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
133 VNET_HW_INTERFACE_FLAG_LINK_UP : 0;
134 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
139 VNET_DEVICE_CLASS (vxlan_gpe_device_class,static) = {
141 .format_device_name = format_vxlan_gpe_name,
142 .format_tx_trace = format_vxlan_gpe_encap_trace,
143 .tx_function = dummy_interface_tx,
144 .admin_up_down_function = vxlan_gpe_interface_admin_up_down,
149 * @brief Formatting function for tracing VXLAN GPE with length
157 static u8 * format_vxlan_gpe_header_with_length (u8 * s, va_list * args)
159 u32 dev_instance = va_arg (*args, u32);
160 s = format (s, "unimplemented dev %u", dev_instance);
164 VNET_HW_INTERFACE_CLASS (vxlan_gpe_hw_class) = {
166 .format_header = format_vxlan_gpe_header_with_length,
167 .build_rewrite = default_build_rewrite,
171 vxlan_gpe_tunnel_restack_dpo(vxlan_gpe_tunnel_t * t)
173 dpo_id_t dpo = DPO_INVALID;
174 u32 encap_index = vxlan_gpe_encap_node.index;
175 fib_forward_chain_type_t forw_type = ip46_address_is_ip4(&t->remote) ?
176 FIB_FORW_CHAIN_TYPE_UNICAST_IP4 : FIB_FORW_CHAIN_TYPE_UNICAST_IP6;
178 fib_entry_contribute_forwarding (t->fib_entry_index, forw_type, &dpo);
179 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
183 static vxlan_gpe_tunnel_t *
184 vxlan_gpe_tunnel_from_fib_node (fib_node_t *node)
187 ASSERT(FIB_NODE_TYPE_VXLAN_GPE_TUNNEL == node->fn_type);
189 return ((vxlan_gpe_tunnel_t*) (((char*)node) -
190 STRUCT_OFFSET_OF(vxlan_gpe_tunnel_t, node)));
194 * Function definition to backwalk a FIB node -
195 * Here we will restack the new dpo of VXLAN_GPE DIP to encap node.
197 static fib_node_back_walk_rc_t
198 vxlan_gpe_tunnel_back_walk (fib_node_t *node,
199 fib_node_back_walk_ctx_t *ctx)
201 vxlan_gpe_tunnel_restack_dpo(vxlan_gpe_tunnel_from_fib_node(node));
202 return (FIB_NODE_BACK_WALK_CONTINUE);
206 * Function definition to get a FIB node from its index
209 vxlan_gpe_tunnel_fib_node_get (fib_node_index_t index)
211 vxlan_gpe_tunnel_t * t;
212 vxlan_gpe_main_t * ngm = &vxlan_gpe_main;
214 t = pool_elt_at_index(ngm->tunnels, index);
220 * Function definition to inform the FIB node that its last lock has gone.
223 vxlan_gpe_tunnel_last_lock_gone (fib_node_t *node)
226 * The VXLAN_GPE tunnel is a root of the graph. As such
227 * it never has children and thus is never locked.
233 * Virtual function table registered by VXLAN_GPE tunnels
234 * for participation in the FIB object graph.
236 const static fib_node_vft_t vxlan_gpe_vft = {
237 .fnv_get = vxlan_gpe_tunnel_fib_node_get,
238 .fnv_last_lock = vxlan_gpe_tunnel_last_lock_gone,
239 .fnv_back_walk = vxlan_gpe_tunnel_back_walk,
242 #define foreach_gpe_copy_field \
245 _(mcast_sw_if_index) \
249 #define foreach_copy_ipv4 { \
250 _(local.ip4.as_u32) \
251 _(remote.ip4.as_u32) \
254 #define foreach_copy_ipv6 { \
255 _(local.ip6.as_u64[0]) \
256 _(local.ip6.as_u64[1]) \
257 _(remote.ip6.as_u64[0]) \
258 _(remote.ip6.as_u64[1]) \
263 * @brief Calculate IPv4 VXLAN GPE rewrite header
270 int vxlan4_gpe_rewrite (vxlan_gpe_tunnel_t * t, u32 extension_size,
271 u8 protocol_override, uword encap_next_node)
275 ip4_vxlan_gpe_header_t * h0;
278 len = sizeof (*h0) + extension_size;
280 vec_free(t->rewrite);
281 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
283 h0 = (ip4_vxlan_gpe_header_t *) rw;
285 /* Fixed portion of the (outer) ip4 header */
287 ip0->ip_version_and_header_length = 0x45;
289 ip0->protocol = IP_PROTOCOL_UDP;
291 /* we fix up the ip4 header length and checksum after-the-fact */
292 ip0->src_address.as_u32 = t->local.ip4.as_u32;
293 ip0->dst_address.as_u32 = t->remote.ip4.as_u32;
294 ip0->checksum = ip4_header_checksum (ip0);
296 /* UDP header, randomize src port on something, maybe? */
297 h0->udp.src_port = clib_host_to_net_u16 (4790);
298 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_VXLAN_GPE);
300 /* VXLAN header. Are we having fun yet? */
301 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
302 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
303 if (protocol_override)
305 h0->vxlan.protocol = protocol_override;
309 h0->vxlan.protocol = t->protocol;
311 t->rewrite_size = sizeof(ip4_vxlan_gpe_header_t) + extension_size;
312 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
315 t->encap_next_node = encap_next_node;
320 * @brief Calculate IPv6 VXLAN GPE rewrite header
327 int vxlan6_gpe_rewrite (vxlan_gpe_tunnel_t * t, u32 extension_size,
328 u8 protocol_override, uword encap_next_node)
332 ip6_vxlan_gpe_header_t * h0;
335 len = sizeof (*h0) + extension_size;
337 vec_free(t->rewrite);
338 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
340 h0 = (ip6_vxlan_gpe_header_t *) rw;
342 /* Fixed portion of the (outer) ip4 header */
344 ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32(6 << 28);
345 ip0->hop_limit = 255;
346 ip0->protocol = IP_PROTOCOL_UDP;
348 ip0->src_address.as_u64[0] = t->local.ip6.as_u64[0];
349 ip0->src_address.as_u64[1] = t->local.ip6.as_u64[1];
350 ip0->dst_address.as_u64[0] = t->remote.ip6.as_u64[0];
351 ip0->dst_address.as_u64[1] = t->remote.ip6.as_u64[1];
353 /* UDP header, randomize src port on something, maybe? */
354 h0->udp.src_port = clib_host_to_net_u16 (4790);
355 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_VXLAN_GPE);
357 /* VXLAN header. Are we having fun yet? */
358 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
359 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
360 if (protocol_override)
362 h0->vxlan.protocol = t->protocol;
366 h0->vxlan.protocol = protocol_override;
368 t->rewrite_size = sizeof(ip4_vxlan_gpe_header_t) + extension_size;
369 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
372 t->encap_next_node = encap_next_node;
377 hash_set_key_copy (uword ** h, void * key, uword v) {
378 size_t ksz = hash_header(*h)->user;
379 void * copy = clib_mem_alloc (ksz);
380 clib_memcpy (copy, key, ksz);
381 hash_set_mem (*h, copy, v);
385 hash_unset_key_free (uword ** h, void * key) {
386 hash_pair_t * hp = hash_get_pair_mem (*h, key);
388 key = uword_to_pointer (hp->key, void *);
389 hash_unset_mem (*h, key);
394 vtep_addr_ref(ip46_address_t *ip)
396 uword *vtep = ip46_address_is_ip4(ip) ?
397 hash_get (vxlan_gpe_main.vtep4, ip->ip4.as_u32) :
398 hash_get_mem (vxlan_gpe_main.vtep6, &ip->ip6);
401 ip46_address_is_ip4(ip) ?
402 hash_set (vxlan_gpe_main.vtep4, ip->ip4.as_u32, 1) :
403 hash_set_key_copy (&vxlan_gpe_main.vtep6, &ip->ip6, 1);
408 vtep_addr_unref(ip46_address_t *ip)
410 uword *vtep = ip46_address_is_ip4(ip) ?
411 hash_get (vxlan_gpe_main.vtep4, ip->ip4.as_u32) :
412 hash_get_mem (vxlan_gpe_main.vtep6, &ip->ip6);
416 ip46_address_is_ip4(ip) ?
417 hash_unset (vxlan_gpe_main.vtep4, ip->ip4.as_u32) :
418 hash_unset_key_free (&vxlan_gpe_main.vtep6, &ip->ip6);
422 typedef CLIB_PACKED(union {
424 fib_node_index_t mfib_entry_index;
425 adj_index_t mcast_adj_index;
430 static inline mcast_shared_t
431 mcast_shared_get(ip46_address_t * ip)
433 ASSERT(ip46_address_is_multicast(ip));
434 uword * p = hash_get_mem (vxlan_gpe_main.mcast_shared, ip);
436 return (mcast_shared_t) { .as_u64 = *p };
440 mcast_shared_add(ip46_address_t *remote,
441 fib_node_index_t mfei,
444 mcast_shared_t new_ep = {
445 .mcast_adj_index = ai,
446 .mfib_entry_index = mfei,
449 hash_set_key_copy (&vxlan_gpe_main.mcast_shared, remote, new_ep.as_u64);
453 mcast_shared_remove(ip46_address_t *remote)
455 mcast_shared_t ep = mcast_shared_get(remote);
457 adj_unlock(ep.mcast_adj_index);
458 mfib_table_entry_delete_index(ep.mfib_entry_index,
459 MFIB_SOURCE_VXLAN_GPE);
461 hash_unset_key_free (&vxlan_gpe_main.mcast_shared, remote);
464 static inline fib_protocol_t
465 fib_ip_proto(bool is_ip6)
467 return (is_ip6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
471 * @brief Add or Del a VXLAN GPE tunnel
474 * @param *sw_if_index
479 int vnet_vxlan_gpe_add_del_tunnel
480 (vnet_vxlan_gpe_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
482 vxlan_gpe_main_t * ngm = &vxlan_gpe_main;
483 vxlan_gpe_tunnel_t *t = 0;
484 vnet_main_t * vnm = ngm->vnet_main;
485 vnet_hw_interface_t * hi;
487 u32 hw_if_index = ~0;
488 u32 sw_if_index = ~0;
490 vxlan4_gpe_tunnel_key_t key4, *key4_copy;
491 vxlan6_gpe_tunnel_key_t key6, *key6_copy;
492 u32 is_ip6 = a->is_ip6;
496 key4.local = a->local.ip4.as_u32;
497 key4.remote = a->remote.ip4.as_u32;
498 key4.vni = clib_host_to_net_u32 (a->vni << 8);
501 p = hash_get_mem(ngm->vxlan4_gpe_tunnel_by_key, &key4);
505 key6.local.as_u64[0] = a->local.ip6.as_u64[0];
506 key6.local.as_u64[1] = a->local.ip6.as_u64[1];
507 key6.remote.as_u64[0] = a->remote.ip6.as_u64[0];
508 key6.remote.as_u64[1] = a->remote.ip6.as_u64[1];
509 key6.vni = clib_host_to_net_u32 (a->vni << 8);
511 p = hash_get_mem(ngm->vxlan6_gpe_tunnel_by_key, &key6);
516 l2input_main_t * l2im = &l2input_main;
518 /* adding a tunnel: tunnel must not already exist */
520 return VNET_API_ERROR_TUNNEL_EXIST;
522 pool_get_aligned (ngm->tunnels, t, CLIB_CACHE_LINE_BYTES);
523 memset (t, 0, sizeof (*t));
525 /* copy from arg structure */
526 #define _(x) t->x = a->x;
527 foreach_gpe_copy_field;
528 if (!a->is_ip6) foreach_copy_ipv4
529 else foreach_copy_ipv6
532 if (!a->is_ip6) t->flags |= VXLAN_GPE_TUNNEL_IS_IPV4;
535 rv = vxlan4_gpe_rewrite (t, 0, 0, VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP);
537 rv = vxlan6_gpe_rewrite (t, 0, 0, VXLAN_GPE_ENCAP_NEXT_IP6_LOOKUP);
542 pool_put (ngm->tunnels, t);
548 key4_copy = clib_mem_alloc (sizeof (*key4_copy));
549 clib_memcpy (key4_copy, &key4, sizeof (*key4_copy));
550 hash_set_mem (ngm->vxlan4_gpe_tunnel_by_key, key4_copy,
555 key6_copy = clib_mem_alloc (sizeof (*key6_copy));
556 clib_memcpy (key6_copy, &key6, sizeof (*key6_copy));
557 hash_set_mem (ngm->vxlan6_gpe_tunnel_by_key, key6_copy,
561 if (vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices) > 0)
563 vnet_interface_main_t * im = &vnm->interface_main;
564 hw_if_index = ngm->free_vxlan_gpe_tunnel_hw_if_indices
565 [vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices)-1];
566 _vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices) -= 1;
568 hi = vnet_get_hw_interface (vnm, hw_if_index);
569 hi->dev_instance = t - ngm->tunnels;
570 hi->hw_instance = hi->dev_instance;
571 /* clear old stats of freed tunnel before reuse */
572 sw_if_index = hi->sw_if_index;
573 vnet_interface_counter_lock(im);
574 vlib_zero_combined_counter
575 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
576 vlib_zero_combined_counter
577 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
578 vlib_zero_simple_counter
579 (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
580 vnet_interface_counter_unlock(im);
584 hw_if_index = vnet_register_interface
585 (vnm, vxlan_gpe_device_class.index, t - ngm->tunnels,
586 vxlan_gpe_hw_class.index, t - ngm->tunnels);
587 hi = vnet_get_hw_interface (vnm, hw_if_index);
588 hi->output_node_index = vxlan_gpe_encap_node.index;
591 t->hw_if_index = hw_if_index;
592 t->sw_if_index = sw_if_index = hi->sw_if_index;
593 vec_validate_init_empty (ngm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
594 ngm->tunnel_index_by_sw_if_index[sw_if_index] = t - ngm->tunnels;
596 /* setup l2 input config with l2 feature and bd 0 to drop packet */
597 vec_validate (l2im->configs, sw_if_index);
598 l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
599 l2im->configs[sw_if_index].bd_index = 0;
601 vnet_sw_interface_t * si = vnet_get_sw_interface (vnm, sw_if_index);
602 si->flags &= ~VNET_SW_INTERFACE_FLAG_HIDDEN;
603 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
604 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
605 fib_node_init(&t->node, FIB_NODE_TYPE_VXLAN_GPE_TUNNEL);
606 fib_prefix_t tun_remote_pfx;
607 u32 encap_index = vxlan_gpe_encap_node.index;
608 vnet_flood_class_t flood_class = VNET_FLOOD_CLASS_TUNNEL_NORMAL;
610 fib_prefix_from_ip46_addr(&t->remote, &tun_remote_pfx);
611 if (!ip46_address_is_multicast(&t->remote))
614 * source the FIB entry for the tunnel's destination
615 * and become a child thereof. The tunnel will then get poked
616 * when the forwarding for the entry updates, and the tunnel can
617 * re-stack accordingly
619 vtep_addr_ref(&t->local);
620 t->fib_entry_index = fib_table_entry_special_add
621 (t->encap_fib_index, &tun_remote_pfx, FIB_SOURCE_RR,
622 FIB_ENTRY_FLAG_NONE);
623 t->sibling_index = fib_entry_child_add
624 (t->fib_entry_index, FIB_NODE_TYPE_VXLAN_GPE_TUNNEL, t - ngm->tunnels);
625 vxlan_gpe_tunnel_restack_dpo(t);
629 /* Multicast tunnel -
630 * as the same mcast group can be used for mutiple mcast tunnels
631 * with different VNIs, create the output fib adjecency only if
632 * it does not already exist
634 fib_protocol_t fp = fib_ip_proto(is_ip6);
636 if (vtep_addr_ref(&t->remote) == 1)
638 fib_node_index_t mfei;
640 fib_route_path_t path = {
641 .frp_proto = fib_proto_to_dpo(fp),
642 .frp_addr = zero_addr,
643 .frp_sw_if_index = 0xffffffff,
646 .frp_flags = FIB_ROUTE_PATH_LOCAL,
648 const mfib_prefix_t mpfx = {
650 .fp_len = (is_ip6 ? 128 : 32),
651 .fp_grp_addr = tun_remote_pfx.fp_addr,
655 * Setup the (*,G) to receive traffic on the mcast group
656 * - the forwarding interface is for-us
657 * - the accepting interface is that from the API
659 mfib_table_entry_path_update(t->encap_fib_index,
661 MFIB_SOURCE_VXLAN_GPE,
663 MFIB_ITF_FLAG_FORWARD);
665 path.frp_sw_if_index = a->mcast_sw_if_index;
666 path.frp_flags = FIB_ROUTE_PATH_FLAG_NONE;
667 mfei = mfib_table_entry_path_update(t->encap_fib_index,
669 MFIB_SOURCE_VXLAN_GPE,
671 MFIB_ITF_FLAG_ACCEPT);
674 * Create the mcast adjacency to send traffic to the group
676 ai = adj_mcast_add_or_lock(fp,
677 fib_proto_to_link(fp),
678 a->mcast_sw_if_index);
681 * create a new end-point
683 mcast_shared_add(&t->remote, mfei, ai);
686 dpo_id_t dpo = DPO_INVALID;
687 mcast_shared_t ep = mcast_shared_get(&t->remote);
689 /* Stack shared mcast remote mac addr rewrite on encap */
690 dpo_set (&dpo, DPO_ADJACENCY_MCAST,
691 fib_proto_to_dpo(fp),
694 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
696 flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER;
699 /* Set vxlan tunnel output node */
700 hi->output_node_index = encap_index;
702 vnet_get_sw_interface (vnet_get_main(), sw_if_index)->flood_class = flood_class;
706 /* deleting a tunnel: tunnel must exist */
708 return VNET_API_ERROR_NO_SUCH_ENTRY;
710 t = pool_elt_at_index (ngm->tunnels, p[0]);
712 sw_if_index = t->sw_if_index;
713 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
714 vnet_sw_interface_t * si = vnet_get_sw_interface (vnm, t->sw_if_index);
715 si->flags |= VNET_SW_INTERFACE_FLAG_HIDDEN;
716 set_int_l2_mode(ngm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0, 0);
717 vec_add1 (ngm->free_vxlan_gpe_tunnel_hw_if_indices, t->hw_if_index);
719 ngm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
722 hash_unset (ngm->vxlan4_gpe_tunnel_by_key, key4.as_u64);
724 hash_unset_key_free (&ngm->vxlan6_gpe_tunnel_by_key, &key6);
726 if (!ip46_address_is_multicast(&t->remote))
728 vtep_addr_unref(&t->local);
729 fib_entry_child_remove(t->fib_entry_index, t->sibling_index);
730 fib_table_entry_delete_index(t->fib_entry_index, FIB_SOURCE_RR);
732 else if (vtep_addr_unref(&t->remote) == 0)
734 mcast_shared_remove(&t->remote);
737 fib_node_deinit(&t->node);
738 vec_free (t->rewrite);
739 pool_put (ngm->tunnels, t);
743 *sw_if_indexp = sw_if_index;
748 static clib_error_t *
749 vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm,
750 unformat_input_t * input,
751 vlib_cli_command_t * cmd)
753 unformat_input_t _line_input, * line_input = &_line_input;
755 ip46_address_t local, remote;
761 u32 mcast_sw_if_index = ~0;
762 u32 encap_fib_index = 0;
763 u32 decap_fib_index = 0;
764 u8 protocol = VXLAN_GPE_PROTOCOL_IP4;
769 vnet_vxlan_gpe_add_del_tunnel_args_t _a, * a = &_a;
771 clib_error_t *error = NULL;
773 /* Get a line of input. */
774 if (! unformat_user (input, unformat_line_input, line_input))
777 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
778 if (unformat (line_input, "del"))
780 else if (unformat (line_input, "local %U",
781 unformat_ip4_address, &local.ip4))
786 else if (unformat (line_input, "remote %U",
787 unformat_ip4_address, &remote.ip4))
792 else if (unformat (line_input, "local %U",
793 unformat_ip6_address, &local.ip6))
798 else if (unformat (line_input, "remote %U",
799 unformat_ip6_address, &remote.ip6))
804 else if (unformat (line_input, "group %U %U",
805 unformat_ip4_address, &remote.ip4,
806 unformat_vnet_sw_interface,
807 vnet_get_main(), &mcast_sw_if_index))
809 grp_set = remote_set = 1;
812 else if (unformat (line_input, "group %U %U",
813 unformat_ip6_address, &remote.ip6,
814 unformat_vnet_sw_interface,
815 vnet_get_main(), &mcast_sw_if_index))
817 grp_set = remote_set = 1;
820 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
823 encap_fib_index = fib_table_find (FIB_PROTOCOL_IP6, tmp);
825 encap_fib_index = fib_table_find (FIB_PROTOCOL_IP4, tmp);
827 if (encap_fib_index == ~0)
829 error = clib_error_return (0, "nonexistent encap fib id %d", tmp);
833 else if (unformat (line_input, "decap-vrf-id %d", &tmp))
836 decap_fib_index = fib_table_find (FIB_PROTOCOL_IP6, tmp);
838 decap_fib_index = fib_table_find (FIB_PROTOCOL_IP4, tmp);
840 if (decap_fib_index == ~0)
842 error = clib_error_return (0, "nonexistent decap fib id %d", tmp);
846 else if (unformat (line_input, "vni %d", &vni))
848 else if (unformat(line_input, "next-ip4"))
849 protocol = VXLAN_GPE_PROTOCOL_IP4;
850 else if (unformat(line_input, "next-ip6"))
851 protocol = VXLAN_GPE_PROTOCOL_IP6;
852 else if (unformat(line_input, "next-ethernet"))
853 protocol = VXLAN_GPE_PROTOCOL_ETHERNET;
854 else if (unformat(line_input, "next-nsh"))
855 protocol = VXLAN_GPE_PROTOCOL_NSH;
858 error = clib_error_return (0, "parse error: '%U'",
859 format_unformat_error, line_input);
866 error = clib_error_return (0, "tunnel local address not specified");
872 error = clib_error_return (0, "tunnel remote address not specified");
876 if (grp_set && !ip46_address_is_multicast(&remote))
878 error = clib_error_return (0, "tunnel group address not multicast");
882 if (grp_set == 0 && ip46_address_is_multicast(&remote))
884 error = clib_error_return (0, "remote address must be unicast");
888 if (grp_set && mcast_sw_if_index == ~0)
890 error = clib_error_return (0, "tunnel nonexistent multicast device");
893 if (ipv4_set && ipv6_set)
895 error = clib_error_return (0, "both IPv4 and IPv6 addresses specified");
899 if ((ipv4_set && memcmp(&local.ip4, &remote.ip4, sizeof(local.ip4)) == 0) ||
900 (ipv6_set && memcmp(&local.ip6, &remote.ip6, sizeof(local.ip6)) == 0))
902 error = clib_error_return (0, "src and remote addresses are identical");
908 error = clib_error_return (0, "vni not specified");
912 memset (a, 0, sizeof (*a));
915 a->is_ip6 = ipv6_set;
917 #define _(x) a->x = x;
918 foreach_gpe_copy_field;
919 if (ipv4_set) foreach_copy_ipv4
920 else foreach_copy_ipv6
923 rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
928 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
930 case VNET_API_ERROR_INVALID_DECAP_NEXT:
931 error = clib_error_return (0, "invalid decap-next...");
934 case VNET_API_ERROR_TUNNEL_EXIST:
935 error = clib_error_return (0, "tunnel already exists...");
938 case VNET_API_ERROR_NO_SUCH_ENTRY:
939 error = clib_error_return (0, "tunnel does not exist...");
943 error = clib_error_return
944 (0, "vnet_vxlan_gpe_add_del_tunnel returned %d", rv);
949 unformat_free (line_input);
955 * Add or delete a VXLAN-GPE Tunnel.
957 * VXLAN-GPE provides the features needed to allow L2 bridge domains (BDs)
958 * to span multiple servers. This is done by building an L2 overlay on
959 * top of an L3 network underlay using VXLAN-GPE tunnels.
961 * This makes it possible for servers to be co-located in the same data
962 * center or be separated geographically as long as they are reachable
963 * through the underlay L3 network.
965 * You can refer to this kind of L2 overlay bridge domain as a VXLAN-GPE sengment.
968 * Example of how to create a VXLAN-GPE Tunnel:
969 * @cliexcmd{create vxlan-gpe tunnel local 10.0.3.1 local 10.0.3.3 vni 13 encap-vrf-id 7}
970 * Example of how to delete a VXLAN Tunnel:
971 * @cliexcmd{create vxlan tunnel src 10.0.3.1 remote 10.0.3.3 vni 13 del}
974 VLIB_CLI_COMMAND (create_vxlan_gpe_tunnel_command, static) = {
975 .path = "create vxlan-gpe tunnel",
977 "create vxlan-gpe tunnel local <local-addr> "
978 " {remote <remote-addr>|group <mcast-addr> <intf-name>}"
979 " vni <nn> [next-ip4][next-ip6][next-ethernet][next-nsh]"
980 " [encap-vrf-id <nn>] [decap-vrf-id <nn>] [del]\n",
981 .function = vxlan_gpe_add_del_tunnel_command_fn,
986 * @brief CLI function for showing VXLAN GPE tunnels
995 static clib_error_t *
996 show_vxlan_gpe_tunnel_command_fn (vlib_main_t * vm,
997 unformat_input_t * input,
998 vlib_cli_command_t * cmd)
1000 vxlan_gpe_main_t * ngm = &vxlan_gpe_main;
1001 vxlan_gpe_tunnel_t * t;
1003 if (pool_elts (ngm->tunnels) == 0)
1004 vlib_cli_output (vm, "No vxlan-gpe tunnels configured.");
1006 pool_foreach (t, ngm->tunnels,
1008 vlib_cli_output (vm, "%U", format_vxlan_gpe_tunnel, t);
1015 * Display all the VXLAN-GPE Tunnel entries.
1018 * Example of how to display the VXLAN-GPE Tunnel entries:
1019 * @cliexstart{show vxlan-gpe tunnel}
1020 * [0] local 10.0.3.1 remote 10.0.3.3 vni 13 encap_fib_index 0 sw_if_index 5 decap_next l2
1024 VLIB_CLI_COMMAND (show_vxlan_gpe_tunnel_command, static) = {
1025 .path = "show vxlan-gpe",
1026 .function = show_vxlan_gpe_tunnel_command_fn,
1030 void vnet_int_vxlan_gpe_bypass_mode (u32 sw_if_index,
1035 vnet_feature_enable_disable ("ip6-unicast", "ip6-vxlan-gpe-bypass",
1036 sw_if_index, is_enable, 0, 0);
1038 vnet_feature_enable_disable ("ip4-unicast", "ip4-vxlan-gpe-bypass",
1039 sw_if_index, is_enable, 0, 0);
1043 static clib_error_t *
1044 set_ip_vxlan_gpe_bypass (u32 is_ip6,
1045 unformat_input_t * input,
1046 vlib_cli_command_t * cmd)
1048 unformat_input_t _line_input, * line_input = &_line_input;
1049 vnet_main_t * vnm = vnet_get_main();
1050 clib_error_t * error = 0;
1051 u32 sw_if_index, is_enable;
1056 if (! unformat_user (input, unformat_line_input, line_input))
1059 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1061 if (unformat_user (line_input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1063 else if (unformat (line_input, "del"))
1067 error = unformat_parse_error (line_input);
1072 if (~0 == sw_if_index)
1074 error = clib_error_return (0, "unknown interface `%U'",
1075 format_unformat_error, line_input);
1079 vnet_int_vxlan_gpe_bypass_mode (sw_if_index, is_ip6, is_enable);
1082 unformat_free (line_input);
1087 static clib_error_t *
1088 set_ip4_vxlan_gpe_bypass (vlib_main_t * vm,
1089 unformat_input_t * input,
1090 vlib_cli_command_t * cmd)
1092 return set_ip_vxlan_gpe_bypass (0, input, cmd);
1096 * This command adds the 'ip4-vxlan-gpe-bypass' graph node for a given interface.
1097 * By adding the IPv4 vxlan-gpe-bypass graph node to an interface, the node checks
1098 * for and validate input vxlan_gpe packet and bypass ip4-lookup, ip4-local,
1099 * ip4-udp-lookup nodes to speedup vxlan_gpe packet forwarding. This node will
1100 * cause extra overhead to for non-vxlan_gpe packets which is kept at a minimum.
1104 * Example of graph node before ip4-vxlan-gpe-bypass is enabled:
1105 * @cliexstart{show vlib graph ip4-vxlan-gpe-bypass}
1106 * Name Next Previous
1107 * ip4-vxlan-gpe-bypass error-drop [0]
1108 * vxlan4-gpe-input [1]
1112 * Example of how to enable ip4-vxlan-gpe-bypass on an interface:
1113 * @cliexcmd{set interface ip vxlan-gpe-bypass GigabitEthernet2/0/0}
1115 * Example of graph node after ip4-vxlan-gpe-bypass is enabled:
1116 * @cliexstart{show vlib graph ip4-vxlan-gpe-bypass}
1117 * Name Next Previous
1118 * ip4-vxlan-gpe-bypass error-drop [0] ip4-input
1119 * vxlan4-gpe-input [1] ip4-input-no-checksum
1123 * Example of how to display the feature enabed on an interface:
1124 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1125 * IP feature paths configured on GigabitEthernet2/0/0...
1128 * ip4-vxlan-gpe-bypass
1133 * Example of how to disable ip4-vxlan-gpe-bypass on an interface:
1134 * @cliexcmd{set interface ip vxlan-gpe-bypass GigabitEthernet2/0/0 del}
1138 VLIB_CLI_COMMAND (set_interface_ip_vxlan_gpe_bypass_command, static) = {
1139 .path = "set interface ip vxlan-gpe-bypass",
1140 .function = set_ip4_vxlan_gpe_bypass,
1141 .short_help = "set interface ip vxlan-gpe-bypass <interface> [del]",
1145 static clib_error_t *
1146 set_ip6_vxlan_gpe_bypass (vlib_main_t * vm,
1147 unformat_input_t * input,
1148 vlib_cli_command_t * cmd)
1150 return set_ip_vxlan_gpe_bypass (1, input, cmd);
1154 * This command adds the 'ip6-vxlan-gpe-bypass' graph node for a given interface.
1155 * By adding the IPv6 vxlan-gpe-bypass graph node to an interface, the node checks
1156 * for and validate input vxlan_gpe packet and bypass ip6-lookup, ip6-local,
1157 * ip6-udp-lookup nodes to speedup vxlan_gpe packet forwarding. This node will
1158 * cause extra overhead to for non-vxlan_gpe packets which is kept at a minimum.
1162 * Example of graph node before ip6-vxlan-gpe-bypass is enabled:
1163 * @cliexstart{show vlib graph ip6-vxlan-gpe-bypass}
1164 * Name Next Previous
1165 * ip6-vxlan-gpe-bypass error-drop [0]
1166 * vxlan6-gpe-input [1]
1170 * Example of how to enable ip6-vxlan-gpe-bypass on an interface:
1171 * @cliexcmd{set interface ip6 vxlan-gpe-bypass GigabitEthernet2/0/0}
1173 * Example of graph node after ip6-vxlan-gpe-bypass is enabled:
1174 * @cliexstart{show vlib graph ip6-vxlan-gpe-bypass}
1175 * Name Next Previous
1176 * ip6-vxlan-gpe-bypass error-drop [0] ip6-input
1177 * vxlan6-gpe-input [1] ip4-input-no-checksum
1181 * Example of how to display the feature enabed on an interface:
1182 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1183 * IP feature paths configured on GigabitEthernet2/0/0...
1186 * ip6-vxlan-gpe-bypass
1191 * Example of how to disable ip6-vxlan-gpe-bypass on an interface:
1192 * @cliexcmd{set interface ip6 vxlan-gpe-bypass GigabitEthernet2/0/0 del}
1196 VLIB_CLI_COMMAND (set_interface_ip6_vxlan_gpe_bypass_command, static) = {
1197 .path = "set interface ip6 vxlan-gpe-bypass",
1198 .function = set_ip6_vxlan_gpe_bypass,
1199 .short_help = "set interface ip vxlan-gpe-bypass <interface> [del]",
1204 VNET_FEATURE_INIT (ip4_vxlan_gpe_bypass, static) =
1206 .arc_name = "ip4-unicast",
1207 .node_name = "ip4-vxlan-gpe-bypass",
1208 .runs_before = VNET_FEATURES ("ip4-lookup"),
1211 VNET_FEATURE_INIT (ip6_vxlan_gpe_bypass, static) =
1213 .arc_name = "ip6-unicast",
1214 .node_name = "ip6-vxlan-gpe-bypass",
1215 .runs_before = VNET_FEATURES ("ip6-lookup"),
1220 * @brief Feature init function for VXLAN GPE
1227 clib_error_t *vxlan_gpe_init (vlib_main_t *vm)
1229 vxlan_gpe_main_t *ngm = &vxlan_gpe_main;
1231 ngm->vnet_main = vnet_get_main();
1232 ngm->vlib_main = vm;
1234 ngm->vxlan4_gpe_tunnel_by_key
1235 = hash_create_mem (0, sizeof(vxlan4_gpe_tunnel_key_t), sizeof (uword));
1237 ngm->vxlan6_gpe_tunnel_by_key
1238 = hash_create_mem (0, sizeof(vxlan6_gpe_tunnel_key_t), sizeof (uword));
1241 ngm->mcast_shared = hash_create_mem(0,
1242 sizeof(ip46_address_t),
1243 sizeof(mcast_shared_t));
1245 udp_register_dst_port (vm, UDP_DST_PORT_VXLAN_GPE,
1246 vxlan4_gpe_input_node.index, 1 /* is_ip4 */);
1247 udp_register_dst_port (vm, UDP_DST_PORT_VXLAN6_GPE,
1248 vxlan6_gpe_input_node.index, 0 /* is_ip4 */);
1250 /* Register the list of standard decap protocols supported */
1251 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_IP4,
1252 VXLAN_GPE_INPUT_NEXT_IP4_INPUT);
1253 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_IP6,
1254 VXLAN_GPE_INPUT_NEXT_IP6_INPUT);
1255 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_ETHERNET,
1256 VXLAN_GPE_INPUT_NEXT_ETHERNET_INPUT);
1258 fib_node_register_type(FIB_NODE_TYPE_VXLAN_GPE_TUNNEL, &vxlan_gpe_vft);
1263 VLIB_INIT_FUNCTION(vxlan_gpe_init);