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> |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 19 | #include <vnet/mfib/mfib_table.h> |
| 20 | #include <vnet/adj/adj_mcast.h> |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 21 | #include <vlib/vlib.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 22 | |
Billy McFall | c5d9cda | 2016-09-14 10:39:58 -0400 | [diff] [blame] | 23 | /** |
| 24 | * @file |
| 25 | * @brief VXLAN. |
| 26 | * |
| 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. |
| 30 | * |
| 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. |
| 34 | * |
| 35 | * You can refer to this kind of L2 overlay bridge domain as a VXLAN |
| 36 | * (Virtual eXtensible VLAN) segment. |
| 37 | */ |
| 38 | |
| 39 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 40 | vxlan_main_t vxlan_main; |
| 41 | |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 42 | static u8 * format_decap_next (u8 * s, va_list * args) |
| 43 | { |
| 44 | u32 next_index = va_arg (*args, u32); |
| 45 | |
| 46 | switch (next_index) |
| 47 | { |
| 48 | case VXLAN_INPUT_NEXT_DROP: |
| 49 | return format (s, "drop"); |
| 50 | case VXLAN_INPUT_NEXT_L2_INPUT: |
| 51 | return format (s, "l2"); |
| 52 | default: |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 53 | return format (s, "index %d", next_index); |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 54 | } |
| 55 | return s; |
| 56 | } |
| 57 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 58 | u8 * format_vxlan_tunnel (u8 * s, va_list * args) |
| 59 | { |
| 60 | vxlan_tunnel_t * t = va_arg (*args, vxlan_tunnel_t *); |
| 61 | vxlan_main_t * ngm = &vxlan_main; |
| 62 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 63 | 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] | 64 | t - ngm->tunnels, |
Damjan Marion | 86be487 | 2016-05-24 23:19:11 +0200 | [diff] [blame] | 65 | format_ip46_address, &t->src, IP46_TYPE_ANY, |
| 66 | format_ip46_address, &t->dst, IP46_TYPE_ANY, |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 67 | t->vni, t->sw_if_index); |
| 68 | |
| 69 | if (ip46_address_is_multicast (&t->dst)) |
| 70 | s = format (s, "mcast_sw_if_index %d ", t->mcast_sw_if_index); |
| 71 | |
| 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); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | return s; |
| 76 | } |
| 77 | |
| 78 | static u8 * format_vxlan_name (u8 * s, va_list * args) |
| 79 | { |
| 80 | u32 dev_instance = va_arg (*args, u32); |
| 81 | return format (s, "vxlan_tunnel%d", dev_instance); |
| 82 | } |
| 83 | |
| 84 | static uword dummy_interface_tx (vlib_main_t * vm, |
| 85 | vlib_node_runtime_t * node, |
| 86 | vlib_frame_t * frame) |
| 87 | { |
| 88 | clib_warning ("you shouldn't be here, leaking buffers..."); |
| 89 | return frame->n_vectors; |
| 90 | } |
| 91 | |
Pavel Kotucek | 988a7c4 | 2016-02-29 15:03:08 +0100 | [diff] [blame] | 92 | static clib_error_t * |
| 93 | vxlan_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) |
| 94 | { |
| 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); |
| 97 | else |
| 98 | vnet_hw_interface_set_flags (vnm, hw_if_index, 0); |
| 99 | |
| 100 | return /* no error */ 0; |
| 101 | } |
| 102 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 103 | VNET_DEVICE_CLASS (vxlan_device_class,static) = { |
| 104 | .name = "VXLAN", |
| 105 | .format_device_name = format_vxlan_name, |
| 106 | .format_tx_trace = format_vxlan_encap_trace, |
| 107 | .tx_function = dummy_interface_tx, |
Pavel Kotucek | 988a7c4 | 2016-02-29 15:03:08 +0100 | [diff] [blame] | 108 | .admin_up_down_function = vxlan_interface_admin_up_down, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | static u8 * format_vxlan_header_with_length (u8 * s, va_list * args) |
| 112 | { |
| 113 | u32 dev_instance = va_arg (*args, u32); |
| 114 | s = format (s, "unimplemented dev %u", dev_instance); |
| 115 | return s; |
| 116 | } |
| 117 | |
| 118 | VNET_HW_INTERFACE_CLASS (vxlan_hw_class) = { |
| 119 | .name = "VXLAN", |
| 120 | .format_header = format_vxlan_header_with_length, |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 121 | .build_rewrite = default_build_rewrite, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 124 | static void |
| 125 | vxlan_tunnel_restack_dpo(vxlan_tunnel_t * t) |
| 126 | { |
| 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; |
| 132 | |
| 133 | fib_entry_contribute_forwarding (t->fib_entry_index, forw_type, &dpo); |
| 134 | dpo_stack_from_node (encap_index, &t->next_dpo, &dpo); |
| 135 | dpo_reset(&dpo); |
| 136 | } |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 137 | |
| 138 | static vxlan_tunnel_t * |
| 139 | vxlan_tunnel_from_fib_node (fib_node_t *node) |
| 140 | { |
| 141 | #if (CLIB_DEBUG > 0) |
| 142 | ASSERT(FIB_NODE_TYPE_VXLAN_TUNNEL == node->fn_type); |
| 143 | #endif |
| 144 | return ((vxlan_tunnel_t*) (((char*)node) - |
| 145 | STRUCT_OFFSET_OF(vxlan_tunnel_t, node))); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Function definition to backwalk a FIB node - |
| 150 | * Here we will restack the new dpo of VXLAN DIP to encap node. |
| 151 | */ |
| 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) |
| 155 | { |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 156 | vxlan_tunnel_restack_dpo(vxlan_tunnel_from_fib_node(node)); |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 157 | return (FIB_NODE_BACK_WALK_CONTINUE); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Function definition to get a FIB node from its index |
| 162 | */ |
| 163 | static fib_node_t* |
| 164 | vxlan_tunnel_fib_node_get (fib_node_index_t index) |
| 165 | { |
| 166 | vxlan_tunnel_t * t; |
| 167 | vxlan_main_t * vxm = &vxlan_main; |
| 168 | |
| 169 | t = pool_elt_at_index(vxm->tunnels, index); |
| 170 | |
| 171 | return (&t->node); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Function definition to inform the FIB node that its last lock has gone. |
| 176 | */ |
| 177 | static void |
| 178 | vxlan_tunnel_last_lock_gone (fib_node_t *node) |
| 179 | { |
| 180 | /* |
| 181 | * The VXLAN tunnel is a root of the graph. As such |
| 182 | * it never has children and thus is never locked. |
| 183 | */ |
| 184 | ASSERT(0); |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * Virtual function table registered by VXLAN tunnels |
| 189 | * for participation in the FIB object graph. |
| 190 | */ |
| 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, |
| 195 | }; |
| 196 | |
| 197 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 198 | #define foreach_copy_field \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 199 | _(vni) \ |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 200 | _(mcast_sw_if_index) \ |
| 201 | _(encap_fib_index) \ |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 202 | _(decap_next_index) \ |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 203 | _(src) \ |
| 204 | _(dst) |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 205 | |
| 206 | static int vxlan4_rewrite (vxlan_tunnel_t * t) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 207 | { |
| 208 | u8 *rw = 0; |
| 209 | ip4_header_t * ip0; |
| 210 | ip4_vxlan_header_t * h0; |
| 211 | int len = sizeof (*h0); |
| 212 | |
| 213 | vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES); |
| 214 | |
| 215 | h0 = (ip4_vxlan_header_t *) rw; |
| 216 | |
| 217 | /* Fixed portion of the (outer) ip4 header */ |
| 218 | ip0 = &h0->ip4; |
| 219 | ip0->ip_version_and_header_length = 0x45; |
| 220 | ip0->ttl = 254; |
| 221 | ip0->protocol = IP_PROTOCOL_UDP; |
| 222 | |
| 223 | /* we fix up the ip4 header length and checksum after-the-fact */ |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 224 | ip0->src_address.as_u32 = t->src.ip4.as_u32; |
| 225 | ip0->dst_address.as_u32 = t->dst.ip4.as_u32; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 226 | ip0->checksum = ip4_header_checksum (ip0); |
| 227 | |
| 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); |
| 231 | |
| 232 | /* VXLAN header */ |
| 233 | vnet_set_vni_and_flags(&h0->vxlan, t->vni); |
| 234 | |
| 235 | t->rewrite = rw; |
| 236 | return (0); |
| 237 | } |
| 238 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 239 | static int vxlan6_rewrite (vxlan_tunnel_t * t) |
| 240 | { |
| 241 | u8 *rw = 0; |
| 242 | ip6_header_t * ip0; |
| 243 | ip6_vxlan_header_t * h0; |
| 244 | int len = sizeof (*h0); |
| 245 | |
| 246 | vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES); |
| 247 | |
| 248 | h0 = (ip6_vxlan_header_t *) rw; |
| 249 | |
| 250 | /* Fixed portion of the (outer) ip6 header */ |
| 251 | ip0 = &h0->ip6; |
| 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; |
| 255 | |
| 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]; |
| 260 | |
| 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); |
| 264 | |
| 265 | /* VXLAN header */ |
| 266 | vnet_set_vni_and_flags(&h0->vxlan, t->vni); |
| 267 | |
| 268 | t->rewrite = rw; |
| 269 | return (0); |
| 270 | } |
| 271 | |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 272 | static int vxlan_check_decap_next(vxlan_main_t * vxm, u32 is_ip6, u32 decap_next_index) |
| 273 | { |
| 274 | vlib_main_t * vm = vxm->vlib_main; |
| 275 | vlib_node_runtime_t *r; |
| 276 | |
| 277 | if(!is_ip6) |
| 278 | { |
| 279 | r = vlib_node_get_runtime (vm, vxlan4_input_node.index); |
| 280 | if(decap_next_index >= r->n_next_nodes) |
| 281 | return 1; |
| 282 | } |
| 283 | else |
| 284 | { |
| 285 | r = vlib_node_get_runtime (vm, vxlan6_input_node.index); |
| 286 | if(decap_next_index >= r->n_next_nodes) |
| 287 | return 1; |
| 288 | } |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 293 | static void |
| 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); |
| 299 | } |
| 300 | |
| 301 | static void |
| 302 | hash_unset_key_free (uword ** h, void * key) { |
| 303 | hash_pair_t * hp = hash_get_pair_mem (*h, key); |
| 304 | ASSERT (hp); |
| 305 | key = uword_to_pointer (hp->key, void *); |
| 306 | hash_unset_mem (*h, key); |
| 307 | clib_mem_free (key); |
| 308 | } |
| 309 | |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 310 | static uword |
| 311 | vtep_addr_ref(ip46_address_t *ip) |
| 312 | { |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 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); |
| 316 | if (vtep) |
| 317 | return ++(*vtep); |
| 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); |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 321 | return 1; |
| 322 | } |
| 323 | |
| 324 | static uword |
| 325 | vtep_addr_unref(ip46_address_t *ip) |
| 326 | { |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 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); |
| 330 | ASSERT(vtep); |
| 331 | if (--(*vtep) != 0) |
| 332 | return *vtep; |
| 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); |
| 336 | return 0; |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 337 | } |
| 338 | |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 339 | typedef CLIB_PACKED(union { |
| 340 | struct { |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 341 | fib_node_index_t mfib_entry_index; |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 342 | adj_index_t mcast_adj_index; |
| 343 | }; |
| 344 | u64 as_u64; |
| 345 | }) mcast_shared_t; |
| 346 | |
| 347 | static inline mcast_shared_t |
| 348 | mcast_shared_get(ip46_address_t * ip) |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 349 | { |
| 350 | ASSERT(ip46_address_is_multicast(ip)); |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 351 | uword * p = hash_get_mem (vxlan_main.mcast_shared, ip); |
| 352 | ASSERT(p); |
| 353 | return (mcast_shared_t) { .as_u64 = *p }; |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 356 | static inline void |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 357 | mcast_shared_add(ip46_address_t *dst, |
| 358 | fib_node_index_t mfei, |
| 359 | adj_index_t ai) |
| 360 | { |
| 361 | mcast_shared_t new_ep = { |
| 362 | .mcast_adj_index = ai, |
| 363 | .mfib_entry_index = mfei, |
| 364 | }; |
| 365 | |
| 366 | hash_set_key_copy (&vxlan_main.mcast_shared, dst, new_ep.as_u64); |
| 367 | } |
| 368 | |
| 369 | static inline void |
| 370 | mcast_shared_remove(ip46_address_t *dst) |
| 371 | { |
| 372 | mcast_shared_t ep = mcast_shared_get(dst); |
| 373 | |
| 374 | adj_unlock(ep.mcast_adj_index); |
| 375 | mfib_table_entry_delete_index(ep.mfib_entry_index, |
| 376 | MFIB_SOURCE_VXLAN); |
| 377 | |
| 378 | hash_unset_key_free (&vxlan_main.mcast_shared, dst); |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 379 | } |
| 380 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 381 | int vnet_vxlan_add_del_tunnel |
| 382 | (vnet_vxlan_add_del_tunnel_args_t *a, u32 * sw_if_indexp) |
| 383 | { |
| 384 | vxlan_main_t * vxm = &vxlan_main; |
| 385 | vxlan_tunnel_t *t = 0; |
| 386 | vnet_main_t * vnm = vxm->vnet_main; |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 387 | uword * p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 388 | u32 hw_if_index = ~0; |
| 389 | u32 sw_if_index = ~0; |
| 390 | int rv; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 391 | vxlan4_tunnel_key_t key4; |
| 392 | vxlan6_tunnel_key_t key6; |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 393 | u32 is_ip6 = a->is_ip6; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 394 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 395 | if (!is_ip6) |
| 396 | { |
| 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); |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 400 | } |
| 401 | else |
| 402 | { |
| 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); |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 407 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 408 | |
| 409 | if (a->is_add) |
| 410 | { |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 411 | l2input_main_t * l2im = &l2input_main; |
| 412 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 413 | /* adding a tunnel: tunnel must not already exist */ |
| 414 | if (p) |
| 415 | return VNET_API_ERROR_TUNNEL_EXIST; |
| 416 | |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 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; |
| 422 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 423 | pool_get_aligned (vxm->tunnels, t, CLIB_CACHE_LINE_BYTES); |
| 424 | memset (t, 0, sizeof (*t)); |
| 425 | |
| 426 | /* copy from arg structure */ |
| 427 | #define _(x) t->x = a->x; |
| 428 | foreach_copy_field; |
| 429 | #undef _ |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 430 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 431 | if (!is_ip6) |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 432 | rv = vxlan4_rewrite (t); |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 433 | else |
| 434 | rv = vxlan6_rewrite (t); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 435 | |
| 436 | if (rv) |
| 437 | { |
| 438 | pool_put (vxm->tunnels, t); |
| 439 | return rv; |
| 440 | } |
| 441 | |
Eyal Bari | 5ac9bf5 | 2017-01-02 14:29:21 +0200 | [diff] [blame] | 442 | /* copy the key */ |
| 443 | if (is_ip6) |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 444 | 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] | 445 | else |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 446 | 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] | 447 | |
| 448 | vnet_hw_interface_t * hi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 449 | if (vec_len (vxm->free_vxlan_tunnel_hw_if_indices) > 0) |
| 450 | { |
| 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; |
| 455 | |
| 456 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 457 | hi->dev_instance = t - vxm->tunnels; |
| 458 | hi->hw_instance = hi->dev_instance; |
| 459 | |
| 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); |
| 470 | } |
| 471 | else |
| 472 | { |
| 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); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | t->hw_if_index = hw_if_index; |
| 480 | t->sw_if_index = sw_if_index = hi->sw_if_index; |
| 481 | |
Dave Wallace | 60231f3 | 2015-12-17 21:04:30 -0500 | [diff] [blame] | 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; |
| 484 | |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 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; |
John Lo | 3ef822e | 2016-06-07 09:14:07 -0400 | [diff] [blame] | 489 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 490 | vnet_sw_interface_set_flags (vnm, sw_if_index, |
| 491 | VNET_SW_INTERFACE_FLAG_ADMIN_UP); |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 492 | fib_node_init(&t->node, FIB_NODE_TYPE_VXLAN_TUNNEL); |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 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; |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 497 | |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 498 | fib_prefix_from_ip46_addr(&t->dst, &tun_dst_pfx); |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 499 | if (!ip46_address_is_multicast(&t->dst)) |
| 500 | { |
| 501 | /* Unicast tunnel - |
| 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 |
| 506 | */ |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 507 | vtep_addr_ref(&t->src); |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 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); |
| 514 | } |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 515 | else |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 516 | { |
| 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 |
| 521 | */ |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 522 | fib_protocol_t fp = (is_ip6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4; |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 523 | dpo_id_t dpo = DPO_INVALID; |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 524 | mcast_shared_t ep; |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 525 | |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 526 | if (vtep_addr_ref(&t->dst) == 1) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 527 | { |
| 528 | fib_node_index_t mfei; |
| 529 | adj_index_t ai; |
| 530 | fib_route_path_t path = { |
| 531 | .frp_proto = fp, |
| 532 | .frp_addr = zero_addr, |
| 533 | .frp_sw_if_index = 0xffffffff, |
| 534 | .frp_fib_index = ~0, |
| 535 | .frp_weight = 0, |
| 536 | .frp_flags = FIB_ROUTE_PATH_LOCAL, |
| 537 | }; |
| 538 | const mfib_prefix_t mpfx = { |
| 539 | .fp_proto = fp, |
| 540 | .fp_len = (is_ip6 ? 128 : 32), |
| 541 | .fp_grp_addr = tun_dst_pfx.fp_addr, |
| 542 | }; |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 543 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 544 | /* |
| 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 |
| 548 | */ |
| 549 | mfib_table_entry_path_update(t->encap_fib_index, |
| 550 | &mpfx, |
| 551 | MFIB_SOURCE_VXLAN, |
| 552 | &path, |
| 553 | MFIB_ITF_FLAG_FORWARD); |
| 554 | |
| 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, |
| 558 | &mpfx, |
| 559 | MFIB_SOURCE_VXLAN, |
| 560 | &path, |
| 561 | MFIB_ITF_FLAG_ACCEPT); |
| 562 | |
| 563 | /* |
| 564 | * Create the mcast adjacency to send traffic to the group |
| 565 | */ |
| 566 | ai = adj_mcast_add_or_lock(fp, |
| 567 | fib_proto_to_link(fp), |
| 568 | a->mcast_sw_if_index); |
| 569 | |
| 570 | /* |
| 571 | * create a new end-point |
| 572 | */ |
| 573 | mcast_shared_add(&t->dst, mfei, ai); |
| 574 | } |
| 575 | |
| 576 | ep = mcast_shared_get(&t->dst); |
| 577 | |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 578 | /* Stack shared mcast dst mac addr rewrite on encap */ |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 579 | dpo_set (&dpo, DPO_ADJACENCY, |
| 580 | fib_proto_to_dpo(fp), |
| 581 | ep.mcast_adj_index); |
| 582 | |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 583 | dpo_stack_from_node (encap_index, &t->next_dpo, &dpo); |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 584 | dpo_reset (&dpo); |
| 585 | flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER; |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 586 | } |
| 587 | |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 588 | /* Set vxlan tunnel output node */ |
| 589 | hi->output_node_index = encap_index; |
| 590 | |
| 591 | 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] | 592 | } |
| 593 | else |
| 594 | { |
| 595 | /* deleting a tunnel: tunnel must exist */ |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 596 | if (!p) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 597 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 598 | |
| 599 | t = pool_elt_at_index (vxm->tunnels, p[0]); |
| 600 | |
| 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); |
| 605 | |
Dave Wallace | 60231f3 | 2015-12-17 21:04:30 -0500 | [diff] [blame] | 606 | vxm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0; |
| 607 | |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 608 | if (!is_ip6) |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 609 | hash_unset (vxm->vxlan4_tunnel_by_key, key4.as_u64); |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 610 | else |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 611 | hash_unset_key_free (&vxm->vxlan6_tunnel_by_key, &key6); |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 612 | |
| 613 | if (!ip46_address_is_multicast(&t->dst)) |
| 614 | { |
Eyal Bari | 5ac9bf5 | 2017-01-02 14:29:21 +0200 | [diff] [blame] | 615 | vtep_addr_unref(&t->src); |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 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); |
| 618 | } |
| 619 | else if (vtep_addr_unref(&t->dst) == 0) |
| 620 | { |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 621 | mcast_shared_remove(&t->dst); |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | fib_node_deinit(&t->node); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 625 | vec_free (t->rewrite); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 626 | pool_put (vxm->tunnels, t); |
| 627 | } |
| 628 | |
| 629 | if (sw_if_indexp) |
| 630 | *sw_if_indexp = sw_if_index; |
| 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 635 | static u32 fib4_index_from_fib_id (u32 fib_id) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 636 | { |
| 637 | ip4_main_t * im = &ip4_main; |
| 638 | uword * p; |
| 639 | |
| 640 | p = hash_get (im->fib_index_by_table_id, fib_id); |
| 641 | if (!p) |
| 642 | return ~0; |
| 643 | |
| 644 | return p[0]; |
| 645 | } |
| 646 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 647 | static u32 fib6_index_from_fib_id (u32 fib_id) |
| 648 | { |
| 649 | ip6_main_t * im = &ip6_main; |
| 650 | uword * p; |
| 651 | |
| 652 | p = hash_get (im->fib_index_by_table_id, fib_id); |
| 653 | if (!p) |
| 654 | return ~0; |
| 655 | |
| 656 | return p[0]; |
| 657 | } |
| 658 | |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 659 | static uword get_decap_next_for_node(u32 node_index, u32 ipv4_set) |
| 660 | { |
| 661 | vxlan_main_t * vxm = &vxlan_main; |
| 662 | vlib_main_t * vm = vxm->vlib_main; |
| 663 | uword next_index = ~0; |
| 664 | |
| 665 | if (ipv4_set) |
| 666 | { |
| 667 | next_index = vlib_node_add_next (vm, vxlan4_input_node.index, node_index); |
| 668 | } |
| 669 | else |
| 670 | { |
| 671 | next_index = vlib_node_add_next (vm, vxlan6_input_node.index, node_index); |
| 672 | } |
| 673 | |
| 674 | return next_index; |
| 675 | } |
| 676 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 677 | static uword unformat_decap_next (unformat_input_t * input, va_list * args) |
| 678 | { |
| 679 | u32 * result = va_arg (*args, u32 *); |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 680 | u32 ipv4_set = va_arg (*args, int); |
| 681 | vxlan_main_t * vxm = &vxlan_main; |
| 682 | vlib_main_t * vm = vxm->vlib_main; |
| 683 | u32 node_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 684 | u32 tmp; |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 685 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 686 | if (unformat (input, "l2")) |
| 687 | *result = VXLAN_INPUT_NEXT_L2_INPUT; |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 688 | else if (unformat (input, "node %U", unformat_vlib_node, vm, &node_index)) |
| 689 | *result = get_decap_next_for_node(node_index, ipv4_set); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 690 | else if (unformat (input, "%d", &tmp)) |
| 691 | *result = tmp; |
| 692 | else |
| 693 | return 0; |
| 694 | return 1; |
| 695 | } |
| 696 | |
| 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) |
| 701 | { |
| 702 | unformat_input_t _line_input, * line_input = &_line_input; |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 703 | ip46_address_t src , dst; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 704 | u8 is_add = 1; |
| 705 | u8 src_set = 0; |
| 706 | u8 dst_set = 0; |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 707 | u8 grp_set = 0; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 708 | u8 ipv4_set = 0; |
| 709 | u8 ipv6_set = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 710 | u32 encap_fib_index = 0; |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 711 | u32 mcast_sw_if_index = ~0; |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 712 | u32 decap_next_index = VXLAN_INPUT_NEXT_L2_INPUT; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 713 | u32 vni = 0; |
| 714 | u32 tmp; |
| 715 | int rv; |
| 716 | vnet_vxlan_add_del_tunnel_args_t _a, * a = &_a; |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 717 | u32 tunnel_sw_if_index; |
| 718 | |
| 719 | /* Cant "universally zero init" (={0}) due to GCC bug 53119 */ |
| 720 | memset(&src, 0, sizeof src); |
| 721 | memset(&dst, 0, sizeof dst); |
| 722 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 723 | /* Get a line of input. */ |
| 724 | if (! unformat_user (input, unformat_line_input, line_input)) |
| 725 | return 0; |
| 726 | |
| 727 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { |
| 728 | if (unformat (line_input, "del")) |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 729 | { |
| 730 | is_add = 0; |
| 731 | } |
| 732 | else if (unformat (line_input, "src %U", |
| 733 | unformat_ip4_address, &src.ip4)) |
| 734 | { |
| 735 | src_set = 1; |
| 736 | ipv4_set = 1; |
| 737 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 738 | else if (unformat (line_input, "dst %U", |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 739 | unformat_ip4_address, &dst.ip4)) |
| 740 | { |
| 741 | dst_set = 1; |
| 742 | ipv4_set = 1; |
| 743 | } |
| 744 | else if (unformat (line_input, "src %U", |
| 745 | unformat_ip6_address, &src.ip6)) |
| 746 | { |
| 747 | src_set = 1; |
| 748 | ipv6_set = 1; |
| 749 | } |
| 750 | else if (unformat (line_input, "dst %U", |
| 751 | unformat_ip6_address, &dst.ip6)) |
| 752 | { |
| 753 | dst_set = 1; |
| 754 | ipv6_set = 1; |
| 755 | } |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 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)) |
| 760 | { |
| 761 | grp_set = dst_set = 1; |
| 762 | ipv4_set = 1; |
| 763 | } |
| 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)) |
| 768 | { |
| 769 | grp_set = dst_set = 1; |
| 770 | ipv6_set = 1; |
| 771 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 772 | else if (unformat (line_input, "encap-vrf-id %d", &tmp)) |
| 773 | { |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 774 | if (ipv6_set) |
| 775 | encap_fib_index = fib6_index_from_fib_id (tmp); |
| 776 | else |
| 777 | encap_fib_index = fib4_index_from_fib_id (tmp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 778 | if (encap_fib_index == ~0) |
| 779 | return clib_error_return (0, "nonexistent encap-vrf-id %d", tmp); |
| 780 | } |
| 781 | else if (unformat (line_input, "decap-next %U", unformat_decap_next, |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 782 | &decap_next_index, ipv4_set)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 783 | ; |
| 784 | else if (unformat (line_input, "vni %d", &vni)) |
| 785 | { |
| 786 | if (vni >> 24) |
| 787 | return clib_error_return (0, "vni %d out of range", vni); |
| 788 | } |
| 789 | else |
| 790 | return clib_error_return (0, "parse error: '%U'", |
| 791 | format_unformat_error, line_input); |
| 792 | } |
| 793 | |
| 794 | unformat_free (line_input); |
| 795 | |
| 796 | if (src_set == 0) |
| 797 | return clib_error_return (0, "tunnel src address not specified"); |
| 798 | |
| 799 | if (dst_set == 0) |
| 800 | return clib_error_return (0, "tunnel dst address not specified"); |
| 801 | |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 802 | if (grp_set && !ip46_address_is_multicast(&dst)) |
| 803 | return clib_error_return (0, "tunnel group address not multicast"); |
| 804 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 805 | if (grp_set == 0 && ip46_address_is_multicast(&dst)) |
| 806 | return clib_error_return (0, "dst address must be unicast"); |
| 807 | |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 808 | if (grp_set && mcast_sw_if_index == ~0) |
| 809 | return clib_error_return (0, "tunnel nonexistent multicast device"); |
| 810 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 811 | if (ipv4_set && ipv6_set) |
| 812 | return clib_error_return (0, "both IPv4 and IPv6 addresses specified"); |
| 813 | |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 814 | if (ip46_address_cmp(&src, &dst) == 0) |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 815 | return clib_error_return (0, "src and dst addresses are identical"); |
| 816 | |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 817 | if (decap_next_index == ~0) |
| 818 | return clib_error_return (0, "next node not found"); |
| 819 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 820 | if (vni == 0) |
| 821 | return clib_error_return (0, "vni not specified"); |
| 822 | |
| 823 | memset (a, 0, sizeof (*a)); |
| 824 | |
| 825 | a->is_add = is_add; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 826 | a->is_ip6 = ipv6_set; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 827 | |
| 828 | #define _(x) a->x = x; |
| 829 | foreach_copy_field; |
| 830 | #undef _ |
| 831 | |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 832 | rv = vnet_vxlan_add_del_tunnel (a, &tunnel_sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 833 | |
| 834 | switch(rv) |
| 835 | { |
| 836 | case 0: |
Pierre Pfister | 78ea9c2 | 2016-05-23 12:51:54 +0100 | [diff] [blame] | 837 | if (is_add) |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 838 | vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 839 | vnet_get_main(), tunnel_sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 840 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 841 | |
| 842 | case VNET_API_ERROR_TUNNEL_EXIST: |
| 843 | return clib_error_return (0, "tunnel already exists..."); |
| 844 | |
| 845 | case VNET_API_ERROR_NO_SUCH_ENTRY: |
| 846 | return clib_error_return (0, "tunnel does not exist..."); |
| 847 | |
| 848 | default: |
| 849 | return clib_error_return |
| 850 | (0, "vnet_vxlan_add_del_tunnel returned %d", rv); |
| 851 | } |
| 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | |
Billy McFall | c5d9cda | 2016-09-14 10:39:58 -0400 | [diff] [blame] | 856 | /*? |
| 857 | * Add or delete a VXLAN Tunnel. |
| 858 | * |
| 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. |
| 862 | * |
| 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. |
| 866 | * |
| 867 | * You can refer to this kind of L2 overlay bridge domain as a VXLAN |
| 868 | * (Virtual eXtensible VLAN) segment. |
| 869 | * |
| 870 | * @cliexpar |
| 871 | * Example of how to create a VXLAN Tunnel: |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 872 | * @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] | 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} |
| 875 | ?*/ |
| 876 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 877 | VLIB_CLI_COMMAND (create_vxlan_tunnel_command, static) = { |
| 878 | .path = "create vxlan tunnel", |
| 879 | .short_help = |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 880 | "create vxlan tunnel src <local-vtep-addr>" |
| 881 | " {dst <remote-vtep-addr>|group <mcast-vtep-addr> <intf-name>} vni <nn>" |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 882 | " [encap-vrf-id <nn>] [decap-next [l2|node <name>]] [del]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 883 | .function = vxlan_add_del_tunnel_command_fn, |
| 884 | }; |
Billy McFall | c5d9cda | 2016-09-14 10:39:58 -0400 | [diff] [blame] | 885 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 886 | |
| 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) |
| 891 | { |
| 892 | vxlan_main_t * vxm = &vxlan_main; |
| 893 | vxlan_tunnel_t * t; |
| 894 | |
| 895 | if (pool_elts (vxm->tunnels) == 0) |
| 896 | vlib_cli_output (vm, "No vxlan tunnels configured..."); |
| 897 | |
| 898 | pool_foreach (t, vxm->tunnels, |
| 899 | ({ |
| 900 | vlib_cli_output (vm, "%U", format_vxlan_tunnel, t); |
| 901 | })); |
| 902 | |
| 903 | return 0; |
| 904 | } |
| 905 | |
Billy McFall | c5d9cda | 2016-09-14 10:39:58 -0400 | [diff] [blame] | 906 | /*? |
| 907 | * Display all the VXLAN Tunnel entries. |
| 908 | * |
| 909 | * @cliexpar |
| 910 | * Example of how to display the VXLAN Tunnel entries: |
| 911 | * @cliexstart{show vxlan tunnel} |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 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 |
Billy McFall | c5d9cda | 2016-09-14 10:39:58 -0400 | [diff] [blame] | 913 | * @cliexend |
| 914 | ?*/ |
| 915 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 916 | VLIB_CLI_COMMAND (show_vxlan_tunnel_command, static) = { |
| 917 | .path = "show vxlan tunnel", |
Billy McFall | c5d9cda | 2016-09-14 10:39:58 -0400 | [diff] [blame] | 918 | .short_help = "show vxlan tunnel", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 919 | .function = show_vxlan_tunnel_command_fn, |
| 920 | }; |
Billy McFall | c5d9cda | 2016-09-14 10:39:58 -0400 | [diff] [blame] | 921 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 922 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 923 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 924 | clib_error_t *vxlan_init (vlib_main_t *vm) |
| 925 | { |
| 926 | vxlan_main_t * vxm = &vxlan_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 927 | |
| 928 | vxm->vnet_main = vnet_get_main(); |
| 929 | vxm->vlib_main = vm; |
| 930 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 931 | /* initialize the ip6 hash */ |
| 932 | vxm->vxlan6_tunnel_by_key = hash_create_mem(0, |
| 933 | sizeof(vxlan6_tunnel_key_t), |
| 934 | sizeof(uword)); |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 935 | vxm->vtep6 = hash_create_mem(0, |
| 936 | sizeof(ip6_address_t), |
Eyal Bari | fff67c8 | 2016-12-21 12:45:47 +0200 | [diff] [blame] | 937 | sizeof(uword)); |
Eyal Bari | 0ded851 | 2017-01-19 17:01:09 +0200 | [diff] [blame] | 938 | vxm->mcast_shared = hash_create_mem(0, |
| 939 | sizeof(ip46_address_t), |
| 940 | sizeof(mcast_shared_t)); |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 941 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 942 | udp_register_dst_port (vm, UDP_DST_PORT_vxlan, |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 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); |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 946 | |
| 947 | fib_node_register_type(FIB_NODE_TYPE_VXLAN_TUNNEL, &vxlan_vft); |
| 948 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 949 | return 0; |
| 950 | } |
| 951 | |
| 952 | VLIB_INIT_FUNCTION(vxlan_init); |