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/mfib/mfib_table.h>
20 #include <vnet/adj/adj_mcast.h>
21 #include <vlib/vlib.h>
27 * VXLAN provides the features needed to allow L2 bridge domains (BDs)
28 * to span multiple servers. This is done by building an L2 overlay on
29 * top of an L3 network underlay using VXLAN tunnels.
31 * This makes it possible for servers to be co-located in the same data
32 * center or be separated geographically as long as they are reachable
33 * through the underlay L3 network.
35 * You can refer to this kind of L2 overlay bridge domain as a VXLAN
36 * (Virtual eXtensible VLAN) segment.
40 vxlan_main_t vxlan_main;
42 static u8 * format_decap_next (u8 * s, va_list * args)
44 u32 next_index = va_arg (*args, u32);
48 case VXLAN_INPUT_NEXT_DROP:
49 return format (s, "drop");
50 case VXLAN_INPUT_NEXT_L2_INPUT:
51 return format (s, "l2");
53 return format (s, "index %d", next_index);
58 u8 * format_vxlan_tunnel (u8 * s, va_list * args)
60 vxlan_tunnel_t * t = va_arg (*args, vxlan_tunnel_t *);
61 vxlan_main_t * ngm = &vxlan_main;
63 s = format (s, "[%d] src %U dst %U vni %d sw_if_index %d ",
65 format_ip46_address, &t->src, IP46_TYPE_ANY,
66 format_ip46_address, &t->dst, IP46_TYPE_ANY,
67 t->vni, t->sw_if_index);
69 if (ip46_address_is_multicast (&t->dst))
70 s = format (s, "mcast_sw_if_index %d ", t->mcast_sw_if_index);
72 s = format (s, "encap_fib_index %d fib_entry_index %d decap_next %U\n",
73 t->encap_fib_index, t->fib_entry_index,
74 format_decap_next, t->decap_next_index);
78 static u8 * format_vxlan_name (u8 * s, va_list * args)
80 u32 dev_instance = va_arg (*args, u32);
81 return format (s, "vxlan_tunnel%d", dev_instance);
84 static uword dummy_interface_tx (vlib_main_t * vm,
85 vlib_node_runtime_t * node,
88 clib_warning ("you shouldn't be here, leaking buffers...");
89 return frame->n_vectors;
93 vxlan_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
95 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
96 vnet_hw_interface_set_flags (vnm, hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP);
98 vnet_hw_interface_set_flags (vnm, hw_if_index, 0);
100 return /* no error */ 0;
103 VNET_DEVICE_CLASS (vxlan_device_class,static) = {
105 .format_device_name = format_vxlan_name,
106 .format_tx_trace = format_vxlan_encap_trace,
107 .tx_function = dummy_interface_tx,
108 .admin_up_down_function = vxlan_interface_admin_up_down,
111 static u8 * format_vxlan_header_with_length (u8 * s, va_list * args)
113 u32 dev_instance = va_arg (*args, u32);
114 s = format (s, "unimplemented dev %u", dev_instance);
118 VNET_HW_INTERFACE_CLASS (vxlan_hw_class) = {
120 .format_header = format_vxlan_header_with_length,
121 .build_rewrite = default_build_rewrite,
125 vxlan_tunnel_restack_dpo(vxlan_tunnel_t * t)
127 dpo_id_t dpo = DPO_INVALID;
128 u32 encap_index = ip46_address_is_ip4(&t->dst) ?
129 vxlan4_encap_node.index : vxlan6_encap_node.index;
130 fib_forward_chain_type_t forw_type = ip46_address_is_ip4(&t->dst) ?
131 FIB_FORW_CHAIN_TYPE_UNICAST_IP4 : FIB_FORW_CHAIN_TYPE_UNICAST_IP6;
133 fib_entry_contribute_forwarding (t->fib_entry_index, forw_type, &dpo);
134 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
138 static vxlan_tunnel_t *
139 vxlan_tunnel_from_fib_node (fib_node_t *node)
142 ASSERT(FIB_NODE_TYPE_VXLAN_TUNNEL == node->fn_type);
144 return ((vxlan_tunnel_t*) (((char*)node) -
145 STRUCT_OFFSET_OF(vxlan_tunnel_t, node)));
149 * Function definition to backwalk a FIB node -
150 * Here we will restack the new dpo of VXLAN DIP to encap node.
152 static fib_node_back_walk_rc_t
153 vxlan_tunnel_back_walk (fib_node_t *node,
154 fib_node_back_walk_ctx_t *ctx)
156 vxlan_tunnel_restack_dpo(vxlan_tunnel_from_fib_node(node));
157 return (FIB_NODE_BACK_WALK_CONTINUE);
161 * Function definition to get a FIB node from its index
164 vxlan_tunnel_fib_node_get (fib_node_index_t index)
167 vxlan_main_t * vxm = &vxlan_main;
169 t = pool_elt_at_index(vxm->tunnels, index);
175 * Function definition to inform the FIB node that its last lock has gone.
178 vxlan_tunnel_last_lock_gone (fib_node_t *node)
181 * The VXLAN tunnel is a root of the graph. As such
182 * it never has children and thus is never locked.
188 * Virtual function table registered by VXLAN tunnels
189 * for participation in the FIB object graph.
191 const static fib_node_vft_t vxlan_vft = {
192 .fnv_get = vxlan_tunnel_fib_node_get,
193 .fnv_last_lock = vxlan_tunnel_last_lock_gone,
194 .fnv_back_walk = vxlan_tunnel_back_walk,
198 #define foreach_copy_field \
200 _(mcast_sw_if_index) \
202 _(decap_next_index) \
206 static int vxlan4_rewrite (vxlan_tunnel_t * t)
210 ip4_vxlan_header_t * h0;
211 int len = sizeof (*h0);
213 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
215 h0 = (ip4_vxlan_header_t *) rw;
217 /* Fixed portion of the (outer) ip4 header */
219 ip0->ip_version_and_header_length = 0x45;
221 ip0->protocol = IP_PROTOCOL_UDP;
223 /* we fix up the ip4 header length and checksum after-the-fact */
224 ip0->src_address.as_u32 = t->src.ip4.as_u32;
225 ip0->dst_address.as_u32 = t->dst.ip4.as_u32;
226 ip0->checksum = ip4_header_checksum (ip0);
228 /* UDP header, randomize src port on something, maybe? */
229 h0->udp.src_port = clib_host_to_net_u16 (4789);
230 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan);
233 vnet_set_vni_and_flags(&h0->vxlan, t->vni);
239 static int vxlan6_rewrite (vxlan_tunnel_t * t)
243 ip6_vxlan_header_t * h0;
244 int len = sizeof (*h0);
246 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
248 h0 = (ip6_vxlan_header_t *) rw;
250 /* Fixed portion of the (outer) ip6 header */
252 ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32(6 << 28);
253 ip0->hop_limit = 255;
254 ip0->protocol = IP_PROTOCOL_UDP;
256 ip0->src_address.as_u64[0] = t->src.ip6.as_u64[0];
257 ip0->src_address.as_u64[1] = t->src.ip6.as_u64[1];
258 ip0->dst_address.as_u64[0] = t->dst.ip6.as_u64[0];
259 ip0->dst_address.as_u64[1] = t->dst.ip6.as_u64[1];
261 /* UDP header, randomize src port on something, maybe? */
262 h0->udp.src_port = clib_host_to_net_u16 (4789);
263 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan);
266 vnet_set_vni_and_flags(&h0->vxlan, t->vni);
272 static int vxlan_check_decap_next(vxlan_main_t * vxm, u32 is_ip6, u32 decap_next_index)
274 vlib_main_t * vm = vxm->vlib_main;
275 vlib_node_runtime_t *r;
279 r = vlib_node_get_runtime (vm, vxlan4_input_node.index);
280 if(decap_next_index >= r->n_next_nodes)
285 r = vlib_node_get_runtime (vm, vxlan6_input_node.index);
286 if(decap_next_index >= r->n_next_nodes)
294 hash_set_key_copy (uword ** h, void * key, uword v) {
295 size_t ksz = hash_header(*h)->user;
296 void * copy = clib_mem_alloc (ksz);
297 clib_memcpy (copy, key, ksz);
298 hash_set_mem (*h, copy, v);
302 hash_unset_key_free (uword ** h, void * key) {
303 hash_pair_t * hp = hash_get_pair_mem (*h, key);
305 key = uword_to_pointer (hp->key, void *);
306 hash_unset_mem (*h, key);
311 vtep_addr_ref(ip46_address_t *ip)
313 uword *vtep = ip46_address_is_ip4(ip) ?
314 hash_get (vxlan_main.vtep4, ip->ip4.as_u32) :
315 hash_get_mem (vxlan_main.vtep6, &ip->ip6);
318 ip46_address_is_ip4(ip) ?
319 hash_set (vxlan_main.vtep4, ip->ip4.as_u32, 1) :
320 hash_set_key_copy (&vxlan_main.vtep6, &ip->ip6, 1);
325 vtep_addr_unref(ip46_address_t *ip)
327 uword *vtep = ip46_address_is_ip4(ip) ?
328 hash_get (vxlan_main.vtep4, ip->ip4.as_u32) :
329 hash_get_mem (vxlan_main.vtep6, &ip->ip6);
333 ip46_address_is_ip4(ip) ?
334 hash_unset (vxlan_main.vtep4, ip->ip4.as_u32) :
335 hash_unset_key_free (&vxlan_main.vtep6, &ip->ip6);
339 typedef CLIB_PACKED(union {
341 fib_node_index_t mfib_entry_index;
342 adj_index_t mcast_adj_index;
347 static inline mcast_shared_t
348 mcast_shared_get(ip46_address_t * ip)
350 ASSERT(ip46_address_is_multicast(ip));
351 uword * p = hash_get_mem (vxlan_main.mcast_shared, ip);
353 return (mcast_shared_t) { .as_u64 = *p };
357 mcast_shared_add(ip46_address_t *dst,
358 fib_node_index_t mfei,
361 mcast_shared_t new_ep = {
362 .mcast_adj_index = ai,
363 .mfib_entry_index = mfei,
366 hash_set_key_copy (&vxlan_main.mcast_shared, dst, new_ep.as_u64);
370 mcast_shared_remove(ip46_address_t *dst)
372 mcast_shared_t ep = mcast_shared_get(dst);
374 adj_unlock(ep.mcast_adj_index);
375 mfib_table_entry_delete_index(ep.mfib_entry_index,
378 hash_unset_key_free (&vxlan_main.mcast_shared, dst);
381 int vnet_vxlan_add_del_tunnel
382 (vnet_vxlan_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
384 vxlan_main_t * vxm = &vxlan_main;
385 vxlan_tunnel_t *t = 0;
386 vnet_main_t * vnm = vxm->vnet_main;
388 u32 hw_if_index = ~0;
389 u32 sw_if_index = ~0;
391 vxlan4_tunnel_key_t key4;
392 vxlan6_tunnel_key_t key6;
393 u32 is_ip6 = a->is_ip6;
397 key4.src = a->dst.ip4.as_u32; /* decap src in key is encap dst in config */
398 key4.vni = clib_host_to_net_u32 (a->vni << 8);
399 p = hash_get (vxm->vxlan4_tunnel_by_key, key4.as_u64);
403 key6.src.as_u64[0] = a->dst.ip6.as_u64[0];
404 key6.src.as_u64[1] = a->dst.ip6.as_u64[1];
405 key6.vni = clib_host_to_net_u32 (a->vni << 8);
406 p = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6);
411 l2input_main_t * l2im = &l2input_main;
413 /* adding a tunnel: tunnel must not already exist */
415 return VNET_API_ERROR_TUNNEL_EXIST;
417 /*if not set explicitly, default to l2 */
418 if(a->decap_next_index == ~0)
419 a->decap_next_index = VXLAN_INPUT_NEXT_L2_INPUT;
420 if (vxlan_check_decap_next(vxm, is_ip6, a->decap_next_index))
421 return VNET_API_ERROR_INVALID_DECAP_NEXT;
423 pool_get_aligned (vxm->tunnels, t, CLIB_CACHE_LINE_BYTES);
424 memset (t, 0, sizeof (*t));
426 /* copy from arg structure */
427 #define _(x) t->x = a->x;
432 rv = vxlan4_rewrite (t);
434 rv = vxlan6_rewrite (t);
438 pool_put (vxm->tunnels, t);
444 hash_set_key_copy (&vxm->vxlan6_tunnel_by_key, &key6, t - vxm->tunnels);
446 hash_set (vxm->vxlan4_tunnel_by_key, key4.as_u64, t - vxm->tunnels);
448 vnet_hw_interface_t * hi;
449 if (vec_len (vxm->free_vxlan_tunnel_hw_if_indices) > 0)
451 vnet_interface_main_t * im = &vnm->interface_main;
452 hw_if_index = vxm->free_vxlan_tunnel_hw_if_indices
453 [vec_len (vxm->free_vxlan_tunnel_hw_if_indices)-1];
454 _vec_len (vxm->free_vxlan_tunnel_hw_if_indices) -= 1;
456 hi = vnet_get_hw_interface (vnm, hw_if_index);
457 hi->dev_instance = t - vxm->tunnels;
458 hi->hw_instance = hi->dev_instance;
460 /* clear old stats of freed tunnel before reuse */
461 sw_if_index = hi->sw_if_index;
462 vnet_interface_counter_lock(im);
463 vlib_zero_combined_counter
464 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
465 vlib_zero_combined_counter
466 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
467 vlib_zero_simple_counter
468 (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
469 vnet_interface_counter_unlock(im);
473 hw_if_index = vnet_register_interface
474 (vnm, vxlan_device_class.index, t - vxm->tunnels,
475 vxlan_hw_class.index, t - vxm->tunnels);
476 hi = vnet_get_hw_interface (vnm, hw_if_index);
479 t->hw_if_index = hw_if_index;
480 t->sw_if_index = sw_if_index = hi->sw_if_index;
482 vec_validate_init_empty (vxm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
483 vxm->tunnel_index_by_sw_if_index[sw_if_index] = t - vxm->tunnels;
485 /* setup l2 input config with l2 feature and bd 0 to drop packet */
486 vec_validate (l2im->configs, sw_if_index);
487 l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
488 l2im->configs[sw_if_index].bd_index = 0;
490 vnet_sw_interface_set_flags (vnm, sw_if_index,
491 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
492 fib_node_init(&t->node, FIB_NODE_TYPE_VXLAN_TUNNEL);
493 fib_prefix_t tun_dst_pfx;
494 u32 encap_index = !is_ip6 ?
495 vxlan4_encap_node.index : vxlan6_encap_node.index;
496 vnet_flood_class_t flood_class = VNET_FLOOD_CLASS_TUNNEL_NORMAL;
498 fib_prefix_from_ip46_addr(&t->dst, &tun_dst_pfx);
499 if (!ip46_address_is_multicast(&t->dst))
502 * source the FIB entry for the tunnel's destination
503 * and become a child thereof. The tunnel will then get poked
504 * when the forwarding for the entry updates, and the tunnel can
505 * re-stack accordingly
507 vtep_addr_ref(&t->src);
508 t->fib_entry_index = fib_table_entry_special_add
509 (t->encap_fib_index, &tun_dst_pfx, FIB_SOURCE_RR,
510 FIB_ENTRY_FLAG_NONE, ADJ_INDEX_INVALID);
511 t->sibling_index = fib_entry_child_add
512 (t->fib_entry_index, FIB_NODE_TYPE_VXLAN_TUNNEL, t - vxm->tunnels);
513 vxlan_tunnel_restack_dpo(t);
517 /* Multicast tunnel -
518 * as the same mcast group can be used for mutiple mcast tunnels
519 * with different VNIs, create the output fib adjecency only if
520 * it does not already exist
522 fib_protocol_t fp = (is_ip6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
523 dpo_id_t dpo = DPO_INVALID;
526 if (vtep_addr_ref(&t->dst) == 1)
528 fib_node_index_t mfei;
530 fib_route_path_t path = {
532 .frp_addr = zero_addr,
533 .frp_sw_if_index = 0xffffffff,
536 .frp_flags = FIB_ROUTE_PATH_LOCAL,
538 const mfib_prefix_t mpfx = {
540 .fp_len = (is_ip6 ? 128 : 32),
541 .fp_grp_addr = tun_dst_pfx.fp_addr,
545 * Setup the (*,G) to receive traffic on the mcast group
546 * - the forwarding interface is for-us
547 * - the accepting interface is that from the API
549 mfib_table_entry_path_update(t->encap_fib_index,
553 MFIB_ITF_FLAG_FORWARD);
555 path.frp_sw_if_index = a->mcast_sw_if_index;
556 path.frp_flags = FIB_ROUTE_PATH_FLAG_NONE;
557 mfei = mfib_table_entry_path_update(t->encap_fib_index,
561 MFIB_ITF_FLAG_ACCEPT);
564 * Create the mcast adjacency to send traffic to the group
566 ai = adj_mcast_add_or_lock(fp,
567 fib_proto_to_link(fp),
568 a->mcast_sw_if_index);
571 * create a new end-point
573 mcast_shared_add(&t->dst, mfei, ai);
576 ep = mcast_shared_get(&t->dst);
578 /* Stack shared mcast dst mac addr rewrite on encap */
579 dpo_set (&dpo, DPO_ADJACENCY,
580 fib_proto_to_dpo(fp),
583 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
585 flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER;
588 /* Set vxlan tunnel output node */
589 hi->output_node_index = encap_index;
591 vnet_get_sw_interface (vnet_get_main(), sw_if_index)->flood_class = flood_class;
595 /* deleting a tunnel: tunnel must exist */
597 return VNET_API_ERROR_NO_SUCH_ENTRY;
599 t = pool_elt_at_index (vxm->tunnels, p[0]);
601 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
602 /* make sure tunnel is removed from l2 bd or xconnect */
603 set_int_l2_mode(vxm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0, 0);
604 vec_add1 (vxm->free_vxlan_tunnel_hw_if_indices, t->hw_if_index);
606 vxm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
609 hash_unset (vxm->vxlan4_tunnel_by_key, key4.as_u64);
611 hash_unset_key_free (&vxm->vxlan6_tunnel_by_key, &key6);
613 if (!ip46_address_is_multicast(&t->dst))
615 vtep_addr_unref(&t->src);
616 fib_entry_child_remove(t->fib_entry_index, t->sibling_index);
617 fib_table_entry_delete_index(t->fib_entry_index, FIB_SOURCE_RR);
619 else if (vtep_addr_unref(&t->dst) == 0)
621 mcast_shared_remove(&t->dst);
624 fib_node_deinit(&t->node);
625 vec_free (t->rewrite);
626 pool_put (vxm->tunnels, t);
630 *sw_if_indexp = sw_if_index;
635 static u32 fib4_index_from_fib_id (u32 fib_id)
637 ip4_main_t * im = &ip4_main;
640 p = hash_get (im->fib_index_by_table_id, fib_id);
647 static u32 fib6_index_from_fib_id (u32 fib_id)
649 ip6_main_t * im = &ip6_main;
652 p = hash_get (im->fib_index_by_table_id, fib_id);
659 static uword get_decap_next_for_node(u32 node_index, u32 ipv4_set)
661 vxlan_main_t * vxm = &vxlan_main;
662 vlib_main_t * vm = vxm->vlib_main;
663 uword next_index = ~0;
667 next_index = vlib_node_add_next (vm, vxlan4_input_node.index, node_index);
671 next_index = vlib_node_add_next (vm, vxlan6_input_node.index, node_index);
677 static uword unformat_decap_next (unformat_input_t * input, va_list * args)
679 u32 * result = va_arg (*args, u32 *);
680 u32 ipv4_set = va_arg (*args, int);
681 vxlan_main_t * vxm = &vxlan_main;
682 vlib_main_t * vm = vxm->vlib_main;
686 if (unformat (input, "l2"))
687 *result = VXLAN_INPUT_NEXT_L2_INPUT;
688 else if (unformat (input, "node %U", unformat_vlib_node, vm, &node_index))
689 *result = get_decap_next_for_node(node_index, ipv4_set);
690 else if (unformat (input, "%d", &tmp))
697 static clib_error_t *
698 vxlan_add_del_tunnel_command_fn (vlib_main_t * vm,
699 unformat_input_t * input,
700 vlib_cli_command_t * cmd)
702 unformat_input_t _line_input, * line_input = &_line_input;
703 ip46_address_t src , dst;
710 u32 encap_fib_index = 0;
711 u32 mcast_sw_if_index = ~0;
712 u32 decap_next_index = VXLAN_INPUT_NEXT_L2_INPUT;
716 vnet_vxlan_add_del_tunnel_args_t _a, * a = &_a;
717 u32 tunnel_sw_if_index;
719 /* Cant "universally zero init" (={0}) due to GCC bug 53119 */
720 memset(&src, 0, sizeof src);
721 memset(&dst, 0, sizeof dst);
723 /* Get a line of input. */
724 if (! unformat_user (input, unformat_line_input, line_input))
727 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
728 if (unformat (line_input, "del"))
732 else if (unformat (line_input, "src %U",
733 unformat_ip4_address, &src.ip4))
738 else if (unformat (line_input, "dst %U",
739 unformat_ip4_address, &dst.ip4))
744 else if (unformat (line_input, "src %U",
745 unformat_ip6_address, &src.ip6))
750 else if (unformat (line_input, "dst %U",
751 unformat_ip6_address, &dst.ip6))
756 else if (unformat (line_input, "group %U %U",
757 unformat_ip4_address, &dst.ip4,
758 unformat_vnet_sw_interface,
759 vnet_get_main(), &mcast_sw_if_index))
761 grp_set = dst_set = 1;
764 else if (unformat (line_input, "group %U %U",
765 unformat_ip6_address, &dst.ip6,
766 unformat_vnet_sw_interface,
767 vnet_get_main(), &mcast_sw_if_index))
769 grp_set = dst_set = 1;
772 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
775 encap_fib_index = fib6_index_from_fib_id (tmp);
777 encap_fib_index = fib4_index_from_fib_id (tmp);
778 if (encap_fib_index == ~0)
779 return clib_error_return (0, "nonexistent encap-vrf-id %d", tmp);
781 else if (unformat (line_input, "decap-next %U", unformat_decap_next,
782 &decap_next_index, ipv4_set))
784 else if (unformat (line_input, "vni %d", &vni))
787 return clib_error_return (0, "vni %d out of range", vni);
790 return clib_error_return (0, "parse error: '%U'",
791 format_unformat_error, line_input);
794 unformat_free (line_input);
797 return clib_error_return (0, "tunnel src address not specified");
800 return clib_error_return (0, "tunnel dst address not specified");
802 if (grp_set && !ip46_address_is_multicast(&dst))
803 return clib_error_return (0, "tunnel group address not multicast");
805 if (grp_set == 0 && ip46_address_is_multicast(&dst))
806 return clib_error_return (0, "dst address must be unicast");
808 if (grp_set && mcast_sw_if_index == ~0)
809 return clib_error_return (0, "tunnel nonexistent multicast device");
811 if (ipv4_set && ipv6_set)
812 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
814 if (ip46_address_cmp(&src, &dst) == 0)
815 return clib_error_return (0, "src and dst addresses are identical");
817 if (decap_next_index == ~0)
818 return clib_error_return (0, "next node not found");
821 return clib_error_return (0, "vni not specified");
823 memset (a, 0, sizeof (*a));
826 a->is_ip6 = ipv6_set;
828 #define _(x) a->x = x;
832 rv = vnet_vxlan_add_del_tunnel (a, &tunnel_sw_if_index);
838 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name,
839 vnet_get_main(), tunnel_sw_if_index);
842 case VNET_API_ERROR_TUNNEL_EXIST:
843 return clib_error_return (0, "tunnel already exists...");
845 case VNET_API_ERROR_NO_SUCH_ENTRY:
846 return clib_error_return (0, "tunnel does not exist...");
849 return clib_error_return
850 (0, "vnet_vxlan_add_del_tunnel returned %d", rv);
857 * Add or delete a VXLAN Tunnel.
859 * VXLAN provides the features needed to allow L2 bridge domains (BDs)
860 * to span multiple servers. This is done by building an L2 overlay on
861 * top of an L3 network underlay using VXLAN tunnels.
863 * This makes it possible for servers to be co-located in the same data
864 * center or be separated geographically as long as they are reachable
865 * through the underlay L3 network.
867 * You can refer to this kind of L2 overlay bridge domain as a VXLAN
868 * (Virtual eXtensible VLAN) segment.
871 * Example of how to create a VXLAN Tunnel:
872 * @cliexcmd{create vxlan tunnel src 10.0.3.1 dst 10.0.3.3 vni 13 encap-vrf-id 7}
873 * Example of how to delete a VXLAN Tunnel:
874 * @cliexcmd{create vxlan tunnel src 10.0.3.1 dst 10.0.3.3 vni 13 del}
877 VLIB_CLI_COMMAND (create_vxlan_tunnel_command, static) = {
878 .path = "create vxlan tunnel",
880 "create vxlan tunnel src <local-vtep-addr>"
881 " {dst <remote-vtep-addr>|group <mcast-vtep-addr> <intf-name>} vni <nn>"
882 " [encap-vrf-id <nn>] [decap-next [l2|node <name>]] [del]",
883 .function = vxlan_add_del_tunnel_command_fn,
887 static clib_error_t *
888 show_vxlan_tunnel_command_fn (vlib_main_t * vm,
889 unformat_input_t * input,
890 vlib_cli_command_t * cmd)
892 vxlan_main_t * vxm = &vxlan_main;
895 if (pool_elts (vxm->tunnels) == 0)
896 vlib_cli_output (vm, "No vxlan tunnels configured...");
898 pool_foreach (t, vxm->tunnels,
900 vlib_cli_output (vm, "%U", format_vxlan_tunnel, t);
907 * Display all the VXLAN Tunnel entries.
910 * Example of how to display the VXLAN Tunnel entries:
911 * @cliexstart{show vxlan tunnel}
912 * [0] src 10.0.3.1 dst 10.0.3.3 vni 13 encap_fib_index 0 sw_if_index 5 decap_next l2
916 VLIB_CLI_COMMAND (show_vxlan_tunnel_command, static) = {
917 .path = "show vxlan tunnel",
918 .short_help = "show vxlan tunnel",
919 .function = show_vxlan_tunnel_command_fn,
924 clib_error_t *vxlan_init (vlib_main_t *vm)
926 vxlan_main_t * vxm = &vxlan_main;
928 vxm->vnet_main = vnet_get_main();
931 /* initialize the ip6 hash */
932 vxm->vxlan6_tunnel_by_key = hash_create_mem(0,
933 sizeof(vxlan6_tunnel_key_t),
935 vxm->vtep6 = hash_create_mem(0,
936 sizeof(ip6_address_t),
938 vxm->mcast_shared = hash_create_mem(0,
939 sizeof(ip46_address_t),
940 sizeof(mcast_shared_t));
942 udp_register_dst_port (vm, UDP_DST_PORT_vxlan,
943 vxlan4_input_node.index, /* is_ip4 */ 1);
944 udp_register_dst_port (vm, UDP_DST_PORT_vxlan6,
945 vxlan6_input_node.index, /* is_ip4 */ 0);
947 fib_node_register_type(FIB_NODE_TYPE_VXLAN_TUNNEL, &vxlan_vft);
952 VLIB_INIT_FUNCTION(vxlan_init);