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.
15 #include <vnet/vxlan/vxlan.h>
16 #include <vnet/ip/format.h>
17 #include <vnet/fib/fib_entry.h>
18 #include <vnet/fib/fib_table.h>
19 #include <vnet/dpo/receive_dpo.h>
20 #include <vlib/vlib.h>
26 * VXLAN provides the features needed to allow L2 bridge domains (BDs)
27 * to span multiple servers. This is done by building an L2 overlay on
28 * top of an L3 network underlay using VXLAN tunnels.
30 * This makes it possible for servers to be co-located in the same data
31 * center or be separated geographically as long as they are reachable
32 * through the underlay L3 network.
34 * You can refer to this kind of L2 overlay bridge domain as a VXLAN
35 * (Virtual eXtensible VLAN) segment.
39 vxlan_main_t vxlan_main;
41 static u8 * format_decap_next (u8 * s, va_list * args)
43 u32 next_index = va_arg (*args, u32);
47 case VXLAN_INPUT_NEXT_DROP:
48 return format (s, "drop");
49 case VXLAN_INPUT_NEXT_L2_INPUT:
50 return format (s, "l2");
52 return format (s, "index %d", next_index);
57 u8 * format_vxlan_tunnel (u8 * s, va_list * args)
59 vxlan_tunnel_t * t = va_arg (*args, vxlan_tunnel_t *);
60 vxlan_main_t * ngm = &vxlan_main;
62 s = format (s, "[%d] src %U dst %U vni %d sw_if_index %d ",
64 format_ip46_address, &t->src, IP46_TYPE_ANY,
65 format_ip46_address, &t->dst, IP46_TYPE_ANY,
66 t->vni, t->sw_if_index);
68 if (ip46_address_is_multicast (&t->dst))
69 s = format (s, "mcast_sw_if_index %d ", t->mcast_sw_if_index);
71 s = format (s, "encap_fib_index %d fib_entry_index %d decap_next %U\n",
72 t->encap_fib_index, t->fib_entry_index,
73 format_decap_next, t->decap_next_index);
77 static u8 * format_vxlan_name (u8 * s, va_list * args)
79 u32 dev_instance = va_arg (*args, u32);
80 return format (s, "vxlan_tunnel%d", dev_instance);
83 static uword dummy_interface_tx (vlib_main_t * vm,
84 vlib_node_runtime_t * node,
87 clib_warning ("you shouldn't be here, leaking buffers...");
88 return frame->n_vectors;
92 vxlan_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
94 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
95 vnet_hw_interface_set_flags (vnm, hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP);
97 vnet_hw_interface_set_flags (vnm, hw_if_index, 0);
99 return /* no error */ 0;
102 VNET_DEVICE_CLASS (vxlan_device_class,static) = {
104 .format_device_name = format_vxlan_name,
105 .format_tx_trace = format_vxlan_encap_trace,
106 .tx_function = dummy_interface_tx,
107 .admin_up_down_function = vxlan_interface_admin_up_down,
110 static u8 * format_vxlan_header_with_length (u8 * s, va_list * args)
112 u32 dev_instance = va_arg (*args, u32);
113 s = format (s, "unimplemented dev %u", dev_instance);
117 VNET_HW_INTERFACE_CLASS (vxlan_hw_class) = {
119 .format_header = format_vxlan_header_with_length,
120 .build_rewrite = default_build_rewrite,
124 vxlan_tunnel_restack_dpo(vxlan_tunnel_t * t)
126 dpo_id_t dpo = DPO_INVALID;
127 u32 encap_index = ip46_address_is_ip4(&t->dst) ?
128 vxlan4_encap_node.index : vxlan6_encap_node.index;
129 fib_forward_chain_type_t forw_type = ip46_address_is_ip4(&t->dst) ?
130 FIB_FORW_CHAIN_TYPE_UNICAST_IP4 : FIB_FORW_CHAIN_TYPE_UNICAST_IP6;
132 fib_entry_contribute_forwarding (t->fib_entry_index, forw_type, &dpo);
133 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
137 static vxlan_tunnel_t *
138 vxlan_tunnel_from_fib_node (fib_node_t *node)
141 ASSERT(FIB_NODE_TYPE_VXLAN_TUNNEL == node->fn_type);
143 return ((vxlan_tunnel_t*) (((char*)node) -
144 STRUCT_OFFSET_OF(vxlan_tunnel_t, node)));
148 * Function definition to backwalk a FIB node -
149 * Here we will restack the new dpo of VXLAN DIP to encap node.
151 static fib_node_back_walk_rc_t
152 vxlan_tunnel_back_walk (fib_node_t *node,
153 fib_node_back_walk_ctx_t *ctx)
155 vxlan_tunnel_restack_dpo(vxlan_tunnel_from_fib_node(node));
156 return (FIB_NODE_BACK_WALK_CONTINUE);
160 * Function definition to get a FIB node from its index
163 vxlan_tunnel_fib_node_get (fib_node_index_t index)
166 vxlan_main_t * vxm = &vxlan_main;
168 t = pool_elt_at_index(vxm->tunnels, index);
174 * Function definition to inform the FIB node that its last lock has gone.
177 vxlan_tunnel_last_lock_gone (fib_node_t *node)
180 * The VXLAN tunnel is a root of the graph. As such
181 * it never has children and thus is never locked.
187 * Virtual function table registered by VXLAN tunnels
188 * for participation in the FIB object graph.
190 const static fib_node_vft_t vxlan_vft = {
191 .fnv_get = vxlan_tunnel_fib_node_get,
192 .fnv_last_lock = vxlan_tunnel_last_lock_gone,
193 .fnv_back_walk = vxlan_tunnel_back_walk,
197 #define foreach_copy_field \
199 _(mcast_sw_if_index) \
201 _(decap_next_index) \
205 static int vxlan4_rewrite (vxlan_tunnel_t * t)
209 ip4_vxlan_header_t * h0;
210 int len = sizeof (*h0);
212 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
214 h0 = (ip4_vxlan_header_t *) rw;
216 /* Fixed portion of the (outer) ip4 header */
218 ip0->ip_version_and_header_length = 0x45;
220 ip0->protocol = IP_PROTOCOL_UDP;
222 /* we fix up the ip4 header length and checksum after-the-fact */
223 ip0->src_address.as_u32 = t->src.ip4.as_u32;
224 ip0->dst_address.as_u32 = t->dst.ip4.as_u32;
225 ip0->checksum = ip4_header_checksum (ip0);
227 /* UDP header, randomize src port on something, maybe? */
228 h0->udp.src_port = clib_host_to_net_u16 (4789);
229 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan);
232 vnet_set_vni_and_flags(&h0->vxlan, t->vni);
238 static int vxlan6_rewrite (vxlan_tunnel_t * t)
242 ip6_vxlan_header_t * h0;
243 int len = sizeof (*h0);
245 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
247 h0 = (ip6_vxlan_header_t *) rw;
249 /* Fixed portion of the (outer) ip6 header */
251 ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32(6 << 28);
252 ip0->hop_limit = 255;
253 ip0->protocol = IP_PROTOCOL_UDP;
255 ip0->src_address.as_u64[0] = t->src.ip6.as_u64[0];
256 ip0->src_address.as_u64[1] = t->src.ip6.as_u64[1];
257 ip0->dst_address.as_u64[0] = t->dst.ip6.as_u64[0];
258 ip0->dst_address.as_u64[1] = t->dst.ip6.as_u64[1];
260 /* UDP header, randomize src port on something, maybe? */
261 h0->udp.src_port = clib_host_to_net_u16 (4789);
262 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan);
265 vnet_set_vni_and_flags(&h0->vxlan, t->vni);
271 static int vxlan_check_decap_next(vxlan_main_t * vxm, u32 is_ip6, u32 decap_next_index)
273 vlib_main_t * vm = vxm->vlib_main;
274 vlib_node_runtime_t *r;
278 r = vlib_node_get_runtime (vm, vxlan4_input_node.index);
279 if(decap_next_index >= r->n_next_nodes)
284 r = vlib_node_get_runtime (vm, vxlan6_input_node.index);
285 if(decap_next_index >= r->n_next_nodes)
293 vtep_addr_ref(ip46_address_t *ip)
295 if (!ip46_address_is_ip4(ip))
296 return 1; /* always create */
297 uword *pvtep = hash_get (vxlan_main.vtep4, ip->ip4.as_u32);
300 hash_set (vxlan_main.vtep4, ip->ip4.as_u32, 1);
305 vtep_addr_unref(ip46_address_t *ip)
307 if (!ip46_address_is_ip4(ip))
308 return 0; /* alwways destroy */
309 uword *pvtep = hash_get (vxlan_main.vtep4, ip->ip4.as_u32);
312 hash_unset (vxlan_main.vtep4, ip->ip4.as_u32);
318 mcast_ep_get(ip46_address_t * ip)
320 ASSERT(ip46_address_is_multicast(ip));
321 uword * ep_idx = hash_get_mem (vxlan_main.mcast_ep_by_ip, ip);
323 return pool_elt_at_index(vxlan_main.mcast_eps, *ep_idx);
327 mcast_ep_add(mcast_remote_t * new_ep)
331 pool_get_aligned (vxlan_main.mcast_eps, ep, CLIB_CACHE_LINE_BYTES);
333 hash_set_mem (vxlan_main.mcast_ep_by_ip, &ep->ip, ep - vxlan_main.mcast_eps);
337 mcast_ep_remove(mcast_remote_t * ep)
339 hash_unset_mem (vxlan_main.mcast_ep_by_ip, &ep->ip);
340 pool_put (vxlan_main.mcast_eps, ep);
344 ip46_multicast_ethernet_address(u8 * ethernet_address, ip46_address_t * ip) {
345 if (ip46_address_is_ip4(ip))
346 ip4_multicast_ethernet_address(ethernet_address, &ip->ip4);
348 ip6_multicast_ethernet_address(ethernet_address, ip->ip6.as_u32[0]);
351 int vnet_vxlan_add_del_tunnel
352 (vnet_vxlan_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
354 vxlan_main_t * vxm = &vxlan_main;
355 vxlan_tunnel_t *t = 0;
356 vnet_main_t * vnm = vxm->vnet_main;
358 u32 hw_if_index = ~0;
359 u32 sw_if_index = ~0;
361 vxlan4_tunnel_key_t key4;
362 vxlan6_tunnel_key_t key6;
363 u32 is_ip6 = a->is_ip6;
367 key4.src = a->dst.ip4.as_u32; /* decap src in key is encap dst in config */
368 key4.vni = clib_host_to_net_u32 (a->vni << 8);
369 p = hash_get (vxm->vxlan4_tunnel_by_key, key4.as_u64);
373 key6.src.as_u64[0] = a->dst.ip6.as_u64[0];
374 key6.src.as_u64[1] = a->dst.ip6.as_u64[1];
375 key6.vni = clib_host_to_net_u32 (a->vni << 8);
376 p = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6);
381 l2input_main_t * l2im = &l2input_main;
383 /* adding a tunnel: tunnel must not already exist */
385 return VNET_API_ERROR_TUNNEL_EXIST;
387 /*if not set explicitly, default to l2 */
388 if(a->decap_next_index == ~0)
389 a->decap_next_index = VXLAN_INPUT_NEXT_L2_INPUT;
390 if (vxlan_check_decap_next(vxm, is_ip6, a->decap_next_index))
391 return VNET_API_ERROR_INVALID_DECAP_NEXT;
393 pool_get_aligned (vxm->tunnels, t, CLIB_CACHE_LINE_BYTES);
394 memset (t, 0, sizeof (*t));
396 /* copy from arg structure */
397 #define _(x) t->x = a->x;
404 t->key6 = clib_mem_alloc (sizeof(vxlan6_tunnel_key_t));
405 clib_memcpy (t->key6, &key6, sizeof(key6));
409 t->key4 = 0; /* not yet used */
413 rv = vxlan4_rewrite (t);
415 rv = vxlan6_rewrite (t);
419 pool_put (vxm->tunnels, t);
424 hash_set (vxm->vxlan4_tunnel_by_key, key4.as_u64, t - vxm->tunnels);
426 hash_set_mem (vxm->vxlan6_tunnel_by_key, t->key6, t - vxm->tunnels);
428 vnet_hw_interface_t * hi;
429 if (vec_len (vxm->free_vxlan_tunnel_hw_if_indices) > 0)
431 vnet_interface_main_t * im = &vnm->interface_main;
432 hw_if_index = vxm->free_vxlan_tunnel_hw_if_indices
433 [vec_len (vxm->free_vxlan_tunnel_hw_if_indices)-1];
434 _vec_len (vxm->free_vxlan_tunnel_hw_if_indices) -= 1;
436 hi = vnet_get_hw_interface (vnm, hw_if_index);
437 hi->dev_instance = t - vxm->tunnels;
438 hi->hw_instance = hi->dev_instance;
440 /* clear old stats of freed tunnel before reuse */
441 sw_if_index = hi->sw_if_index;
442 vnet_interface_counter_lock(im);
443 vlib_zero_combined_counter
444 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
445 vlib_zero_combined_counter
446 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
447 vlib_zero_simple_counter
448 (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
449 vnet_interface_counter_unlock(im);
453 hw_if_index = vnet_register_interface
454 (vnm, vxlan_device_class.index, t - vxm->tunnels,
455 vxlan_hw_class.index, t - vxm->tunnels);
456 hi = vnet_get_hw_interface (vnm, hw_if_index);
459 t->hw_if_index = hw_if_index;
460 t->sw_if_index = sw_if_index = hi->sw_if_index;
462 vec_validate_init_empty (vxm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
463 vxm->tunnel_index_by_sw_if_index[sw_if_index] = t - vxm->tunnels;
465 /* setup l2 input config with l2 feature and bd 0 to drop packet */
466 vec_validate (l2im->configs, sw_if_index);
467 l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
468 l2im->configs[sw_if_index].bd_index = 0;
470 vnet_sw_interface_set_flags (vnm, sw_if_index,
471 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
472 fib_node_init(&t->node, FIB_NODE_TYPE_VXLAN_TUNNEL);
473 fib_prefix_t tun_dst_pfx;
474 u32 encap_index = !is_ip6 ?
475 vxlan4_encap_node.index : vxlan6_encap_node.index;
476 vnet_flood_class_t flood_class = VNET_FLOOD_CLASS_TUNNEL_NORMAL;
478 fib_prefix_from_ip46_addr(&t->dst, &tun_dst_pfx);
479 if (!ip46_address_is_multicast(&t->dst))
482 * source the FIB entry for the tunnel's destination
483 * and become a child thereof. The tunnel will then get poked
484 * when the forwarding for the entry updates, and the tunnel can
485 * re-stack accordingly
487 vtep_addr_ref(&t->src);
488 t->fib_entry_index = fib_table_entry_special_add
489 (t->encap_fib_index, &tun_dst_pfx, FIB_SOURCE_RR,
490 FIB_ENTRY_FLAG_NONE, ADJ_INDEX_INVALID);
491 t->sibling_index = fib_entry_child_add
492 (t->fib_entry_index, FIB_NODE_TYPE_VXLAN_TUNNEL, t - vxm->tunnels);
493 vxlan_tunnel_restack_dpo(t);
497 /* Multicast tunnel -
498 * as the same mcast group can be used for mutiple mcast tunnels
499 * with different VNIs, create the output fib adjecency only if
500 * it does not already exist
502 fib_protocol_t fp = (is_ip6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
503 dpo_id_t dpo = DPO_INVALID;
504 dpo_proto_t dproto = fib_proto_to_dpo(fp);
506 if (vtep_addr_ref(&t->dst) == 1)
510 ip46_multicast_ethernet_address(mcast_mac, &t->dst);
511 receive_dpo_add_or_lock(dproto, ~0, NULL, &dpo);
512 mcast_remote_t new_ep = {
514 .mcast_adj_index = adj_rewrite_add_and_lock
515 (fp, fib_proto_to_link(fp), a->mcast_sw_if_index, mcast_mac),
516 /* Add VRF local mcast adj. */
517 .fib_entry_index = fib_table_entry_special_dpo_add
518 (t->encap_fib_index, &tun_dst_pfx,
519 FIB_SOURCE_SPECIAL, FIB_ENTRY_FLAG_NONE, &dpo)
521 mcast_ep_add(&new_ep);
524 /* Stack shared mcast dst mac addr rewrite on encap */
525 dpo_set (&dpo, DPO_ADJACENCY, dproto,
526 mcast_ep_get(&t->dst)->mcast_adj_index);
527 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
529 flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER;
532 /* Set vxlan tunnel output node */
533 hi->output_node_index = encap_index;
535 vnet_get_sw_interface (vnet_get_main(), sw_if_index)->flood_class = flood_class;
539 /* deleting a tunnel: tunnel must exist */
541 return VNET_API_ERROR_NO_SUCH_ENTRY;
543 t = pool_elt_at_index (vxm->tunnels, p[0]);
545 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
546 /* make sure tunnel is removed from l2 bd or xconnect */
547 set_int_l2_mode(vxm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0, 0);
548 vec_add1 (vxm->free_vxlan_tunnel_hw_if_indices, t->hw_if_index);
550 vxm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
553 hash_unset (vxm->vxlan4_tunnel_by_key, key4.as_u64);
556 hash_unset_mem (vxm->vxlan6_tunnel_by_key, t->key6);
557 clib_mem_free (t->key6);
560 if (!ip46_address_is_multicast(&t->dst))
562 vtep_addr_unref(&a->src);
563 fib_entry_child_remove(t->fib_entry_index, t->sibling_index);
564 fib_table_entry_delete_index(t->fib_entry_index, FIB_SOURCE_RR);
566 else if (vtep_addr_unref(&t->dst) == 0)
568 mcast_remote_t* ep = mcast_ep_get(&t->dst);
569 adj_unlock(ep->mcast_adj_index);
570 fib_table_entry_delete_index(ep->fib_entry_index, FIB_SOURCE_SPECIAL);
574 fib_node_deinit(&t->node);
575 vec_free (t->rewrite);
576 pool_put (vxm->tunnels, t);
580 *sw_if_indexp = sw_if_index;
585 static u32 fib4_index_from_fib_id (u32 fib_id)
587 ip4_main_t * im = &ip4_main;
590 p = hash_get (im->fib_index_by_table_id, fib_id);
597 static u32 fib6_index_from_fib_id (u32 fib_id)
599 ip6_main_t * im = &ip6_main;
602 p = hash_get (im->fib_index_by_table_id, fib_id);
609 static uword get_decap_next_for_node(u32 node_index, u32 ipv4_set)
611 vxlan_main_t * vxm = &vxlan_main;
612 vlib_main_t * vm = vxm->vlib_main;
613 uword next_index = ~0;
617 next_index = vlib_node_add_next (vm, vxlan4_input_node.index, node_index);
621 next_index = vlib_node_add_next (vm, vxlan6_input_node.index, node_index);
627 static uword unformat_decap_next (unformat_input_t * input, va_list * args)
629 u32 * result = va_arg (*args, u32 *);
630 u32 ipv4_set = va_arg (*args, int);
631 vxlan_main_t * vxm = &vxlan_main;
632 vlib_main_t * vm = vxm->vlib_main;
636 if (unformat (input, "l2"))
637 *result = VXLAN_INPUT_NEXT_L2_INPUT;
638 else if (unformat (input, "node %U", unformat_vlib_node, vm, &node_index))
639 *result = get_decap_next_for_node(node_index, ipv4_set);
640 else if (unformat (input, "%d", &tmp))
647 static clib_error_t *
648 vxlan_add_del_tunnel_command_fn (vlib_main_t * vm,
649 unformat_input_t * input,
650 vlib_cli_command_t * cmd)
652 unformat_input_t _line_input, * line_input = &_line_input;
653 ip46_address_t src , dst;
660 u32 encap_fib_index = 0;
661 u32 mcast_sw_if_index = ~0;
662 u32 decap_next_index = VXLAN_INPUT_NEXT_L2_INPUT;
666 vnet_vxlan_add_del_tunnel_args_t _a, * a = &_a;
667 u32 tunnel_sw_if_index;
669 /* Cant "universally zero init" (={0}) due to GCC bug 53119 */
670 memset(&src, 0, sizeof src);
671 memset(&dst, 0, sizeof dst);
673 /* Get a line of input. */
674 if (! unformat_user (input, unformat_line_input, line_input))
677 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
678 if (unformat (line_input, "del"))
682 else if (unformat (line_input, "src %U",
683 unformat_ip4_address, &src.ip4))
688 else if (unformat (line_input, "dst %U",
689 unformat_ip4_address, &dst.ip4))
694 else if (unformat (line_input, "src %U",
695 unformat_ip6_address, &src.ip6))
700 else if (unformat (line_input, "dst %U",
701 unformat_ip6_address, &dst.ip6))
706 else if (unformat (line_input, "group %U %U",
707 unformat_ip4_address, &dst.ip4,
708 unformat_vnet_sw_interface,
709 vnet_get_main(), &mcast_sw_if_index))
711 grp_set = dst_set = 1;
714 else if (unformat (line_input, "group %U %U",
715 unformat_ip6_address, &dst.ip6,
716 unformat_vnet_sw_interface,
717 vnet_get_main(), &mcast_sw_if_index))
719 grp_set = dst_set = 1;
722 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
725 encap_fib_index = fib6_index_from_fib_id (tmp);
727 encap_fib_index = fib4_index_from_fib_id (tmp);
728 if (encap_fib_index == ~0)
729 return clib_error_return (0, "nonexistent encap-vrf-id %d", tmp);
731 else if (unformat (line_input, "decap-next %U", unformat_decap_next,
732 &decap_next_index, ipv4_set))
734 else if (unformat (line_input, "vni %d", &vni))
737 return clib_error_return (0, "vni %d out of range", vni);
740 return clib_error_return (0, "parse error: '%U'",
741 format_unformat_error, line_input);
744 unformat_free (line_input);
747 return clib_error_return (0, "tunnel src address not specified");
750 return clib_error_return (0, "tunnel dst address not specified");
752 if (grp_set && !ip46_address_is_multicast(&dst))
753 return clib_error_return (0, "tunnel group address not multicast");
755 if (grp_set == 0 && ip46_address_is_multicast(&dst))
756 return clib_error_return (0, "dst address must be unicast");
758 if (grp_set && mcast_sw_if_index == ~0)
759 return clib_error_return (0, "tunnel nonexistent multicast device");
761 if (ipv4_set && ipv6_set)
762 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
764 if (ip46_address_cmp(&src, &dst) == 0)
765 return clib_error_return (0, "src and dst addresses are identical");
767 if (decap_next_index == ~0)
768 return clib_error_return (0, "next node not found");
771 return clib_error_return (0, "vni not specified");
773 memset (a, 0, sizeof (*a));
776 a->is_ip6 = ipv6_set;
778 #define _(x) a->x = x;
782 rv = vnet_vxlan_add_del_tunnel (a, &tunnel_sw_if_index);
788 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name,
789 vnet_get_main(), tunnel_sw_if_index);
792 case VNET_API_ERROR_TUNNEL_EXIST:
793 return clib_error_return (0, "tunnel already exists...");
795 case VNET_API_ERROR_NO_SUCH_ENTRY:
796 return clib_error_return (0, "tunnel does not exist...");
799 return clib_error_return
800 (0, "vnet_vxlan_add_del_tunnel returned %d", rv);
807 * Add or delete a VXLAN Tunnel.
809 * VXLAN provides the features needed to allow L2 bridge domains (BDs)
810 * to span multiple servers. This is done by building an L2 overlay on
811 * top of an L3 network underlay using VXLAN tunnels.
813 * This makes it possible for servers to be co-located in the same data
814 * center or be separated geographically as long as they are reachable
815 * through the underlay L3 network.
817 * You can refer to this kind of L2 overlay bridge domain as a VXLAN
818 * (Virtual eXtensible VLAN) segment.
821 * Example of how to create a VXLAN Tunnel:
822 * @cliexcmd{create vxlan tunnel src 10.0.3.1 dst 10.0.3.3 vni 13 encap-vrf-id 7}
823 * Example of how to delete a VXLAN Tunnel:
824 * @cliexcmd{create vxlan tunnel src 10.0.3.1 dst 10.0.3.3 vni 13 del}
827 VLIB_CLI_COMMAND (create_vxlan_tunnel_command, static) = {
828 .path = "create vxlan tunnel",
830 "create vxlan tunnel src <local-vtep-addr>"
831 " {dst <remote-vtep-addr>|group <mcast-vtep-addr> <intf-name>} vni <nn>"
832 " [encap-vrf-id <nn>] [decap-next [l2|node <name>]] [del]",
833 .function = vxlan_add_del_tunnel_command_fn,
837 static clib_error_t *
838 show_vxlan_tunnel_command_fn (vlib_main_t * vm,
839 unformat_input_t * input,
840 vlib_cli_command_t * cmd)
842 vxlan_main_t * vxm = &vxlan_main;
845 if (pool_elts (vxm->tunnels) == 0)
846 vlib_cli_output (vm, "No vxlan tunnels configured...");
848 pool_foreach (t, vxm->tunnels,
850 vlib_cli_output (vm, "%U", format_vxlan_tunnel, t);
857 * Display all the VXLAN Tunnel entries.
860 * Example of how to display the VXLAN Tunnel entries:
861 * @cliexstart{show vxlan tunnel}
862 * [0] src 10.0.3.1 dst 10.0.3.3 vni 13 encap_fib_index 0 sw_if_index 5 decap_next l2
866 VLIB_CLI_COMMAND (show_vxlan_tunnel_command, static) = {
867 .path = "show vxlan tunnel",
868 .short_help = "show vxlan tunnel",
869 .function = show_vxlan_tunnel_command_fn,
874 clib_error_t *vxlan_init (vlib_main_t *vm)
876 vxlan_main_t * vxm = &vxlan_main;
878 vxm->vnet_main = vnet_get_main();
881 /* initialize the ip6 hash */
882 vxm->vxlan6_tunnel_by_key = hash_create_mem(0,
883 sizeof(vxlan6_tunnel_key_t),
885 vxm->mcast_ep_by_ip = hash_create_mem(0,
886 sizeof(ip46_address_t),
889 udp_register_dst_port (vm, UDP_DST_PORT_vxlan,
890 vxlan4_input_node.index, /* is_ip4 */ 1);
891 udp_register_dst_port (vm, UDP_DST_PORT_vxlan6,
892 vxlan6_input_node.index, /* is_ip4 */ 0);
894 fib_node_register_type(FIB_NODE_TYPE_VXLAN_TUNNEL, &vxlan_vft);
899 VLIB_INIT_FUNCTION(vxlan_init);