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)
186 ASSERT(FIB_NODE_TYPE_VXLAN_GPE_TUNNEL == node->fn_type);
187 return ((vxlan_gpe_tunnel_t*) (((char*)node) -
188 STRUCT_OFFSET_OF(vxlan_gpe_tunnel_t, node)));
192 * Function definition to backwalk a FIB node -
193 * Here we will restack the new dpo of VXLAN_GPE DIP to encap node.
195 static fib_node_back_walk_rc_t
196 vxlan_gpe_tunnel_back_walk (fib_node_t *node,
197 fib_node_back_walk_ctx_t *ctx)
199 vxlan_gpe_tunnel_restack_dpo(vxlan_gpe_tunnel_from_fib_node(node));
200 return (FIB_NODE_BACK_WALK_CONTINUE);
204 * Function definition to get a FIB node from its index
207 vxlan_gpe_tunnel_fib_node_get (fib_node_index_t index)
209 vxlan_gpe_tunnel_t * t;
210 vxlan_gpe_main_t * ngm = &vxlan_gpe_main;
212 t = pool_elt_at_index(ngm->tunnels, index);
218 * Function definition to inform the FIB node that its last lock has gone.
221 vxlan_gpe_tunnel_last_lock_gone (fib_node_t *node)
224 * The VXLAN_GPE tunnel is a root of the graph. As such
225 * it never has children and thus is never locked.
231 * Virtual function table registered by VXLAN_GPE tunnels
232 * for participation in the FIB object graph.
234 const static fib_node_vft_t vxlan_gpe_vft = {
235 .fnv_get = vxlan_gpe_tunnel_fib_node_get,
236 .fnv_last_lock = vxlan_gpe_tunnel_last_lock_gone,
237 .fnv_back_walk = vxlan_gpe_tunnel_back_walk,
240 #define foreach_gpe_copy_field \
243 _(mcast_sw_if_index) \
247 #define foreach_copy_ipv4 { \
248 _(local.ip4.as_u32) \
249 _(remote.ip4.as_u32) \
252 #define foreach_copy_ipv6 { \
253 _(local.ip6.as_u64[0]) \
254 _(local.ip6.as_u64[1]) \
255 _(remote.ip6.as_u64[0]) \
256 _(remote.ip6.as_u64[1]) \
261 * @brief Calculate IPv4 VXLAN GPE rewrite header
268 int vxlan4_gpe_rewrite (vxlan_gpe_tunnel_t * t, u32 extension_size,
269 u8 protocol_override, uword encap_next_node)
273 ip4_vxlan_gpe_header_t * h0;
276 len = sizeof (*h0) + extension_size;
278 vec_free(t->rewrite);
279 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
281 h0 = (ip4_vxlan_gpe_header_t *) rw;
283 /* Fixed portion of the (outer) ip4 header */
285 ip0->ip_version_and_header_length = 0x45;
287 ip0->protocol = IP_PROTOCOL_UDP;
289 /* we fix up the ip4 header length and checksum after-the-fact */
290 ip0->src_address.as_u32 = t->local.ip4.as_u32;
291 ip0->dst_address.as_u32 = t->remote.ip4.as_u32;
292 ip0->checksum = ip4_header_checksum (ip0);
294 /* UDP header, randomize src port on something, maybe? */
295 h0->udp.src_port = clib_host_to_net_u16 (4790);
296 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_VXLAN_GPE);
298 /* VXLAN header. Are we having fun yet? */
299 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
300 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
301 if (protocol_override)
303 h0->vxlan.protocol = protocol_override;
307 h0->vxlan.protocol = t->protocol;
309 t->rewrite_size = sizeof(ip4_vxlan_gpe_header_t) + extension_size;
310 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
313 t->encap_next_node = encap_next_node;
318 * @brief Calculate IPv6 VXLAN GPE rewrite header
325 int vxlan6_gpe_rewrite (vxlan_gpe_tunnel_t * t, u32 extension_size,
326 u8 protocol_override, uword encap_next_node)
330 ip6_vxlan_gpe_header_t * h0;
333 len = sizeof (*h0) + extension_size;
335 vec_free(t->rewrite);
336 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
338 h0 = (ip6_vxlan_gpe_header_t *) rw;
340 /* Fixed portion of the (outer) ip4 header */
342 ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32(6 << 28);
343 ip0->hop_limit = 255;
344 ip0->protocol = IP_PROTOCOL_UDP;
346 ip0->src_address.as_u64[0] = t->local.ip6.as_u64[0];
347 ip0->src_address.as_u64[1] = t->local.ip6.as_u64[1];
348 ip0->dst_address.as_u64[0] = t->remote.ip6.as_u64[0];
349 ip0->dst_address.as_u64[1] = t->remote.ip6.as_u64[1];
351 /* UDP header, randomize src port on something, maybe? */
352 h0->udp.src_port = clib_host_to_net_u16 (4790);
353 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_VXLAN_GPE);
355 /* VXLAN header. Are we having fun yet? */
356 h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P;
357 h0->vxlan.ver_res = VXLAN_GPE_VERSION;
358 if (protocol_override)
360 h0->vxlan.protocol = t->protocol;
364 h0->vxlan.protocol = protocol_override;
366 t->rewrite_size = sizeof(ip4_vxlan_gpe_header_t) + extension_size;
367 h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8);
370 t->encap_next_node = encap_next_node;
375 hash_set_key_copy (uword ** h, void * key, uword v) {
376 size_t ksz = hash_header(*h)->user;
377 void * copy = clib_mem_alloc (ksz);
378 clib_memcpy (copy, key, ksz);
379 hash_set_mem (*h, copy, v);
383 hash_unset_key_free (uword ** h, void * key) {
384 hash_pair_t * hp = hash_get_pair_mem (*h, key);
386 key = uword_to_pointer (hp->key, void *);
387 hash_unset_mem (*h, key);
392 vtep_addr_ref(ip46_address_t *ip)
394 uword *vtep = ip46_address_is_ip4(ip) ?
395 hash_get (vxlan_gpe_main.vtep4, ip->ip4.as_u32) :
396 hash_get_mem (vxlan_gpe_main.vtep6, &ip->ip6);
399 ip46_address_is_ip4(ip) ?
400 hash_set (vxlan_gpe_main.vtep4, ip->ip4.as_u32, 1) :
401 hash_set_key_copy (&vxlan_gpe_main.vtep6, &ip->ip6, 1);
406 vtep_addr_unref(ip46_address_t *ip)
408 uword *vtep = ip46_address_is_ip4(ip) ?
409 hash_get (vxlan_gpe_main.vtep4, ip->ip4.as_u32) :
410 hash_get_mem (vxlan_gpe_main.vtep6, &ip->ip6);
414 ip46_address_is_ip4(ip) ?
415 hash_unset (vxlan_gpe_main.vtep4, ip->ip4.as_u32) :
416 hash_unset_key_free (&vxlan_gpe_main.vtep6, &ip->ip6);
420 typedef CLIB_PACKED(union {
422 fib_node_index_t mfib_entry_index;
423 adj_index_t mcast_adj_index;
428 static inline mcast_shared_t
429 mcast_shared_get(ip46_address_t * ip)
431 ASSERT(ip46_address_is_multicast(ip));
432 uword * p = hash_get_mem (vxlan_gpe_main.mcast_shared, ip);
434 return (mcast_shared_t) { .as_u64 = *p };
438 mcast_shared_add(ip46_address_t *remote,
439 fib_node_index_t mfei,
442 mcast_shared_t new_ep = {
443 .mcast_adj_index = ai,
444 .mfib_entry_index = mfei,
447 hash_set_key_copy (&vxlan_gpe_main.mcast_shared, remote, new_ep.as_u64);
451 mcast_shared_remove(ip46_address_t *remote)
453 mcast_shared_t ep = mcast_shared_get(remote);
455 adj_unlock(ep.mcast_adj_index);
456 mfib_table_entry_delete_index(ep.mfib_entry_index,
457 MFIB_SOURCE_VXLAN_GPE);
459 hash_unset_key_free (&vxlan_gpe_main.mcast_shared, remote);
462 static inline fib_protocol_t
463 fib_ip_proto(bool is_ip6)
465 return (is_ip6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
469 * @brief Add or Del a VXLAN GPE tunnel
472 * @param *sw_if_index
477 int vnet_vxlan_gpe_add_del_tunnel
478 (vnet_vxlan_gpe_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
480 vxlan_gpe_main_t * ngm = &vxlan_gpe_main;
481 vxlan_gpe_tunnel_t *t = 0;
482 vnet_main_t * vnm = ngm->vnet_main;
483 vnet_hw_interface_t * hi;
485 u32 hw_if_index = ~0;
486 u32 sw_if_index = ~0;
488 vxlan4_gpe_tunnel_key_t key4, *key4_copy;
489 vxlan6_gpe_tunnel_key_t key6, *key6_copy;
490 u32 is_ip6 = a->is_ip6;
494 key4.local = a->local.ip4.as_u32;
495 key4.remote = a->remote.ip4.as_u32;
496 key4.vni = clib_host_to_net_u32 (a->vni << 8);
499 p = hash_get_mem(ngm->vxlan4_gpe_tunnel_by_key, &key4);
503 key6.local.as_u64[0] = a->local.ip6.as_u64[0];
504 key6.local.as_u64[1] = a->local.ip6.as_u64[1];
505 key6.remote.as_u64[0] = a->remote.ip6.as_u64[0];
506 key6.remote.as_u64[1] = a->remote.ip6.as_u64[1];
507 key6.vni = clib_host_to_net_u32 (a->vni << 8);
509 p = hash_get_mem(ngm->vxlan6_gpe_tunnel_by_key, &key6);
514 l2input_main_t * l2im = &l2input_main;
516 /* adding a tunnel: tunnel must not already exist */
518 return VNET_API_ERROR_TUNNEL_EXIST;
520 pool_get_aligned (ngm->tunnels, t, CLIB_CACHE_LINE_BYTES);
521 memset (t, 0, sizeof (*t));
523 /* copy from arg structure */
524 #define _(x) t->x = a->x;
525 foreach_gpe_copy_field;
526 if (!a->is_ip6) foreach_copy_ipv4
527 else foreach_copy_ipv6
530 if (!a->is_ip6) t->flags |= VXLAN_GPE_TUNNEL_IS_IPV4;
533 rv = vxlan4_gpe_rewrite (t, 0, 0, VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP);
535 rv = vxlan6_gpe_rewrite (t, 0, 0, VXLAN_GPE_ENCAP_NEXT_IP6_LOOKUP);
540 pool_put (ngm->tunnels, t);
546 key4_copy = clib_mem_alloc (sizeof (*key4_copy));
547 clib_memcpy (key4_copy, &key4, sizeof (*key4_copy));
548 hash_set_mem (ngm->vxlan4_gpe_tunnel_by_key, key4_copy,
553 key6_copy = clib_mem_alloc (sizeof (*key6_copy));
554 clib_memcpy (key6_copy, &key6, sizeof (*key6_copy));
555 hash_set_mem (ngm->vxlan6_gpe_tunnel_by_key, key6_copy,
559 if (vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices) > 0)
561 vnet_interface_main_t * im = &vnm->interface_main;
562 hw_if_index = ngm->free_vxlan_gpe_tunnel_hw_if_indices
563 [vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices)-1];
564 _vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices) -= 1;
566 hi = vnet_get_hw_interface (vnm, hw_if_index);
567 hi->dev_instance = t - ngm->tunnels;
568 hi->hw_instance = hi->dev_instance;
569 /* clear old stats of freed tunnel before reuse */
570 sw_if_index = hi->sw_if_index;
571 vnet_interface_counter_lock(im);
572 vlib_zero_combined_counter
573 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
574 vlib_zero_combined_counter
575 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
576 vlib_zero_simple_counter
577 (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
578 vnet_interface_counter_unlock(im);
582 hw_if_index = vnet_register_interface
583 (vnm, vxlan_gpe_device_class.index, t - ngm->tunnels,
584 vxlan_gpe_hw_class.index, t - ngm->tunnels);
585 hi = vnet_get_hw_interface (vnm, hw_if_index);
586 hi->output_node_index = vxlan_gpe_encap_node.index;
589 t->hw_if_index = hw_if_index;
590 t->sw_if_index = sw_if_index = hi->sw_if_index;
591 vec_validate_init_empty (ngm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
592 ngm->tunnel_index_by_sw_if_index[sw_if_index] = t - ngm->tunnels;
594 /* setup l2 input config with l2 feature and bd 0 to drop packet */
595 vec_validate (l2im->configs, sw_if_index);
596 l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
597 l2im->configs[sw_if_index].bd_index = 0;
599 vnet_sw_interface_t * si = vnet_get_sw_interface (vnm, sw_if_index);
600 si->flags &= ~VNET_SW_INTERFACE_FLAG_HIDDEN;
601 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
602 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
603 fib_node_init(&t->node, FIB_NODE_TYPE_VXLAN_GPE_TUNNEL);
604 fib_prefix_t tun_remote_pfx;
605 u32 encap_index = vxlan_gpe_encap_node.index;
606 vnet_flood_class_t flood_class = VNET_FLOOD_CLASS_TUNNEL_NORMAL;
608 fib_prefix_from_ip46_addr(&t->remote, &tun_remote_pfx);
609 if (!ip46_address_is_multicast(&t->remote))
612 * source the FIB entry for the tunnel's destination
613 * and become a child thereof. The tunnel will then get poked
614 * when the forwarding for the entry updates, and the tunnel can
615 * re-stack accordingly
617 vtep_addr_ref(&t->local);
618 t->fib_entry_index = fib_table_entry_special_add
619 (t->encap_fib_index, &tun_remote_pfx, FIB_SOURCE_RR,
620 FIB_ENTRY_FLAG_NONE);
621 t->sibling_index = fib_entry_child_add
622 (t->fib_entry_index, FIB_NODE_TYPE_VXLAN_GPE_TUNNEL, t - ngm->tunnels);
623 vxlan_gpe_tunnel_restack_dpo(t);
627 /* Multicast tunnel -
628 * as the same mcast group can be used for mutiple mcast tunnels
629 * with different VNIs, create the output fib adjecency only if
630 * it does not already exist
632 fib_protocol_t fp = fib_ip_proto(is_ip6);
634 if (vtep_addr_ref(&t->remote) == 1)
636 fib_node_index_t mfei;
638 fib_route_path_t path = {
639 .frp_proto = fib_proto_to_dpo(fp),
640 .frp_addr = zero_addr,
641 .frp_sw_if_index = 0xffffffff,
644 .frp_flags = FIB_ROUTE_PATH_LOCAL,
646 const mfib_prefix_t mpfx = {
648 .fp_len = (is_ip6 ? 128 : 32),
649 .fp_grp_addr = tun_remote_pfx.fp_addr,
653 * Setup the (*,G) to receive traffic on the mcast group
654 * - the forwarding interface is for-us
655 * - the accepting interface is that from the API
657 mfib_table_entry_path_update(t->encap_fib_index,
659 MFIB_SOURCE_VXLAN_GPE,
661 MFIB_ITF_FLAG_FORWARD);
663 path.frp_sw_if_index = a->mcast_sw_if_index;
664 path.frp_flags = FIB_ROUTE_PATH_FLAG_NONE;
665 mfei = mfib_table_entry_path_update(t->encap_fib_index,
667 MFIB_SOURCE_VXLAN_GPE,
669 MFIB_ITF_FLAG_ACCEPT);
672 * Create the mcast adjacency to send traffic to the group
674 ai = adj_mcast_add_or_lock(fp,
675 fib_proto_to_link(fp),
676 a->mcast_sw_if_index);
679 * create a new end-point
681 mcast_shared_add(&t->remote, mfei, ai);
684 dpo_id_t dpo = DPO_INVALID;
685 mcast_shared_t ep = mcast_shared_get(&t->remote);
687 /* Stack shared mcast remote mac addr rewrite on encap */
688 dpo_set (&dpo, DPO_ADJACENCY_MCAST,
689 fib_proto_to_dpo(fp),
692 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
694 flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER;
697 /* Set vxlan tunnel output node */
698 hi->output_node_index = encap_index;
700 vnet_get_sw_interface (vnet_get_main(), sw_if_index)->flood_class = flood_class;
704 /* deleting a tunnel: tunnel must exist */
706 return VNET_API_ERROR_NO_SUCH_ENTRY;
708 t = pool_elt_at_index (ngm->tunnels, p[0]);
710 sw_if_index = t->sw_if_index;
711 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
712 vnet_sw_interface_t * si = vnet_get_sw_interface (vnm, t->sw_if_index);
713 si->flags |= VNET_SW_INTERFACE_FLAG_HIDDEN;
714 set_int_l2_mode(ngm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0, 0);
715 vec_add1 (ngm->free_vxlan_gpe_tunnel_hw_if_indices, t->hw_if_index);
717 ngm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
720 hash_unset (ngm->vxlan4_gpe_tunnel_by_key, key4.as_u64);
722 hash_unset_key_free (&ngm->vxlan6_gpe_tunnel_by_key, &key6);
724 if (!ip46_address_is_multicast(&t->remote))
726 vtep_addr_unref(&t->local);
727 fib_entry_child_remove(t->fib_entry_index, t->sibling_index);
728 fib_table_entry_delete_index(t->fib_entry_index, FIB_SOURCE_RR);
730 else if (vtep_addr_unref(&t->remote) == 0)
732 mcast_shared_remove(&t->remote);
735 fib_node_deinit(&t->node);
736 vec_free (t->rewrite);
737 pool_put (ngm->tunnels, t);
741 *sw_if_indexp = sw_if_index;
746 static clib_error_t *
747 vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm,
748 unformat_input_t * input,
749 vlib_cli_command_t * cmd)
751 unformat_input_t _line_input, * line_input = &_line_input;
753 ip46_address_t local, remote;
759 u32 mcast_sw_if_index = ~0;
760 u32 encap_fib_index = 0;
761 u32 decap_fib_index = 0;
762 u8 protocol = VXLAN_GPE_PROTOCOL_IP4;
767 vnet_vxlan_gpe_add_del_tunnel_args_t _a, * a = &_a;
769 clib_error_t *error = NULL;
771 /* Get a line of input. */
772 if (! unformat_user (input, unformat_line_input, line_input))
775 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
776 if (unformat (line_input, "del"))
778 else if (unformat (line_input, "local %U",
779 unformat_ip4_address, &local.ip4))
784 else if (unformat (line_input, "remote %U",
785 unformat_ip4_address, &remote.ip4))
790 else if (unformat (line_input, "local %U",
791 unformat_ip6_address, &local.ip6))
796 else if (unformat (line_input, "remote %U",
797 unformat_ip6_address, &remote.ip6))
802 else if (unformat (line_input, "group %U %U",
803 unformat_ip4_address, &remote.ip4,
804 unformat_vnet_sw_interface,
805 vnet_get_main(), &mcast_sw_if_index))
807 grp_set = remote_set = 1;
810 else if (unformat (line_input, "group %U %U",
811 unformat_ip6_address, &remote.ip6,
812 unformat_vnet_sw_interface,
813 vnet_get_main(), &mcast_sw_if_index))
815 grp_set = remote_set = 1;
818 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
821 encap_fib_index = fib_table_find (FIB_PROTOCOL_IP6, tmp);
823 encap_fib_index = fib_table_find (FIB_PROTOCOL_IP4, tmp);
825 if (encap_fib_index == ~0)
827 error = clib_error_return (0, "nonexistent encap fib id %d", tmp);
831 else if (unformat (line_input, "decap-vrf-id %d", &tmp))
834 decap_fib_index = fib_table_find (FIB_PROTOCOL_IP6, tmp);
836 decap_fib_index = fib_table_find (FIB_PROTOCOL_IP4, tmp);
838 if (decap_fib_index == ~0)
840 error = clib_error_return (0, "nonexistent decap fib id %d", tmp);
844 else if (unformat (line_input, "vni %d", &vni))
846 else if (unformat(line_input, "next-ip4"))
847 protocol = VXLAN_GPE_PROTOCOL_IP4;
848 else if (unformat(line_input, "next-ip6"))
849 protocol = VXLAN_GPE_PROTOCOL_IP6;
850 else if (unformat(line_input, "next-ethernet"))
851 protocol = VXLAN_GPE_PROTOCOL_ETHERNET;
852 else if (unformat(line_input, "next-nsh"))
853 protocol = VXLAN_GPE_PROTOCOL_NSH;
856 error = clib_error_return (0, "parse error: '%U'",
857 format_unformat_error, line_input);
864 error = clib_error_return (0, "tunnel local address not specified");
870 error = clib_error_return (0, "tunnel remote address not specified");
874 if (grp_set && !ip46_address_is_multicast(&remote))
876 error = clib_error_return (0, "tunnel group address not multicast");
880 if (grp_set == 0 && ip46_address_is_multicast(&remote))
882 error = clib_error_return (0, "remote address must be unicast");
886 if (grp_set && mcast_sw_if_index == ~0)
888 error = clib_error_return (0, "tunnel nonexistent multicast device");
891 if (ipv4_set && ipv6_set)
893 error = clib_error_return (0, "both IPv4 and IPv6 addresses specified");
897 if ((ipv4_set && memcmp(&local.ip4, &remote.ip4, sizeof(local.ip4)) == 0) ||
898 (ipv6_set && memcmp(&local.ip6, &remote.ip6, sizeof(local.ip6)) == 0))
900 error = clib_error_return (0, "src and remote addresses are identical");
906 error = clib_error_return (0, "vni not specified");
910 memset (a, 0, sizeof (*a));
913 a->is_ip6 = ipv6_set;
915 #define _(x) a->x = x;
916 foreach_gpe_copy_field;
917 if (ipv4_set) foreach_copy_ipv4
918 else foreach_copy_ipv6
921 rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
926 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
928 case VNET_API_ERROR_INVALID_DECAP_NEXT:
929 error = clib_error_return (0, "invalid decap-next...");
932 case VNET_API_ERROR_TUNNEL_EXIST:
933 error = clib_error_return (0, "tunnel already exists...");
936 case VNET_API_ERROR_NO_SUCH_ENTRY:
937 error = clib_error_return (0, "tunnel does not exist...");
941 error = clib_error_return
942 (0, "vnet_vxlan_gpe_add_del_tunnel returned %d", rv);
947 unformat_free (line_input);
953 * Add or delete a VXLAN-GPE Tunnel.
955 * VXLAN-GPE provides the features needed to allow L2 bridge domains (BDs)
956 * to span multiple servers. This is done by building an L2 overlay on
957 * top of an L3 network underlay using VXLAN-GPE tunnels.
959 * This makes it possible for servers to be co-located in the same data
960 * center or be separated geographically as long as they are reachable
961 * through the underlay L3 network.
963 * You can refer to this kind of L2 overlay bridge domain as a VXLAN-GPE sengment.
966 * Example of how to create a VXLAN-GPE Tunnel:
967 * @cliexcmd{create vxlan-gpe tunnel local 10.0.3.1 local 10.0.3.3 vni 13 encap-vrf-id 7}
968 * Example of how to delete a VXLAN Tunnel:
969 * @cliexcmd{create vxlan tunnel src 10.0.3.1 remote 10.0.3.3 vni 13 del}
972 VLIB_CLI_COMMAND (create_vxlan_gpe_tunnel_command, static) = {
973 .path = "create vxlan-gpe tunnel",
975 "create vxlan-gpe tunnel local <local-addr> "
976 " {remote <remote-addr>|group <mcast-addr> <intf-name>}"
977 " vni <nn> [next-ip4][next-ip6][next-ethernet][next-nsh]"
978 " [encap-vrf-id <nn>] [decap-vrf-id <nn>] [del]\n",
979 .function = vxlan_gpe_add_del_tunnel_command_fn,
984 * @brief CLI function for showing VXLAN GPE tunnels
993 static clib_error_t *
994 show_vxlan_gpe_tunnel_command_fn (vlib_main_t * vm,
995 unformat_input_t * input,
996 vlib_cli_command_t * cmd)
998 vxlan_gpe_main_t * ngm = &vxlan_gpe_main;
999 vxlan_gpe_tunnel_t * t;
1001 if (pool_elts (ngm->tunnels) == 0)
1002 vlib_cli_output (vm, "No vxlan-gpe tunnels configured.");
1004 pool_foreach (t, ngm->tunnels,
1006 vlib_cli_output (vm, "%U", format_vxlan_gpe_tunnel, t);
1013 * Display all the VXLAN-GPE Tunnel entries.
1016 * Example of how to display the VXLAN-GPE Tunnel entries:
1017 * @cliexstart{show vxlan-gpe tunnel}
1018 * [0] local 10.0.3.1 remote 10.0.3.3 vni 13 encap_fib_index 0 sw_if_index 5 decap_next l2
1022 VLIB_CLI_COMMAND (show_vxlan_gpe_tunnel_command, static) = {
1023 .path = "show vxlan-gpe",
1024 .function = show_vxlan_gpe_tunnel_command_fn,
1028 void vnet_int_vxlan_gpe_bypass_mode (u32 sw_if_index,
1033 vnet_feature_enable_disable ("ip6-unicast", "ip6-vxlan-gpe-bypass",
1034 sw_if_index, is_enable, 0, 0);
1036 vnet_feature_enable_disable ("ip4-unicast", "ip4-vxlan-gpe-bypass",
1037 sw_if_index, is_enable, 0, 0);
1041 static clib_error_t *
1042 set_ip_vxlan_gpe_bypass (u32 is_ip6,
1043 unformat_input_t * input,
1044 vlib_cli_command_t * cmd)
1046 unformat_input_t _line_input, * line_input = &_line_input;
1047 vnet_main_t * vnm = vnet_get_main();
1048 clib_error_t * error = 0;
1049 u32 sw_if_index, is_enable;
1054 if (! unformat_user (input, unformat_line_input, line_input))
1057 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1059 if (unformat_user (line_input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1061 else if (unformat (line_input, "del"))
1065 error = unformat_parse_error (line_input);
1070 if (~0 == sw_if_index)
1072 error = clib_error_return (0, "unknown interface `%U'",
1073 format_unformat_error, line_input);
1077 vnet_int_vxlan_gpe_bypass_mode (sw_if_index, is_ip6, is_enable);
1080 unformat_free (line_input);
1085 static clib_error_t *
1086 set_ip4_vxlan_gpe_bypass (vlib_main_t * vm,
1087 unformat_input_t * input,
1088 vlib_cli_command_t * cmd)
1090 return set_ip_vxlan_gpe_bypass (0, input, cmd);
1094 * This command adds the 'ip4-vxlan-gpe-bypass' graph node for a given interface.
1095 * By adding the IPv4 vxlan-gpe-bypass graph node to an interface, the node checks
1096 * for and validate input vxlan_gpe packet and bypass ip4-lookup, ip4-local,
1097 * ip4-udp-lookup nodes to speedup vxlan_gpe packet forwarding. This node will
1098 * cause extra overhead to for non-vxlan_gpe packets which is kept at a minimum.
1102 * Example of graph node before ip4-vxlan-gpe-bypass is enabled:
1103 * @cliexstart{show vlib graph ip4-vxlan-gpe-bypass}
1104 * Name Next Previous
1105 * ip4-vxlan-gpe-bypass error-drop [0]
1106 * vxlan4-gpe-input [1]
1110 * Example of how to enable ip4-vxlan-gpe-bypass on an interface:
1111 * @cliexcmd{set interface ip vxlan-gpe-bypass GigabitEthernet2/0/0}
1113 * Example of graph node after ip4-vxlan-gpe-bypass is enabled:
1114 * @cliexstart{show vlib graph ip4-vxlan-gpe-bypass}
1115 * Name Next Previous
1116 * ip4-vxlan-gpe-bypass error-drop [0] ip4-input
1117 * vxlan4-gpe-input [1] ip4-input-no-checksum
1121 * Example of how to display the feature enabed on an interface:
1122 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1123 * IP feature paths configured on GigabitEthernet2/0/0...
1126 * ip4-vxlan-gpe-bypass
1131 * Example of how to disable ip4-vxlan-gpe-bypass on an interface:
1132 * @cliexcmd{set interface ip vxlan-gpe-bypass GigabitEthernet2/0/0 del}
1136 VLIB_CLI_COMMAND (set_interface_ip_vxlan_gpe_bypass_command, static) = {
1137 .path = "set interface ip vxlan-gpe-bypass",
1138 .function = set_ip4_vxlan_gpe_bypass,
1139 .short_help = "set interface ip vxlan-gpe-bypass <interface> [del]",
1143 static clib_error_t *
1144 set_ip6_vxlan_gpe_bypass (vlib_main_t * vm,
1145 unformat_input_t * input,
1146 vlib_cli_command_t * cmd)
1148 return set_ip_vxlan_gpe_bypass (1, input, cmd);
1152 * This command adds the 'ip6-vxlan-gpe-bypass' graph node for a given interface.
1153 * By adding the IPv6 vxlan-gpe-bypass graph node to an interface, the node checks
1154 * for and validate input vxlan_gpe packet and bypass ip6-lookup, ip6-local,
1155 * ip6-udp-lookup nodes to speedup vxlan_gpe packet forwarding. This node will
1156 * cause extra overhead to for non-vxlan_gpe packets which is kept at a minimum.
1160 * Example of graph node before ip6-vxlan-gpe-bypass is enabled:
1161 * @cliexstart{show vlib graph ip6-vxlan-gpe-bypass}
1162 * Name Next Previous
1163 * ip6-vxlan-gpe-bypass error-drop [0]
1164 * vxlan6-gpe-input [1]
1168 * Example of how to enable ip6-vxlan-gpe-bypass on an interface:
1169 * @cliexcmd{set interface ip6 vxlan-gpe-bypass GigabitEthernet2/0/0}
1171 * Example of graph node after ip6-vxlan-gpe-bypass is enabled:
1172 * @cliexstart{show vlib graph ip6-vxlan-gpe-bypass}
1173 * Name Next Previous
1174 * ip6-vxlan-gpe-bypass error-drop [0] ip6-input
1175 * vxlan6-gpe-input [1] ip4-input-no-checksum
1179 * Example of how to display the feature enabed on an interface:
1180 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
1181 * IP feature paths configured on GigabitEthernet2/0/0...
1184 * ip6-vxlan-gpe-bypass
1189 * Example of how to disable ip6-vxlan-gpe-bypass on an interface:
1190 * @cliexcmd{set interface ip6 vxlan-gpe-bypass GigabitEthernet2/0/0 del}
1194 VLIB_CLI_COMMAND (set_interface_ip6_vxlan_gpe_bypass_command, static) = {
1195 .path = "set interface ip6 vxlan-gpe-bypass",
1196 .function = set_ip6_vxlan_gpe_bypass,
1197 .short_help = "set interface ip vxlan-gpe-bypass <interface> [del]",
1202 VNET_FEATURE_INIT (ip4_vxlan_gpe_bypass, static) =
1204 .arc_name = "ip4-unicast",
1205 .node_name = "ip4-vxlan-gpe-bypass",
1206 .runs_before = VNET_FEATURES ("ip4-lookup"),
1209 VNET_FEATURE_INIT (ip6_vxlan_gpe_bypass, static) =
1211 .arc_name = "ip6-unicast",
1212 .node_name = "ip6-vxlan-gpe-bypass",
1213 .runs_before = VNET_FEATURES ("ip6-lookup"),
1218 * @brief Feature init function for VXLAN GPE
1225 clib_error_t *vxlan_gpe_init (vlib_main_t *vm)
1227 vxlan_gpe_main_t *ngm = &vxlan_gpe_main;
1229 ngm->vnet_main = vnet_get_main();
1230 ngm->vlib_main = vm;
1232 ngm->vxlan4_gpe_tunnel_by_key
1233 = hash_create_mem (0, sizeof(vxlan4_gpe_tunnel_key_t), sizeof (uword));
1235 ngm->vxlan6_gpe_tunnel_by_key
1236 = hash_create_mem (0, sizeof(vxlan6_gpe_tunnel_key_t), sizeof (uword));
1239 ngm->mcast_shared = hash_create_mem(0,
1240 sizeof(ip46_address_t),
1241 sizeof(mcast_shared_t));
1243 udp_register_dst_port (vm, UDP_DST_PORT_VXLAN_GPE,
1244 vxlan4_gpe_input_node.index, 1 /* is_ip4 */);
1245 udp_register_dst_port (vm, UDP_DST_PORT_VXLAN6_GPE,
1246 vxlan6_gpe_input_node.index, 0 /* is_ip4 */);
1248 /* Register the list of standard decap protocols supported */
1249 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_IP4,
1250 VXLAN_GPE_INPUT_NEXT_IP4_INPUT);
1251 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_IP6,
1252 VXLAN_GPE_INPUT_NEXT_IP6_INPUT);
1253 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_ETHERNET,
1254 VXLAN_GPE_INPUT_NEXT_ETHERNET_INPUT);
1256 fib_node_register_type(FIB_NODE_TYPE_VXLAN_GPE_TUNNEL, &vxlan_gpe_vft);
1261 VLIB_INIT_FUNCTION(vxlan_gpe_init);