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>
25 * VXLAN provides the features needed to allow L2 bridge domains (BDs)
26 * to span multiple servers. This is done by building an L2 overlay on
27 * top of an L3 network underlay using VXLAN tunnels.
29 * This makes it possible for servers to be co-located in the same data
30 * center or be separated geographically as long as they are reachable
31 * through the underlay L3 network.
33 * You can refer to this kind of L2 overlay bridge domain as a VXLAN
34 * (Virtual eXtensible VLAN) segment.
38 vxlan_main_t vxlan_main;
40 u8 * format_vxlan_tunnel (u8 * s, va_list * args)
42 vxlan_tunnel_t * t = va_arg (*args, vxlan_tunnel_t *);
43 vxlan_main_t * ngm = &vxlan_main;
46 "[%d] src %U dst %U vni %d encap_fib_index %d sw_if_index %d "
47 "fib_entry_index %d\n",
49 format_ip46_address, &t->src, IP46_TYPE_ANY,
50 format_ip46_address, &t->dst, IP46_TYPE_ANY,
51 t->vni, t->encap_fib_index, t->sw_if_index, t->fib_entry_index);
55 static u8 * format_vxlan_name (u8 * s, va_list * args)
57 u32 dev_instance = va_arg (*args, u32);
58 return format (s, "vxlan_tunnel%d", dev_instance);
61 static uword dummy_interface_tx (vlib_main_t * vm,
62 vlib_node_runtime_t * node,
65 clib_warning ("you shouldn't be here, leaking buffers...");
66 return frame->n_vectors;
70 vxlan_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
72 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
73 vnet_hw_interface_set_flags (vnm, hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP);
75 vnet_hw_interface_set_flags (vnm, hw_if_index, 0);
77 return /* no error */ 0;
80 VNET_DEVICE_CLASS (vxlan_device_class,static) = {
82 .format_device_name = format_vxlan_name,
83 .format_tx_trace = format_vxlan_encap_trace,
84 .tx_function = dummy_interface_tx,
85 .admin_up_down_function = vxlan_interface_admin_up_down,
88 static u8 * format_vxlan_header_with_length (u8 * s, va_list * args)
90 u32 dev_instance = va_arg (*args, u32);
91 s = format (s, "unimplemented dev %u", dev_instance);
95 VNET_HW_INTERFACE_CLASS (vxlan_hw_class) = {
97 .format_header = format_vxlan_header_with_length,
98 .build_rewrite = default_build_rewrite,
102 vxlan_tunnel_restack_dpo(vxlan_tunnel_t * t)
104 dpo_id_t dpo = DPO_INVALID;
105 u32 encap_index = ip46_address_is_ip4(&t->dst) ?
106 vxlan4_encap_node.index : vxlan6_encap_node.index;
107 fib_forward_chain_type_t forw_type = ip46_address_is_ip4(&t->dst) ?
108 FIB_FORW_CHAIN_TYPE_UNICAST_IP4 : FIB_FORW_CHAIN_TYPE_UNICAST_IP6;
110 fib_entry_contribute_forwarding (t->fib_entry_index, forw_type, &dpo);
111 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
115 static vxlan_tunnel_t *
116 vxlan_tunnel_from_fib_node (fib_node_t *node)
119 ASSERT(FIB_NODE_TYPE_VXLAN_TUNNEL == node->fn_type);
121 return ((vxlan_tunnel_t*) (((char*)node) -
122 STRUCT_OFFSET_OF(vxlan_tunnel_t, node)));
126 * Function definition to backwalk a FIB node -
127 * Here we will restack the new dpo of VXLAN DIP to encap node.
129 static fib_node_back_walk_rc_t
130 vxlan_tunnel_back_walk (fib_node_t *node,
131 fib_node_back_walk_ctx_t *ctx)
133 vxlan_tunnel_restack_dpo(vxlan_tunnel_from_fib_node(node));
134 return (FIB_NODE_BACK_WALK_CONTINUE);
138 * Function definition to get a FIB node from its index
141 vxlan_tunnel_fib_node_get (fib_node_index_t index)
144 vxlan_main_t * vxm = &vxlan_main;
146 t = pool_elt_at_index(vxm->tunnels, index);
152 * Function definition to inform the FIB node that its last lock has gone.
155 vxlan_tunnel_last_lock_gone (fib_node_t *node)
158 * The VXLAN tunnel is a root of the graph. As such
159 * it never has children and thus is never locked.
165 * Virtual function table registered by VXLAN tunnels
166 * for participation in the FIB object graph.
168 const static fib_node_vft_t vxlan_vft = {
169 .fnv_get = vxlan_tunnel_fib_node_get,
170 .fnv_last_lock = vxlan_tunnel_last_lock_gone,
171 .fnv_back_walk = vxlan_tunnel_back_walk,
175 #define foreach_copy_field \
177 _(mcast_sw_if_index) \
182 static int vxlan4_rewrite (vxlan_tunnel_t * t)
186 ip4_vxlan_header_t * h0;
187 int len = sizeof (*h0);
189 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
191 h0 = (ip4_vxlan_header_t *) rw;
193 /* Fixed portion of the (outer) ip4 header */
195 ip0->ip_version_and_header_length = 0x45;
197 ip0->protocol = IP_PROTOCOL_UDP;
199 /* we fix up the ip4 header length and checksum after-the-fact */
200 ip0->src_address.as_u32 = t->src.ip4.as_u32;
201 ip0->dst_address.as_u32 = t->dst.ip4.as_u32;
202 ip0->checksum = ip4_header_checksum (ip0);
204 /* UDP header, randomize src port on something, maybe? */
205 h0->udp.src_port = clib_host_to_net_u16 (4789);
206 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan);
209 vnet_set_vni_and_flags(&h0->vxlan, t->vni);
215 static int vxlan6_rewrite (vxlan_tunnel_t * t)
219 ip6_vxlan_header_t * h0;
220 int len = sizeof (*h0);
222 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
224 h0 = (ip6_vxlan_header_t *) rw;
226 /* Fixed portion of the (outer) ip6 header */
228 ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32(6 << 28);
229 ip0->hop_limit = 255;
230 ip0->protocol = IP_PROTOCOL_UDP;
232 ip0->src_address.as_u64[0] = t->src.ip6.as_u64[0];
233 ip0->src_address.as_u64[1] = t->src.ip6.as_u64[1];
234 ip0->dst_address.as_u64[0] = t->dst.ip6.as_u64[0];
235 ip0->dst_address.as_u64[1] = t->dst.ip6.as_u64[1];
237 /* UDP header, randomize src port on something, maybe? */
238 h0->udp.src_port = clib_host_to_net_u16 (4789);
239 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan);
242 vnet_set_vni_and_flags(&h0->vxlan, t->vni);
248 int vnet_vxlan_add_del_tunnel
249 (vnet_vxlan_add_del_tunnel_args_t *a, u32 * sw_if_indexp)
251 vxlan_main_t * vxm = &vxlan_main;
252 vxlan_tunnel_t *t = 0;
253 vnet_main_t * vnm = vxm->vnet_main;
254 vnet_hw_interface_t * hi;
256 u32 hw_if_index = ~0;
257 u32 sw_if_index = ~0;
259 vxlan4_tunnel_key_t key4;
260 vxlan6_tunnel_key_t key6;
261 u32 is_ip6 = a->is_ip6;
264 key4.src = a->dst.ip4.as_u32; /* decap src in key is encap dst in config */
265 key4.vni = clib_host_to_net_u32 (a->vni << 8);
267 p = hash_get (vxm->vxlan4_tunnel_by_key, key4.as_u64);
269 key6.src.as_u64[0] = a->dst.ip6.as_u64[0];
270 key6.src.as_u64[1] = a->dst.ip6.as_u64[1];
271 key6.vni = clib_host_to_net_u32 (a->vni << 8);
273 p = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6);
278 l2input_main_t * l2im = &l2input_main;
280 /* adding a tunnel: tunnel must not already exist */
282 return VNET_API_ERROR_TUNNEL_EXIST;
284 pool_get_aligned (vxm->tunnels, t, CLIB_CACHE_LINE_BYTES);
285 memset (t, 0, sizeof (*t));
287 /* copy from arg structure */
288 #define _(x) t->x = a->x;
295 t->key6 = clib_mem_alloc (sizeof(vxlan6_tunnel_key_t));
296 clib_memcpy (t->key6, &key6, sizeof(key6));
300 t->key4 = 0; /* not yet used */
304 rv = vxlan4_rewrite (t);
306 rv = vxlan6_rewrite (t);
311 pool_put (vxm->tunnels, t);
316 hash_set (vxm->vxlan4_tunnel_by_key, key4.as_u64, t - vxm->tunnels);
318 hash_set_mem (vxm->vxlan6_tunnel_by_key, t->key6, t - vxm->tunnels);
320 if (vec_len (vxm->free_vxlan_tunnel_hw_if_indices) > 0)
322 vnet_interface_main_t * im = &vnm->interface_main;
323 hw_if_index = vxm->free_vxlan_tunnel_hw_if_indices
324 [vec_len (vxm->free_vxlan_tunnel_hw_if_indices)-1];
325 _vec_len (vxm->free_vxlan_tunnel_hw_if_indices) -= 1;
327 hi = vnet_get_hw_interface (vnm, hw_if_index);
328 hi->dev_instance = t - vxm->tunnels;
329 hi->hw_instance = hi->dev_instance;
331 /* clear old stats of freed tunnel before reuse */
332 sw_if_index = hi->sw_if_index;
333 vnet_interface_counter_lock(im);
334 vlib_zero_combined_counter
335 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index);
336 vlib_zero_combined_counter
337 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index);
338 vlib_zero_simple_counter
339 (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index);
340 vnet_interface_counter_unlock(im);
344 hw_if_index = vnet_register_interface
345 (vnm, vxlan_device_class.index, t - vxm->tunnels,
346 vxlan_hw_class.index, t - vxm->tunnels);
347 hi = vnet_get_hw_interface (vnm, hw_if_index);
350 t->hw_if_index = hw_if_index;
351 t->sw_if_index = sw_if_index = hi->sw_if_index;
353 vec_validate_init_empty (vxm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
354 vxm->tunnel_index_by_sw_if_index[sw_if_index] = t - vxm->tunnels;
356 /* setup l2 input config with l2 feature and bd 0 to drop packet */
357 vec_validate (l2im->configs, sw_if_index);
358 l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP;
359 l2im->configs[sw_if_index].bd_index = 0;
361 vnet_sw_interface_set_flags (vnm, sw_if_index,
362 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
363 fib_node_init(&t->node, FIB_NODE_TYPE_VXLAN_TUNNEL);
364 fib_prefix_t tun_dst_pfx;
365 u32 encap_index = !is_ip6 ?
366 vxlan4_encap_node.index : vxlan6_encap_node.index;
367 vnet_flood_class_t flood_class = VNET_FLOOD_CLASS_TUNNEL_NORMAL;
369 fib_prefix_from_ip46_addr(&t->dst, &tun_dst_pfx);
370 if (ip46_address_is_multicast(&t->dst))
375 ip4_multicast_ethernet_address(mcast_mac, &t->dst.ip4);
376 fp = FIB_PROTOCOL_IP4;
378 ip6_multicast_ethernet_address(mcast_mac, t->dst.ip6.as_u32[0]);
379 fp = FIB_PROTOCOL_IP6;
381 t->mcast_adj_index = adj_rewrite_add_and_lock
382 (fp, fib_proto_to_link(fp), t->mcast_sw_if_index, mcast_mac);
384 flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER;
386 /* Stack mcast dst mac addr rewrite on encap */
387 dpo_proto_t dproto = fib_proto_to_dpo(fp);
388 dpo_id_t dpo = DPO_INVALID;
390 dpo_set (&dpo, DPO_ADJACENCY, dproto, t->mcast_adj_index);
391 dpo_stack_from_node (encap_index, &t->next_dpo, &dpo);
394 /* Add local mcast adj. */
395 receive_dpo_add_or_lock(dproto, ~0, NULL, &dpo);
396 t->fib_entry_index = fib_table_entry_special_dpo_add
397 (t->encap_fib_index, &tun_dst_pfx, FIB_SOURCE_SPECIAL, FIB_ENTRY_FLAG_NONE, &dpo);
401 * source the FIB entry for the tunnel's destination
402 * and become a child thereof. The tunnel will then get poked
403 * when the forwarding for the entry updates, and the tunnel can
404 * re-stack accordingly
406 t->fib_entry_index = fib_table_entry_special_add
407 (t->encap_fib_index, &tun_dst_pfx, FIB_SOURCE_RR, FIB_ENTRY_FLAG_NONE, ADJ_INDEX_INVALID);
408 t->sibling_index = fib_entry_child_add
409 (t->fib_entry_index, FIB_NODE_TYPE_VXLAN_TUNNEL, t - vxm->tunnels);
410 vxlan_tunnel_restack_dpo(t);
412 /* Set vxlan tunnel output node */
413 hi->output_node_index = encap_index;
415 vnet_get_sw_interface (vnet_get_main(), sw_if_index)->flood_class = flood_class;
419 /* deleting a tunnel: tunnel must exist */
421 return VNET_API_ERROR_NO_SUCH_ENTRY;
423 t = pool_elt_at_index (vxm->tunnels, p[0]);
425 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */);
426 /* make sure tunnel is removed from l2 bd or xconnect */
427 set_int_l2_mode(vxm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0, 0);
428 vec_add1 (vxm->free_vxlan_tunnel_hw_if_indices, t->hw_if_index);
430 vxm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0;
432 if (ip46_address_is_multicast(&t->dst))
434 adj_unlock(t->mcast_adj_index);
435 fib_table_entry_delete_index(t->fib_entry_index, FIB_SOURCE_SPECIAL);
439 fib_entry_child_remove(t->fib_entry_index, t->sibling_index);
440 fib_table_entry_delete_index(t->fib_entry_index, FIB_SOURCE_RR);
442 fib_node_deinit(&t->node);
446 hash_unset (vxm->vxlan4_tunnel_by_key, key4.as_u64);
450 hash_unset_mem (vxm->vxlan6_tunnel_by_key, t->key6);
451 clib_mem_free (t->key6);
453 vec_free (t->rewrite);
454 pool_put (vxm->tunnels, t);
458 *sw_if_indexp = sw_if_index;
463 static u32 fib4_index_from_fib_id (u32 fib_id)
465 ip4_main_t * im = &ip4_main;
468 p = hash_get (im->fib_index_by_table_id, fib_id);
475 static u32 fib6_index_from_fib_id (u32 fib_id)
477 ip6_main_t * im = &ip6_main;
480 p = hash_get (im->fib_index_by_table_id, fib_id);
487 static uword unformat_decap_next (unformat_input_t * input, va_list * args)
489 u32 * result = va_arg (*args, u32 *);
492 if (unformat (input, "l2"))
493 *result = VXLAN_INPUT_NEXT_L2_INPUT;
494 else if (unformat (input, "%d", &tmp))
501 static clib_error_t *
502 vxlan_add_del_tunnel_command_fn (vlib_main_t * vm,
503 unformat_input_t * input,
504 vlib_cli_command_t * cmd)
506 unformat_input_t _line_input, * line_input = &_line_input;
507 ip46_address_t src , dst;
514 u32 encap_fib_index = 0;
515 u32 mcast_sw_if_index = ~0;
516 u32 decap_next_index = ~0;
520 vnet_vxlan_add_del_tunnel_args_t _a, * a = &_a;
521 u32 tunnel_sw_if_index;
523 /* Cant "universally zero init" (={0}) due to GCC bug 53119 */
524 memset(&src, 0, sizeof src);
525 memset(&dst, 0, sizeof dst);
527 /* Get a line of input. */
528 if (! unformat_user (input, unformat_line_input, line_input))
531 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
532 if (unformat (line_input, "del"))
536 else if (unformat (line_input, "src %U",
537 unformat_ip4_address, &src.ip4))
542 else if (unformat (line_input, "dst %U",
543 unformat_ip4_address, &dst.ip4))
548 else if (unformat (line_input, "src %U",
549 unformat_ip6_address, &src.ip6))
554 else if (unformat (line_input, "dst %U",
555 unformat_ip6_address, &dst.ip6))
560 else if (unformat (line_input, "group %U %U",
561 unformat_ip4_address, &dst.ip4,
562 unformat_vnet_sw_interface,
563 vnet_get_main(), &mcast_sw_if_index))
565 grp_set = dst_set = 1;
568 else if (unformat (line_input, "group %U %U",
569 unformat_ip6_address, &dst.ip6,
570 unformat_vnet_sw_interface,
571 vnet_get_main(), &mcast_sw_if_index))
573 grp_set = dst_set = 1;
576 else if (unformat (line_input, "encap-vrf-id %d", &tmp))
579 encap_fib_index = fib6_index_from_fib_id (tmp);
581 encap_fib_index = fib4_index_from_fib_id (tmp);
582 if (encap_fib_index == ~0)
583 return clib_error_return (0, "nonexistent encap-vrf-id %d", tmp);
585 else if (unformat (line_input, "decap-next %U", unformat_decap_next,
588 else if (unformat (line_input, "vni %d", &vni))
591 return clib_error_return (0, "vni %d out of range", vni);
594 return clib_error_return (0, "parse error: '%U'",
595 format_unformat_error, line_input);
598 unformat_free (line_input);
601 return clib_error_return (0, "tunnel src address not specified");
604 return clib_error_return (0, "tunnel dst address not specified");
606 if (grp_set && !ip46_address_is_multicast(&dst))
607 return clib_error_return (0, "tunnel group address not multicast");
609 if (grp_set && mcast_sw_if_index == ~0)
610 return clib_error_return (0, "tunnel nonexistent multicast device");
612 if (ipv4_set && ipv6_set)
613 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
615 if (ip46_address_cmp(&src, &dst) == 0)
616 return clib_error_return (0, "src and dst addresses are identical");
619 return clib_error_return (0, "vni not specified");
621 memset (a, 0, sizeof (*a));
624 a->is_ip6 = ipv6_set;
626 #define _(x) a->x = x;
630 rv = vnet_vxlan_add_del_tunnel (a, &tunnel_sw_if_index);
636 vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name,
637 vnet_get_main(), tunnel_sw_if_index);
640 case VNET_API_ERROR_TUNNEL_EXIST:
641 return clib_error_return (0, "tunnel already exists...");
643 case VNET_API_ERROR_NO_SUCH_ENTRY:
644 return clib_error_return (0, "tunnel does not exist...");
647 return clib_error_return
648 (0, "vnet_vxlan_add_del_tunnel returned %d", rv);
655 * Add or delete a VXLAN Tunnel.
657 * VXLAN provides the features needed to allow L2 bridge domains (BDs)
658 * to span multiple servers. This is done by building an L2 overlay on
659 * top of an L3 network underlay using VXLAN tunnels.
661 * This makes it possible for servers to be co-located in the same data
662 * center or be separated geographically as long as they are reachable
663 * through the underlay L3 network.
665 * You can refer to this kind of L2 overlay bridge domain as a VXLAN
666 * (Virtual eXtensible VLAN) segment.
669 * Example of how to create a VXLAN Tunnel:
670 * @cliexcmd{create vxlan tunnel src 10.0.3.1 dst 10.0.3.3 vni 13 encap-vrf-id 7}
671 * Example of how to delete a VXLAN Tunnel:
672 * @cliexcmd{create vxlan tunnel src 10.0.3.1 dst 10.0.3.3 vni 13 del}
675 VLIB_CLI_COMMAND (create_vxlan_tunnel_command, static) = {
676 .path = "create vxlan tunnel",
678 "create vxlan tunnel src <local-vtep-addr>"
679 " {dst <remote-vtep-addr>|group <mcast-vtep-addr> <intf-name>} vni <nn>"
680 " [encap-vrf-id <nn>]",
681 .function = vxlan_add_del_tunnel_command_fn,
685 static clib_error_t *
686 show_vxlan_tunnel_command_fn (vlib_main_t * vm,
687 unformat_input_t * input,
688 vlib_cli_command_t * cmd)
690 vxlan_main_t * vxm = &vxlan_main;
693 if (pool_elts (vxm->tunnels) == 0)
694 vlib_cli_output (vm, "No vxlan tunnels configured...");
696 pool_foreach (t, vxm->tunnels,
698 vlib_cli_output (vm, "%U", format_vxlan_tunnel, t);
705 * Display all the VXLAN Tunnel entries.
708 * Example of how to display the VXLAN Tunnel entries:
709 * @cliexstart{show vxlan tunnel}
710 * [0] src 10.0.3.1 dst 10.0.3.3 vni 13 encap_fib_index 0 sw_if_index 5
714 VLIB_CLI_COMMAND (show_vxlan_tunnel_command, static) = {
715 .path = "show vxlan tunnel",
716 .short_help = "show vxlan tunnel",
717 .function = show_vxlan_tunnel_command_fn,
722 clib_error_t *vxlan_init (vlib_main_t *vm)
724 vxlan_main_t * vxm = &vxlan_main;
726 vxm->vnet_main = vnet_get_main();
729 /* initialize the ip6 hash */
730 vxm->vxlan6_tunnel_by_key = hash_create_mem(0,
731 sizeof(vxlan6_tunnel_key_t),
734 udp_register_dst_port (vm, UDP_DST_PORT_vxlan,
735 vxlan4_input_node.index, /* is_ip4 */ 1);
736 udp_register_dst_port (vm, UDP_DST_PORT_vxlan6,
737 vxlan6_input_node.index, /* is_ip4 */ 0);
739 fib_node_register_type(FIB_NODE_TYPE_VXLAN_TUNNEL, &vxlan_vft);
744 VLIB_INIT_FUNCTION(vxlan_init);