Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 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. |
| 14 | */ |
| 15 | #include <vnet/vxlan/vxlan.h> |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 16 | #include <vnet/ip/format.h> |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 17 | #include <vnet/fib/fib_entry.h> |
| 18 | #include <vnet/fib/fib_table.h> |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 19 | #include <vnet/dpo/receive_dpo.h> |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 20 | #include <vlib/vlib.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 21 | |
Billy McFall | c5d9cda | 2016-09-14 10:39:58 -0400 | [diff] [blame] | 22 | /** |
| 23 | * @file |
| 24 | * @brief VXLAN. |
| 25 | * |
| 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. |
| 29 | * |
| 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. |
| 33 | * |
| 34 | * You can refer to this kind of L2 overlay bridge domain as a VXLAN |
| 35 | * (Virtual eXtensible VLAN) segment. |
| 36 | */ |
| 37 | |
| 38 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 39 | vxlan_main_t vxlan_main; |
| 40 | |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 41 | static u8 * format_decap_next (u8 * s, va_list * args) |
| 42 | { |
| 43 | u32 next_index = va_arg (*args, u32); |
| 44 | |
| 45 | switch (next_index) |
| 46 | { |
| 47 | case VXLAN_INPUT_NEXT_DROP: |
| 48 | return format (s, "drop"); |
| 49 | case VXLAN_INPUT_NEXT_L2_INPUT: |
| 50 | return format (s, "l2"); |
| 51 | default: |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 52 | return format (s, "index %d", next_index); |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 53 | } |
| 54 | return s; |
| 55 | } |
| 56 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | u8 * format_vxlan_tunnel (u8 * s, va_list * args) |
| 58 | { |
| 59 | vxlan_tunnel_t * t = va_arg (*args, vxlan_tunnel_t *); |
| 60 | vxlan_main_t * ngm = &vxlan_main; |
| 61 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 62 | s = format (s, "[%d] src %U dst %U vni %d sw_if_index %d ", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | t - ngm->tunnels, |
Damjan Marion | 86be487 | 2016-05-24 23:19:11 +0200 | [diff] [blame] | 64 | format_ip46_address, &t->src, IP46_TYPE_ANY, |
| 65 | format_ip46_address, &t->dst, IP46_TYPE_ANY, |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 66 | t->vni, t->sw_if_index); |
| 67 | |
| 68 | if (ip46_address_is_multicast (&t->dst)) |
| 69 | s = format (s, "mcast_sw_if_index %d ", t->mcast_sw_if_index); |
| 70 | |
| 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); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | return s; |
| 75 | } |
| 76 | |
| 77 | static u8 * format_vxlan_name (u8 * s, va_list * args) |
| 78 | { |
| 79 | u32 dev_instance = va_arg (*args, u32); |
| 80 | return format (s, "vxlan_tunnel%d", dev_instance); |
| 81 | } |
| 82 | |
| 83 | static uword dummy_interface_tx (vlib_main_t * vm, |
| 84 | vlib_node_runtime_t * node, |
| 85 | vlib_frame_t * frame) |
| 86 | { |
| 87 | clib_warning ("you shouldn't be here, leaking buffers..."); |
| 88 | return frame->n_vectors; |
| 89 | } |
| 90 | |
Pavel Kotucek | 988a7c4 | 2016-02-29 15:03:08 +0100 | [diff] [blame] | 91 | static clib_error_t * |
| 92 | vxlan_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) |
| 93 | { |
| 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); |
| 96 | else |
| 97 | vnet_hw_interface_set_flags (vnm, hw_if_index, 0); |
| 98 | |
| 99 | return /* no error */ 0; |
| 100 | } |
| 101 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | VNET_DEVICE_CLASS (vxlan_device_class,static) = { |
| 103 | .name = "VXLAN", |
| 104 | .format_device_name = format_vxlan_name, |
| 105 | .format_tx_trace = format_vxlan_encap_trace, |
| 106 | .tx_function = dummy_interface_tx, |
Pavel Kotucek | 988a7c4 | 2016-02-29 15:03:08 +0100 | [diff] [blame] | 107 | .admin_up_down_function = vxlan_interface_admin_up_down, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | static u8 * format_vxlan_header_with_length (u8 * s, va_list * args) |
| 111 | { |
| 112 | u32 dev_instance = va_arg (*args, u32); |
| 113 | s = format (s, "unimplemented dev %u", dev_instance); |
| 114 | return s; |
| 115 | } |
| 116 | |
| 117 | VNET_HW_INTERFACE_CLASS (vxlan_hw_class) = { |
| 118 | .name = "VXLAN", |
| 119 | .format_header = format_vxlan_header_with_length, |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 120 | .build_rewrite = default_build_rewrite, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 123 | static void |
| 124 | vxlan_tunnel_restack_dpo(vxlan_tunnel_t * t) |
| 125 | { |
| 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; |
| 131 | |
| 132 | fib_entry_contribute_forwarding (t->fib_entry_index, forw_type, &dpo); |
| 133 | dpo_stack_from_node (encap_index, &t->next_dpo, &dpo); |
| 134 | dpo_reset(&dpo); |
| 135 | } |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 136 | |
| 137 | static vxlan_tunnel_t * |
| 138 | vxlan_tunnel_from_fib_node (fib_node_t *node) |
| 139 | { |
| 140 | #if (CLIB_DEBUG > 0) |
| 141 | ASSERT(FIB_NODE_TYPE_VXLAN_TUNNEL == node->fn_type); |
| 142 | #endif |
| 143 | return ((vxlan_tunnel_t*) (((char*)node) - |
| 144 | STRUCT_OFFSET_OF(vxlan_tunnel_t, node))); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Function definition to backwalk a FIB node - |
| 149 | * Here we will restack the new dpo of VXLAN DIP to encap node. |
| 150 | */ |
| 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) |
| 154 | { |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 155 | vxlan_tunnel_restack_dpo(vxlan_tunnel_from_fib_node(node)); |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 156 | return (FIB_NODE_BACK_WALK_CONTINUE); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Function definition to get a FIB node from its index |
| 161 | */ |
| 162 | static fib_node_t* |
| 163 | vxlan_tunnel_fib_node_get (fib_node_index_t index) |
| 164 | { |
| 165 | vxlan_tunnel_t * t; |
| 166 | vxlan_main_t * vxm = &vxlan_main; |
| 167 | |
| 168 | t = pool_elt_at_index(vxm->tunnels, index); |
| 169 | |
| 170 | return (&t->node); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Function definition to inform the FIB node that its last lock has gone. |
| 175 | */ |
| 176 | static void |
| 177 | vxlan_tunnel_last_lock_gone (fib_node_t *node) |
| 178 | { |
| 179 | /* |
| 180 | * The VXLAN tunnel is a root of the graph. As such |
| 181 | * it never has children and thus is never locked. |
| 182 | */ |
| 183 | ASSERT(0); |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Virtual function table registered by VXLAN tunnels |
| 188 | * for participation in the FIB object graph. |
| 189 | */ |
| 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, |
| 194 | }; |
| 195 | |
| 196 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | #define foreach_copy_field \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 198 | _(vni) \ |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 199 | _(mcast_sw_if_index) \ |
| 200 | _(encap_fib_index) \ |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 201 | _(decap_next_index) \ |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 202 | _(src) \ |
| 203 | _(dst) |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 204 | |
| 205 | static int vxlan4_rewrite (vxlan_tunnel_t * t) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 206 | { |
| 207 | u8 *rw = 0; |
| 208 | ip4_header_t * ip0; |
| 209 | ip4_vxlan_header_t * h0; |
| 210 | int len = sizeof (*h0); |
| 211 | |
| 212 | vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES); |
| 213 | |
| 214 | h0 = (ip4_vxlan_header_t *) rw; |
| 215 | |
| 216 | /* Fixed portion of the (outer) ip4 header */ |
| 217 | ip0 = &h0->ip4; |
| 218 | ip0->ip_version_and_header_length = 0x45; |
| 219 | ip0->ttl = 254; |
| 220 | ip0->protocol = IP_PROTOCOL_UDP; |
| 221 | |
| 222 | /* we fix up the ip4 header length and checksum after-the-fact */ |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 223 | ip0->src_address.as_u32 = t->src.ip4.as_u32; |
| 224 | ip0->dst_address.as_u32 = t->dst.ip4.as_u32; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 225 | ip0->checksum = ip4_header_checksum (ip0); |
| 226 | |
| 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); |
| 230 | |
| 231 | /* VXLAN header */ |
| 232 | vnet_set_vni_and_flags(&h0->vxlan, t->vni); |
| 233 | |
| 234 | t->rewrite = rw; |
| 235 | return (0); |
| 236 | } |
| 237 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 238 | static int vxlan6_rewrite (vxlan_tunnel_t * t) |
| 239 | { |
| 240 | u8 *rw = 0; |
| 241 | ip6_header_t * ip0; |
| 242 | ip6_vxlan_header_t * h0; |
| 243 | int len = sizeof (*h0); |
| 244 | |
| 245 | vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES); |
| 246 | |
| 247 | h0 = (ip6_vxlan_header_t *) rw; |
| 248 | |
| 249 | /* Fixed portion of the (outer) ip6 header */ |
| 250 | ip0 = &h0->ip6; |
| 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; |
| 254 | |
| 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]; |
| 259 | |
| 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); |
| 263 | |
| 264 | /* VXLAN header */ |
| 265 | vnet_set_vni_and_flags(&h0->vxlan, t->vni); |
| 266 | |
| 267 | t->rewrite = rw; |
| 268 | return (0); |
| 269 | } |
| 270 | |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 271 | static int vxlan_check_decap_next(vxlan_main_t * vxm, u32 is_ip6, u32 decap_next_index) |
| 272 | { |
| 273 | vlib_main_t * vm = vxm->vlib_main; |
| 274 | vlib_node_runtime_t *r; |
| 275 | |
| 276 | if(!is_ip6) |
| 277 | { |
| 278 | r = vlib_node_get_runtime (vm, vxlan4_input_node.index); |
| 279 | if(decap_next_index >= r->n_next_nodes) |
| 280 | return 1; |
| 281 | } |
| 282 | else |
| 283 | { |
| 284 | r = vlib_node_get_runtime (vm, vxlan6_input_node.index); |
| 285 | if(decap_next_index >= r->n_next_nodes) |
| 286 | return 1; |
| 287 | } |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 292 | static void |
| 293 | hash_set_key_copy (uword ** h, void * key, uword v) { |
| 294 | size_t ksz = hash_header(*h)->user; |
| 295 | void * copy = clib_mem_alloc (ksz); |
| 296 | clib_memcpy (copy, key, ksz); |
| 297 | hash_set_mem (*h, copy, v); |
| 298 | } |
| 299 | |
| 300 | static void |
| 301 | hash_unset_key_free (uword ** h, void * key) { |
| 302 | hash_pair_t * hp = hash_get_pair_mem (*h, key); |
| 303 | ASSERT (hp); |
| 304 | key = uword_to_pointer (hp->key, void *); |
| 305 | hash_unset_mem (*h, key); |
| 306 | clib_mem_free (key); |
| 307 | } |
| 308 | |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 309 | static uword |
| 310 | vtep_addr_ref(ip46_address_t *ip) |
| 311 | { |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 312 | uword *vtep = ip46_address_is_ip4(ip) ? |
| 313 | hash_get (vxlan_main.vtep4, ip->ip4.as_u32) : |
| 314 | hash_get_mem (vxlan_main.vtep6, &ip->ip6); |
| 315 | if (vtep) |
| 316 | return ++(*vtep); |
| 317 | ip46_address_is_ip4(ip) ? |
| 318 | hash_set (vxlan_main.vtep4, ip->ip4.as_u32, 1) : |
| 319 | hash_set_key_copy (&vxlan_main.vtep6, &ip->ip6, 1); |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 320 | return 1; |
| 321 | } |
| 322 | |
| 323 | static uword |
| 324 | vtep_addr_unref(ip46_address_t *ip) |
| 325 | { |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 326 | uword *vtep = ip46_address_is_ip4(ip) ? |
| 327 | hash_get (vxlan_main.vtep4, ip->ip4.as_u32) : |
| 328 | hash_get_mem (vxlan_main.vtep6, &ip->ip6); |
| 329 | ASSERT(vtep); |
| 330 | if (--(*vtep) != 0) |
| 331 | return *vtep; |
| 332 | ip46_address_is_ip4(ip) ? |
| 333 | hash_unset (vxlan_main.vtep4, ip->ip4.as_u32) : |
| 334 | hash_unset_key_free (&vxlan_main.vtep6, &ip->ip6); |
| 335 | return 0; |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 336 | } |
| 337 | |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 338 | typedef CLIB_PACKED(union { |
| 339 | struct { |
| 340 | fib_node_index_t fib_entry_index; |
| 341 | adj_index_t mcast_adj_index; |
| 342 | }; |
| 343 | u64 as_u64; |
| 344 | }) mcast_shared_t; |
| 345 | |
| 346 | static inline mcast_shared_t |
| 347 | mcast_shared_get(ip46_address_t * ip) |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 348 | { |
| 349 | ASSERT(ip46_address_is_multicast(ip)); |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 350 | uword * p = hash_get_mem (vxlan_main.mcast_shared, ip); |
| 351 | ASSERT(p); |
| 352 | return (mcast_shared_t) { .as_u64 = *p }; |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 353 | } |
| 354 | |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 355 | static inline void |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 356 | ip46_multicast_ethernet_address(u8 * ethernet_address, ip46_address_t * ip) { |
| 357 | if (ip46_address_is_ip4(ip)) |
| 358 | ip4_multicast_ethernet_address(ethernet_address, &ip->ip4); |
| 359 | else |
| 360 | ip6_multicast_ethernet_address(ethernet_address, ip->ip6.as_u32[0]); |
| 361 | } |
| 362 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 363 | int vnet_vxlan_add_del_tunnel |
| 364 | (vnet_vxlan_add_del_tunnel_args_t *a, u32 * sw_if_indexp) |
| 365 | { |
| 366 | vxlan_main_t * vxm = &vxlan_main; |
| 367 | vxlan_tunnel_t *t = 0; |
| 368 | vnet_main_t * vnm = vxm->vnet_main; |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 369 | uword * p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 370 | u32 hw_if_index = ~0; |
| 371 | u32 sw_if_index = ~0; |
| 372 | int rv; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 373 | vxlan4_tunnel_key_t key4; |
| 374 | vxlan6_tunnel_key_t key6; |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 375 | u32 is_ip6 = a->is_ip6; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 376 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 377 | if (!is_ip6) |
| 378 | { |
| 379 | key4.src = a->dst.ip4.as_u32; /* decap src in key is encap dst in config */ |
| 380 | key4.vni = clib_host_to_net_u32 (a->vni << 8); |
| 381 | p = hash_get (vxm->vxlan4_tunnel_by_key, key4.as_u64); |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 382 | } |
| 383 | else |
| 384 | { |
| 385 | key6.src.as_u64[0] = a->dst.ip6.as_u64[0]; |
| 386 | key6.src.as_u64[1] = a->dst.ip6.as_u64[1]; |
| 387 | key6.vni = clib_host_to_net_u32 (a->vni << 8); |
| 388 | p = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6); |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 389 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 390 | |
| 391 | if (a->is_add) |
| 392 | { |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 393 | l2input_main_t * l2im = &l2input_main; |
| 394 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 395 | /* adding a tunnel: tunnel must not already exist */ |
| 396 | if (p) |
| 397 | return VNET_API_ERROR_TUNNEL_EXIST; |
| 398 | |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 399 | /*if not set explicitly, default to l2 */ |
| 400 | if(a->decap_next_index == ~0) |
| 401 | a->decap_next_index = VXLAN_INPUT_NEXT_L2_INPUT; |
| 402 | if (vxlan_check_decap_next(vxm, is_ip6, a->decap_next_index)) |
| 403 | return VNET_API_ERROR_INVALID_DECAP_NEXT; |
| 404 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 405 | pool_get_aligned (vxm->tunnels, t, CLIB_CACHE_LINE_BYTES); |
| 406 | memset (t, 0, sizeof (*t)); |
| 407 | |
| 408 | /* copy from arg structure */ |
| 409 | #define _(x) t->x = a->x; |
| 410 | foreach_copy_field; |
| 411 | #undef _ |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 412 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 413 | if (!is_ip6) |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 414 | rv = vxlan4_rewrite (t); |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 415 | else |
| 416 | rv = vxlan6_rewrite (t); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 417 | |
| 418 | if (rv) |
| 419 | { |
| 420 | pool_put (vxm->tunnels, t); |
| 421 | return rv; |
| 422 | } |
| 423 | |
Eyal Bari | 5ac9bf5 | 2017-01-02 14:29:21 +0200 | [diff] [blame] | 424 | /* copy the key */ |
| 425 | if (is_ip6) |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 426 | hash_set_key_copy (&vxm->vxlan6_tunnel_by_key, &key6, t - vxm->tunnels); |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 427 | else |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 428 | hash_set (vxm->vxlan4_tunnel_by_key, key4.as_u64, t - vxm->tunnels); |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 429 | |
| 430 | vnet_hw_interface_t * hi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 431 | if (vec_len (vxm->free_vxlan_tunnel_hw_if_indices) > 0) |
| 432 | { |
| 433 | vnet_interface_main_t * im = &vnm->interface_main; |
| 434 | hw_if_index = vxm->free_vxlan_tunnel_hw_if_indices |
| 435 | [vec_len (vxm->free_vxlan_tunnel_hw_if_indices)-1]; |
| 436 | _vec_len (vxm->free_vxlan_tunnel_hw_if_indices) -= 1; |
| 437 | |
| 438 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 439 | hi->dev_instance = t - vxm->tunnels; |
| 440 | hi->hw_instance = hi->dev_instance; |
| 441 | |
| 442 | /* clear old stats of freed tunnel before reuse */ |
| 443 | sw_if_index = hi->sw_if_index; |
| 444 | vnet_interface_counter_lock(im); |
| 445 | vlib_zero_combined_counter |
| 446 | (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index); |
| 447 | vlib_zero_combined_counter |
| 448 | (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index); |
| 449 | vlib_zero_simple_counter |
| 450 | (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index); |
| 451 | vnet_interface_counter_unlock(im); |
| 452 | } |
| 453 | else |
| 454 | { |
| 455 | hw_if_index = vnet_register_interface |
| 456 | (vnm, vxlan_device_class.index, t - vxm->tunnels, |
| 457 | vxlan_hw_class.index, t - vxm->tunnels); |
| 458 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | t->hw_if_index = hw_if_index; |
| 462 | t->sw_if_index = sw_if_index = hi->sw_if_index; |
| 463 | |
Dave Wallace | 60231f3 | 2015-12-17 21:04:30 -0500 | [diff] [blame] | 464 | vec_validate_init_empty (vxm->tunnel_index_by_sw_if_index, sw_if_index, ~0); |
| 465 | vxm->tunnel_index_by_sw_if_index[sw_if_index] = t - vxm->tunnels; |
| 466 | |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 467 | /* setup l2 input config with l2 feature and bd 0 to drop packet */ |
| 468 | vec_validate (l2im->configs, sw_if_index); |
| 469 | l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP; |
| 470 | l2im->configs[sw_if_index].bd_index = 0; |
John Lo | 3ef822e | 2016-06-07 09:14:07 -0400 | [diff] [blame] | 471 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 472 | vnet_sw_interface_set_flags (vnm, sw_if_index, |
| 473 | VNET_SW_INTERFACE_FLAG_ADMIN_UP); |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 474 | fib_node_init(&t->node, FIB_NODE_TYPE_VXLAN_TUNNEL); |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 475 | fib_prefix_t tun_dst_pfx; |
| 476 | u32 encap_index = !is_ip6 ? |
| 477 | vxlan4_encap_node.index : vxlan6_encap_node.index; |
| 478 | vnet_flood_class_t flood_class = VNET_FLOOD_CLASS_TUNNEL_NORMAL; |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 479 | |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 480 | fib_prefix_from_ip46_addr(&t->dst, &tun_dst_pfx); |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 481 | if (!ip46_address_is_multicast(&t->dst)) |
| 482 | { |
| 483 | /* Unicast tunnel - |
| 484 | * source the FIB entry for the tunnel's destination |
| 485 | * and become a child thereof. The tunnel will then get poked |
| 486 | * when the forwarding for the entry updates, and the tunnel can |
| 487 | * re-stack accordingly |
| 488 | */ |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 489 | vtep_addr_ref(&t->src); |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 490 | t->fib_entry_index = fib_table_entry_special_add |
| 491 | (t->encap_fib_index, &tun_dst_pfx, FIB_SOURCE_RR, |
| 492 | FIB_ENTRY_FLAG_NONE, ADJ_INDEX_INVALID); |
| 493 | t->sibling_index = fib_entry_child_add |
| 494 | (t->fib_entry_index, FIB_NODE_TYPE_VXLAN_TUNNEL, t - vxm->tunnels); |
| 495 | vxlan_tunnel_restack_dpo(t); |
| 496 | } |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 497 | else |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 498 | { |
| 499 | /* Multicast tunnel - |
| 500 | * as the same mcast group can be used for mutiple mcast tunnels |
| 501 | * with different VNIs, create the output fib adjecency only if |
| 502 | * it does not already exist |
| 503 | */ |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 504 | fib_protocol_t fp = (is_ip6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4; |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 505 | dpo_id_t dpo = DPO_INVALID; |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 506 | dpo_proto_t dproto = fib_proto_to_dpo(fp); |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 507 | |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 508 | if (vtep_addr_ref(&t->dst) == 1) |
| 509 | { |
| 510 | u8 mcast_mac[6]; |
| 511 | |
| 512 | ip46_multicast_ethernet_address(mcast_mac, &t->dst); |
| 513 | receive_dpo_add_or_lock(dproto, ~0, NULL, &dpo); |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 514 | mcast_shared_t new_ep = { |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 515 | .mcast_adj_index = adj_rewrite_add_and_lock |
| 516 | (fp, fib_proto_to_link(fp), a->mcast_sw_if_index, mcast_mac), |
| 517 | /* Add VRF local mcast adj. */ |
| 518 | .fib_entry_index = fib_table_entry_special_dpo_add |
| 519 | (t->encap_fib_index, &tun_dst_pfx, |
| 520 | FIB_SOURCE_SPECIAL, FIB_ENTRY_FLAG_NONE, &dpo) |
| 521 | }; |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 522 | hash_set_key_copy (&vxm->mcast_shared, &t->dst, new_ep.as_u64); |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 523 | dpo_reset(&dpo); |
| 524 | } |
| 525 | /* Stack shared mcast dst mac addr rewrite on encap */ |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 526 | mcast_shared_t ep = mcast_shared_get(&t->dst); |
| 527 | dpo_set (&dpo, DPO_ADJACENCY, dproto, ep.mcast_adj_index); |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 528 | dpo_stack_from_node (encap_index, &t->next_dpo, &dpo); |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 529 | dpo_reset (&dpo); |
| 530 | flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER; |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 531 | } |
| 532 | |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 533 | /* Set vxlan tunnel output node */ |
| 534 | hi->output_node_index = encap_index; |
| 535 | |
| 536 | vnet_get_sw_interface (vnet_get_main(), sw_if_index)->flood_class = flood_class; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 537 | } |
| 538 | else |
| 539 | { |
| 540 | /* deleting a tunnel: tunnel must exist */ |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 541 | if (!p) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 542 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 543 | |
| 544 | t = pool_elt_at_index (vxm->tunnels, p[0]); |
| 545 | |
| 546 | vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */); |
| 547 | /* make sure tunnel is removed from l2 bd or xconnect */ |
| 548 | set_int_l2_mode(vxm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0, 0); |
| 549 | vec_add1 (vxm->free_vxlan_tunnel_hw_if_indices, t->hw_if_index); |
| 550 | |
Dave Wallace | 60231f3 | 2015-12-17 21:04:30 -0500 | [diff] [blame] | 551 | vxm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0; |
| 552 | |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 553 | if (!is_ip6) |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 554 | hash_unset (vxm->vxlan4_tunnel_by_key, key4.as_u64); |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 555 | else |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 556 | hash_unset_key_free (&vxm->vxlan6_tunnel_by_key, &key6); |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 557 | |
| 558 | if (!ip46_address_is_multicast(&t->dst)) |
| 559 | { |
Eyal Bari | 5ac9bf5 | 2017-01-02 14:29:21 +0200 | [diff] [blame] | 560 | vtep_addr_unref(&t->src); |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 561 | fib_entry_child_remove(t->fib_entry_index, t->sibling_index); |
| 562 | fib_table_entry_delete_index(t->fib_entry_index, FIB_SOURCE_RR); |
| 563 | } |
| 564 | else if (vtep_addr_unref(&t->dst) == 0) |
| 565 | { |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 566 | mcast_shared_t ep = mcast_shared_get(&t->dst); |
| 567 | adj_unlock(ep.mcast_adj_index); |
| 568 | fib_table_entry_delete_index(ep.fib_entry_index, FIB_SOURCE_SPECIAL); |
| 569 | hash_unset_key_free (&vxm->mcast_shared, &t->dst); |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | fib_node_deinit(&t->node); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 573 | vec_free (t->rewrite); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 574 | pool_put (vxm->tunnels, t); |
| 575 | } |
| 576 | |
| 577 | if (sw_if_indexp) |
| 578 | *sw_if_indexp = sw_if_index; |
| 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 583 | static u32 fib4_index_from_fib_id (u32 fib_id) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 584 | { |
| 585 | ip4_main_t * im = &ip4_main; |
| 586 | uword * p; |
| 587 | |
| 588 | p = hash_get (im->fib_index_by_table_id, fib_id); |
| 589 | if (!p) |
| 590 | return ~0; |
| 591 | |
| 592 | return p[0]; |
| 593 | } |
| 594 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 595 | static u32 fib6_index_from_fib_id (u32 fib_id) |
| 596 | { |
| 597 | ip6_main_t * im = &ip6_main; |
| 598 | uword * p; |
| 599 | |
| 600 | p = hash_get (im->fib_index_by_table_id, fib_id); |
| 601 | if (!p) |
| 602 | return ~0; |
| 603 | |
| 604 | return p[0]; |
| 605 | } |
| 606 | |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 607 | static uword get_decap_next_for_node(u32 node_index, u32 ipv4_set) |
| 608 | { |
| 609 | vxlan_main_t * vxm = &vxlan_main; |
| 610 | vlib_main_t * vm = vxm->vlib_main; |
| 611 | uword next_index = ~0; |
| 612 | |
| 613 | if (ipv4_set) |
| 614 | { |
| 615 | next_index = vlib_node_add_next (vm, vxlan4_input_node.index, node_index); |
| 616 | } |
| 617 | else |
| 618 | { |
| 619 | next_index = vlib_node_add_next (vm, vxlan6_input_node.index, node_index); |
| 620 | } |
| 621 | |
| 622 | return next_index; |
| 623 | } |
| 624 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 625 | static uword unformat_decap_next (unformat_input_t * input, va_list * args) |
| 626 | { |
| 627 | u32 * result = va_arg (*args, u32 *); |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 628 | u32 ipv4_set = va_arg (*args, int); |
| 629 | vxlan_main_t * vxm = &vxlan_main; |
| 630 | vlib_main_t * vm = vxm->vlib_main; |
| 631 | u32 node_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 632 | u32 tmp; |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 633 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 634 | if (unformat (input, "l2")) |
| 635 | *result = VXLAN_INPUT_NEXT_L2_INPUT; |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 636 | else if (unformat (input, "node %U", unformat_vlib_node, vm, &node_index)) |
| 637 | *result = get_decap_next_for_node(node_index, ipv4_set); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 638 | else if (unformat (input, "%d", &tmp)) |
| 639 | *result = tmp; |
| 640 | else |
| 641 | return 0; |
| 642 | return 1; |
| 643 | } |
| 644 | |
| 645 | static clib_error_t * |
| 646 | vxlan_add_del_tunnel_command_fn (vlib_main_t * vm, |
| 647 | unformat_input_t * input, |
| 648 | vlib_cli_command_t * cmd) |
| 649 | { |
| 650 | unformat_input_t _line_input, * line_input = &_line_input; |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 651 | ip46_address_t src , dst; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 652 | u8 is_add = 1; |
| 653 | u8 src_set = 0; |
| 654 | u8 dst_set = 0; |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 655 | u8 grp_set = 0; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 656 | u8 ipv4_set = 0; |
| 657 | u8 ipv6_set = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 658 | u32 encap_fib_index = 0; |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 659 | u32 mcast_sw_if_index = ~0; |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 660 | u32 decap_next_index = VXLAN_INPUT_NEXT_L2_INPUT; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 661 | u32 vni = 0; |
| 662 | u32 tmp; |
| 663 | int rv; |
| 664 | vnet_vxlan_add_del_tunnel_args_t _a, * a = &_a; |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 665 | u32 tunnel_sw_if_index; |
| 666 | |
| 667 | /* Cant "universally zero init" (={0}) due to GCC bug 53119 */ |
| 668 | memset(&src, 0, sizeof src); |
| 669 | memset(&dst, 0, sizeof dst); |
| 670 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 671 | /* Get a line of input. */ |
| 672 | if (! unformat_user (input, unformat_line_input, line_input)) |
| 673 | return 0; |
| 674 | |
| 675 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { |
| 676 | if (unformat (line_input, "del")) |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 677 | { |
| 678 | is_add = 0; |
| 679 | } |
| 680 | else if (unformat (line_input, "src %U", |
| 681 | unformat_ip4_address, &src.ip4)) |
| 682 | { |
| 683 | src_set = 1; |
| 684 | ipv4_set = 1; |
| 685 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 686 | else if (unformat (line_input, "dst %U", |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 687 | unformat_ip4_address, &dst.ip4)) |
| 688 | { |
| 689 | dst_set = 1; |
| 690 | ipv4_set = 1; |
| 691 | } |
| 692 | else if (unformat (line_input, "src %U", |
| 693 | unformat_ip6_address, &src.ip6)) |
| 694 | { |
| 695 | src_set = 1; |
| 696 | ipv6_set = 1; |
| 697 | } |
| 698 | else if (unformat (line_input, "dst %U", |
| 699 | unformat_ip6_address, &dst.ip6)) |
| 700 | { |
| 701 | dst_set = 1; |
| 702 | ipv6_set = 1; |
| 703 | } |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 704 | else if (unformat (line_input, "group %U %U", |
| 705 | unformat_ip4_address, &dst.ip4, |
| 706 | unformat_vnet_sw_interface, |
| 707 | vnet_get_main(), &mcast_sw_if_index)) |
| 708 | { |
| 709 | grp_set = dst_set = 1; |
| 710 | ipv4_set = 1; |
| 711 | } |
| 712 | else if (unformat (line_input, "group %U %U", |
| 713 | unformat_ip6_address, &dst.ip6, |
| 714 | unformat_vnet_sw_interface, |
| 715 | vnet_get_main(), &mcast_sw_if_index)) |
| 716 | { |
| 717 | grp_set = dst_set = 1; |
| 718 | ipv6_set = 1; |
| 719 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 720 | else if (unformat (line_input, "encap-vrf-id %d", &tmp)) |
| 721 | { |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 722 | if (ipv6_set) |
| 723 | encap_fib_index = fib6_index_from_fib_id (tmp); |
| 724 | else |
| 725 | encap_fib_index = fib4_index_from_fib_id (tmp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 726 | if (encap_fib_index == ~0) |
| 727 | return clib_error_return (0, "nonexistent encap-vrf-id %d", tmp); |
| 728 | } |
| 729 | else if (unformat (line_input, "decap-next %U", unformat_decap_next, |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 730 | &decap_next_index, ipv4_set)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 731 | ; |
| 732 | else if (unformat (line_input, "vni %d", &vni)) |
| 733 | { |
| 734 | if (vni >> 24) |
| 735 | return clib_error_return (0, "vni %d out of range", vni); |
| 736 | } |
| 737 | else |
| 738 | return clib_error_return (0, "parse error: '%U'", |
| 739 | format_unformat_error, line_input); |
| 740 | } |
| 741 | |
| 742 | unformat_free (line_input); |
| 743 | |
| 744 | if (src_set == 0) |
| 745 | return clib_error_return (0, "tunnel src address not specified"); |
| 746 | |
| 747 | if (dst_set == 0) |
| 748 | return clib_error_return (0, "tunnel dst address not specified"); |
| 749 | |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 750 | if (grp_set && !ip46_address_is_multicast(&dst)) |
| 751 | return clib_error_return (0, "tunnel group address not multicast"); |
| 752 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 753 | if (grp_set == 0 && ip46_address_is_multicast(&dst)) |
| 754 | return clib_error_return (0, "dst address must be unicast"); |
| 755 | |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 756 | if (grp_set && mcast_sw_if_index == ~0) |
| 757 | return clib_error_return (0, "tunnel nonexistent multicast device"); |
| 758 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 759 | if (ipv4_set && ipv6_set) |
| 760 | return clib_error_return (0, "both IPv4 and IPv6 addresses specified"); |
| 761 | |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 762 | if (ip46_address_cmp(&src, &dst) == 0) |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 763 | return clib_error_return (0, "src and dst addresses are identical"); |
| 764 | |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 765 | if (decap_next_index == ~0) |
| 766 | return clib_error_return (0, "next node not found"); |
| 767 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 768 | if (vni == 0) |
| 769 | return clib_error_return (0, "vni not specified"); |
| 770 | |
| 771 | memset (a, 0, sizeof (*a)); |
| 772 | |
| 773 | a->is_add = is_add; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 774 | a->is_ip6 = ipv6_set; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 775 | |
| 776 | #define _(x) a->x = x; |
| 777 | foreach_copy_field; |
| 778 | #undef _ |
| 779 | |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 780 | rv = vnet_vxlan_add_del_tunnel (a, &tunnel_sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 781 | |
| 782 | switch(rv) |
| 783 | { |
| 784 | case 0: |
Pierre Pfister | 78ea9c2 | 2016-05-23 12:51:54 +0100 | [diff] [blame] | 785 | if (is_add) |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 786 | vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 787 | vnet_get_main(), tunnel_sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 788 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 789 | |
| 790 | case VNET_API_ERROR_TUNNEL_EXIST: |
| 791 | return clib_error_return (0, "tunnel already exists..."); |
| 792 | |
| 793 | case VNET_API_ERROR_NO_SUCH_ENTRY: |
| 794 | return clib_error_return (0, "tunnel does not exist..."); |
| 795 | |
| 796 | default: |
| 797 | return clib_error_return |
| 798 | (0, "vnet_vxlan_add_del_tunnel returned %d", rv); |
| 799 | } |
| 800 | |
| 801 | return 0; |
| 802 | } |
| 803 | |
Billy McFall | c5d9cda | 2016-09-14 10:39:58 -0400 | [diff] [blame] | 804 | /*? |
| 805 | * Add or delete a VXLAN Tunnel. |
| 806 | * |
| 807 | * VXLAN provides the features needed to allow L2 bridge domains (BDs) |
| 808 | * to span multiple servers. This is done by building an L2 overlay on |
| 809 | * top of an L3 network underlay using VXLAN tunnels. |
| 810 | * |
| 811 | * This makes it possible for servers to be co-located in the same data |
| 812 | * center or be separated geographically as long as they are reachable |
| 813 | * through the underlay L3 network. |
| 814 | * |
| 815 | * You can refer to this kind of L2 overlay bridge domain as a VXLAN |
| 816 | * (Virtual eXtensible VLAN) segment. |
| 817 | * |
| 818 | * @cliexpar |
| 819 | * Example of how to create a VXLAN Tunnel: |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 820 | * @cliexcmd{create vxlan tunnel src 10.0.3.1 dst 10.0.3.3 vni 13 encap-vrf-id 7} |
Billy McFall | c5d9cda | 2016-09-14 10:39:58 -0400 | [diff] [blame] | 821 | * Example of how to delete a VXLAN Tunnel: |
| 822 | * @cliexcmd{create vxlan tunnel src 10.0.3.1 dst 10.0.3.3 vni 13 del} |
| 823 | ?*/ |
| 824 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 825 | VLIB_CLI_COMMAND (create_vxlan_tunnel_command, static) = { |
| 826 | .path = "create vxlan tunnel", |
| 827 | .short_help = |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 828 | "create vxlan tunnel src <local-vtep-addr>" |
| 829 | " {dst <remote-vtep-addr>|group <mcast-vtep-addr> <intf-name>} vni <nn>" |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 830 | " [encap-vrf-id <nn>] [decap-next [l2|node <name>]] [del]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 831 | .function = vxlan_add_del_tunnel_command_fn, |
| 832 | }; |
Billy McFall | c5d9cda | 2016-09-14 10:39:58 -0400 | [diff] [blame] | 833 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 834 | |
| 835 | static clib_error_t * |
| 836 | show_vxlan_tunnel_command_fn (vlib_main_t * vm, |
| 837 | unformat_input_t * input, |
| 838 | vlib_cli_command_t * cmd) |
| 839 | { |
| 840 | vxlan_main_t * vxm = &vxlan_main; |
| 841 | vxlan_tunnel_t * t; |
| 842 | |
| 843 | if (pool_elts (vxm->tunnels) == 0) |
| 844 | vlib_cli_output (vm, "No vxlan tunnels configured..."); |
| 845 | |
| 846 | pool_foreach (t, vxm->tunnels, |
| 847 | ({ |
| 848 | vlib_cli_output (vm, "%U", format_vxlan_tunnel, t); |
| 849 | })); |
| 850 | |
| 851 | return 0; |
| 852 | } |
| 853 | |
Billy McFall | c5d9cda | 2016-09-14 10:39:58 -0400 | [diff] [blame] | 854 | /*? |
| 855 | * Display all the VXLAN Tunnel entries. |
| 856 | * |
| 857 | * @cliexpar |
| 858 | * Example of how to display the VXLAN Tunnel entries: |
| 859 | * @cliexstart{show vxlan tunnel} |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 860 | * [0] src 10.0.3.1 dst 10.0.3.3 vni 13 encap_fib_index 0 sw_if_index 5 decap_next l2 |
Billy McFall | c5d9cda | 2016-09-14 10:39:58 -0400 | [diff] [blame] | 861 | * @cliexend |
| 862 | ?*/ |
| 863 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 864 | VLIB_CLI_COMMAND (show_vxlan_tunnel_command, static) = { |
| 865 | .path = "show vxlan tunnel", |
Billy McFall | c5d9cda | 2016-09-14 10:39:58 -0400 | [diff] [blame] | 866 | .short_help = "show vxlan tunnel", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 867 | .function = show_vxlan_tunnel_command_fn, |
| 868 | }; |
Billy McFall | c5d9cda | 2016-09-14 10:39:58 -0400 | [diff] [blame] | 869 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 870 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 871 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 872 | clib_error_t *vxlan_init (vlib_main_t *vm) |
| 873 | { |
| 874 | vxlan_main_t * vxm = &vxlan_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 875 | |
| 876 | vxm->vnet_main = vnet_get_main(); |
| 877 | vxm->vlib_main = vm; |
| 878 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 879 | /* initialize the ip6 hash */ |
| 880 | vxm->vxlan6_tunnel_by_key = hash_create_mem(0, |
| 881 | sizeof(vxlan6_tunnel_key_t), |
| 882 | sizeof(uword)); |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 883 | vxm->vtep6 = hash_create_mem(0, |
| 884 | sizeof(ip6_address_t), |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 885 | sizeof(uword)); |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 886 | vxm->mcast_shared = hash_create_mem(0, |
| 887 | sizeof(ip46_address_t), |
| 888 | sizeof(mcast_shared_t)); |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 889 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 890 | udp_register_dst_port (vm, UDP_DST_PORT_vxlan, |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 891 | vxlan4_input_node.index, /* is_ip4 */ 1); |
| 892 | udp_register_dst_port (vm, UDP_DST_PORT_vxlan6, |
| 893 | vxlan6_input_node.index, /* is_ip4 */ 0); |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 894 | |
| 895 | fib_node_register_type(FIB_NODE_TYPE_VXLAN_TUNNEL, &vxlan_vft); |
| 896 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 897 | return 0; |
| 898 | } |
| 899 | |
| 900 | VLIB_INIT_FUNCTION(vxlan_init); |