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> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 17 | |
Billy McFall | c5d9cda | 2016-09-14 10:39:58 -0400 | [diff] [blame] | 18 | /** |
| 19 | * @file |
| 20 | * @brief VXLAN. |
| 21 | * |
| 22 | * VXLAN provides the features needed to allow L2 bridge domains (BDs) |
| 23 | * to span multiple servers. This is done by building an L2 overlay on |
| 24 | * top of an L3 network underlay using VXLAN tunnels. |
| 25 | * |
| 26 | * This makes it possible for servers to be co-located in the same data |
| 27 | * center or be separated geographically as long as they are reachable |
| 28 | * through the underlay L3 network. |
| 29 | * |
| 30 | * You can refer to this kind of L2 overlay bridge domain as a VXLAN |
| 31 | * (Virtual eXtensible VLAN) segment. |
| 32 | */ |
| 33 | |
| 34 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 35 | vxlan_main_t vxlan_main; |
| 36 | |
| 37 | static u8 * format_decap_next (u8 * s, va_list * args) |
| 38 | { |
| 39 | u32 next_index = va_arg (*args, u32); |
| 40 | |
| 41 | switch (next_index) |
| 42 | { |
| 43 | case VXLAN_INPUT_NEXT_DROP: |
| 44 | return format (s, "drop"); |
| 45 | case VXLAN_INPUT_NEXT_L2_INPUT: |
| 46 | return format (s, "l2"); |
| 47 | case VXLAN_INPUT_NEXT_IP4_INPUT: |
| 48 | return format (s, "ip4"); |
| 49 | case VXLAN_INPUT_NEXT_IP6_INPUT: |
| 50 | return format (s, "ip6"); |
| 51 | default: |
| 52 | return format (s, "unknown %d", next_index); |
| 53 | } |
| 54 | return s; |
| 55 | } |
| 56 | |
| 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 | |
| 62 | s = format (s, |
| 63 | "[%d] %U (src) %U (dst) vni %d encap_fib_index %d", |
| 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, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | t->vni, |
| 68 | t->encap_fib_index); |
| 69 | s = format (s, " decap_next %U\n", format_decap_next, t->decap_next_index); |
| 70 | return s; |
| 71 | } |
| 72 | |
| 73 | static u8 * format_vxlan_name (u8 * s, va_list * args) |
| 74 | { |
| 75 | u32 dev_instance = va_arg (*args, u32); |
| 76 | return format (s, "vxlan_tunnel%d", dev_instance); |
| 77 | } |
| 78 | |
| 79 | static uword dummy_interface_tx (vlib_main_t * vm, |
| 80 | vlib_node_runtime_t * node, |
| 81 | vlib_frame_t * frame) |
| 82 | { |
| 83 | clib_warning ("you shouldn't be here, leaking buffers..."); |
| 84 | return frame->n_vectors; |
| 85 | } |
| 86 | |
Pavel Kotucek | 988a7c4 | 2016-02-29 15:03:08 +0100 | [diff] [blame] | 87 | static clib_error_t * |
| 88 | vxlan_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) |
| 89 | { |
| 90 | if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) |
| 91 | vnet_hw_interface_set_flags (vnm, hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 92 | else |
| 93 | vnet_hw_interface_set_flags (vnm, hw_if_index, 0); |
| 94 | |
| 95 | return /* no error */ 0; |
| 96 | } |
| 97 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 98 | VNET_DEVICE_CLASS (vxlan_device_class,static) = { |
| 99 | .name = "VXLAN", |
| 100 | .format_device_name = format_vxlan_name, |
| 101 | .format_tx_trace = format_vxlan_encap_trace, |
| 102 | .tx_function = dummy_interface_tx, |
Pavel Kotucek | 988a7c4 | 2016-02-29 15:03:08 +0100 | [diff] [blame] | 103 | .admin_up_down_function = vxlan_interface_admin_up_down, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | static uword dummy_set_rewrite (vnet_main_t * vnm, |
| 107 | u32 sw_if_index, |
| 108 | u32 l3_type, |
| 109 | void * dst_address, |
| 110 | void * rewrite, |
| 111 | uword max_rewrite_bytes) |
| 112 | { |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static u8 * format_vxlan_header_with_length (u8 * s, va_list * args) |
| 117 | { |
| 118 | u32 dev_instance = va_arg (*args, u32); |
| 119 | s = format (s, "unimplemented dev %u", dev_instance); |
| 120 | return s; |
| 121 | } |
| 122 | |
| 123 | VNET_HW_INTERFACE_CLASS (vxlan_hw_class) = { |
| 124 | .name = "VXLAN", |
| 125 | .format_header = format_vxlan_header_with_length, |
| 126 | .set_rewrite = dummy_set_rewrite, |
| 127 | }; |
| 128 | |
| 129 | #define foreach_copy_field \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | _(vni) \ |
| 131 | _(encap_fib_index) \ |
| 132 | _(decap_next_index) |
| 133 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 134 | #define foreach_copy_ipv4 { \ |
| 135 | _(src.ip4.as_u32) \ |
| 136 | _(dst.ip4.as_u32) \ |
| 137 | } |
| 138 | |
| 139 | #define foreach_copy_ipv6 { \ |
| 140 | _(src.ip6.as_u64[0]) \ |
| 141 | _(src.ip6.as_u64[1]) \ |
| 142 | _(dst.ip6.as_u64[0]) \ |
| 143 | _(dst.ip6.as_u64[1]) \ |
| 144 | } |
| 145 | |
| 146 | static int vxlan4_rewrite (vxlan_tunnel_t * t) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 147 | { |
| 148 | u8 *rw = 0; |
| 149 | ip4_header_t * ip0; |
| 150 | ip4_vxlan_header_t * h0; |
| 151 | int len = sizeof (*h0); |
| 152 | |
| 153 | vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES); |
| 154 | |
| 155 | h0 = (ip4_vxlan_header_t *) rw; |
| 156 | |
| 157 | /* Fixed portion of the (outer) ip4 header */ |
| 158 | ip0 = &h0->ip4; |
| 159 | ip0->ip_version_and_header_length = 0x45; |
| 160 | ip0->ttl = 254; |
| 161 | ip0->protocol = IP_PROTOCOL_UDP; |
| 162 | |
| 163 | /* we fix up the ip4 header length and checksum after-the-fact */ |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 164 | ip0->src_address.as_u32 = t->src.ip4.as_u32; |
| 165 | ip0->dst_address.as_u32 = t->dst.ip4.as_u32; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | ip0->checksum = ip4_header_checksum (ip0); |
| 167 | |
| 168 | /* UDP header, randomize src port on something, maybe? */ |
| 169 | h0->udp.src_port = clib_host_to_net_u16 (4789); |
| 170 | h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan); |
| 171 | |
| 172 | /* VXLAN header */ |
| 173 | vnet_set_vni_and_flags(&h0->vxlan, t->vni); |
| 174 | |
| 175 | t->rewrite = rw; |
| 176 | return (0); |
| 177 | } |
| 178 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 179 | static int vxlan6_rewrite (vxlan_tunnel_t * t) |
| 180 | { |
| 181 | u8 *rw = 0; |
| 182 | ip6_header_t * ip0; |
| 183 | ip6_vxlan_header_t * h0; |
| 184 | int len = sizeof (*h0); |
| 185 | |
| 186 | vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES); |
| 187 | |
| 188 | h0 = (ip6_vxlan_header_t *) rw; |
| 189 | |
| 190 | /* Fixed portion of the (outer) ip6 header */ |
| 191 | ip0 = &h0->ip6; |
| 192 | ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32(6 << 28); |
| 193 | ip0->hop_limit = 255; |
| 194 | ip0->protocol = IP_PROTOCOL_UDP; |
| 195 | |
| 196 | ip0->src_address.as_u64[0] = t->src.ip6.as_u64[0]; |
| 197 | ip0->src_address.as_u64[1] = t->src.ip6.as_u64[1]; |
| 198 | ip0->dst_address.as_u64[0] = t->dst.ip6.as_u64[0]; |
| 199 | ip0->dst_address.as_u64[1] = t->dst.ip6.as_u64[1]; |
| 200 | |
| 201 | /* UDP header, randomize src port on something, maybe? */ |
| 202 | h0->udp.src_port = clib_host_to_net_u16 (4789); |
| 203 | h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_vxlan); |
| 204 | |
| 205 | /* VXLAN header */ |
| 206 | vnet_set_vni_and_flags(&h0->vxlan, t->vni); |
| 207 | |
| 208 | t->rewrite = rw; |
| 209 | return (0); |
| 210 | } |
| 211 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | int vnet_vxlan_add_del_tunnel |
| 213 | (vnet_vxlan_add_del_tunnel_args_t *a, u32 * sw_if_indexp) |
| 214 | { |
| 215 | vxlan_main_t * vxm = &vxlan_main; |
| 216 | vxlan_tunnel_t *t = 0; |
| 217 | vnet_main_t * vnm = vxm->vnet_main; |
John Lo | 2d34374 | 2016-01-19 17:27:17 -0500 | [diff] [blame] | 218 | ip4_main_t * im4 = &ip4_main; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 219 | ip6_main_t * im6 = &ip6_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 220 | vnet_hw_interface_t * hi; |
| 221 | uword * p; |
| 222 | u32 hw_if_index = ~0; |
| 223 | u32 sw_if_index = ~0; |
| 224 | int rv; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 225 | vxlan4_tunnel_key_t key4; |
| 226 | vxlan6_tunnel_key_t key6; |
John Lo | 3ef822e | 2016-06-07 09:14:07 -0400 | [diff] [blame] | 227 | l2output_main_t * l2om = &l2output_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 229 | if (!a->is_ip6) { |
| 230 | key4.src = a->dst.ip4.as_u32; /* decap src in key is encap dst in config */ |
| 231 | key4.vni = clib_host_to_net_u32 (a->vni << 8); |
| 232 | |
| 233 | p = hash_get (vxm->vxlan4_tunnel_by_key, key4.as_u64); |
| 234 | } else { |
| 235 | key6.src.as_u64[0] = a->dst.ip6.as_u64[0]; |
| 236 | key6.src.as_u64[1] = a->dst.ip6.as_u64[1]; |
| 237 | key6.vni = clib_host_to_net_u32 (a->vni << 8); |
| 238 | |
Chris Luke | c794915 | 2016-05-31 10:42:14 -0400 | [diff] [blame] | 239 | p = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6); |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 240 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 241 | |
| 242 | if (a->is_add) |
| 243 | { |
| 244 | /* adding a tunnel: tunnel must not already exist */ |
| 245 | if (p) |
| 246 | return VNET_API_ERROR_TUNNEL_EXIST; |
| 247 | |
| 248 | if (a->decap_next_index == ~0) |
| 249 | a->decap_next_index = VXLAN_INPUT_NEXT_L2_INPUT; |
| 250 | |
| 251 | if (a->decap_next_index >= VXLAN_INPUT_N_NEXT) |
| 252 | return VNET_API_ERROR_INVALID_DECAP_NEXT; |
| 253 | |
| 254 | pool_get_aligned (vxm->tunnels, t, CLIB_CACHE_LINE_BYTES); |
| 255 | memset (t, 0, sizeof (*t)); |
| 256 | |
| 257 | /* copy from arg structure */ |
| 258 | #define _(x) t->x = a->x; |
| 259 | foreach_copy_field; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 260 | if (!a->is_ip6) foreach_copy_ipv4 |
| 261 | else foreach_copy_ipv6 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 262 | #undef _ |
| 263 | |
Chris Luke | c794915 | 2016-05-31 10:42:14 -0400 | [diff] [blame] | 264 | /* copy the key */ |
| 265 | if (a->is_ip6) |
| 266 | { |
| 267 | t->key6 = clib_mem_alloc (sizeof(vxlan6_tunnel_key_t)); |
| 268 | clib_memcpy (t->key6, &key6, sizeof(key6)); |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | t->key4 = 0; /* not yet used */ |
| 273 | } |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 274 | |
| 275 | if (!a->is_ip6) t->flags |= VXLAN_TUNNEL_IS_IPV4; |
| 276 | |
| 277 | if (!a->is_ip6) { |
| 278 | rv = vxlan4_rewrite (t); |
| 279 | } else { |
| 280 | rv = vxlan6_rewrite (t); |
| 281 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 282 | |
| 283 | if (rv) |
| 284 | { |
| 285 | pool_put (vxm->tunnels, t); |
| 286 | return rv; |
| 287 | } |
| 288 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 289 | if (!a->is_ip6) |
| 290 | hash_set (vxm->vxlan4_tunnel_by_key, key4.as_u64, t - vxm->tunnels); |
| 291 | else |
Chris Luke | c794915 | 2016-05-31 10:42:14 -0400 | [diff] [blame] | 292 | hash_set_mem (vxm->vxlan6_tunnel_by_key, t->key6, t - vxm->tunnels); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 293 | |
| 294 | if (vec_len (vxm->free_vxlan_tunnel_hw_if_indices) > 0) |
| 295 | { |
| 296 | vnet_interface_main_t * im = &vnm->interface_main; |
| 297 | hw_if_index = vxm->free_vxlan_tunnel_hw_if_indices |
| 298 | [vec_len (vxm->free_vxlan_tunnel_hw_if_indices)-1]; |
| 299 | _vec_len (vxm->free_vxlan_tunnel_hw_if_indices) -= 1; |
| 300 | |
| 301 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 302 | hi->dev_instance = t - vxm->tunnels; |
| 303 | hi->hw_instance = hi->dev_instance; |
| 304 | |
| 305 | /* clear old stats of freed tunnel before reuse */ |
| 306 | sw_if_index = hi->sw_if_index; |
| 307 | vnet_interface_counter_lock(im); |
| 308 | vlib_zero_combined_counter |
| 309 | (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index); |
| 310 | vlib_zero_combined_counter |
| 311 | (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index); |
| 312 | vlib_zero_simple_counter |
| 313 | (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index); |
| 314 | vnet_interface_counter_unlock(im); |
| 315 | } |
| 316 | else |
| 317 | { |
| 318 | hw_if_index = vnet_register_interface |
| 319 | (vnm, vxlan_device_class.index, t - vxm->tunnels, |
| 320 | vxlan_hw_class.index, t - vxm->tunnels); |
| 321 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 322 | hi->output_node_index = vxlan_encap_node.index; |
| 323 | } |
| 324 | |
| 325 | t->hw_if_index = hw_if_index; |
| 326 | t->sw_if_index = sw_if_index = hi->sw_if_index; |
| 327 | |
Dave Wallace | 60231f3 | 2015-12-17 21:04:30 -0500 | [diff] [blame] | 328 | vec_validate_init_empty (vxm->tunnel_index_by_sw_if_index, sw_if_index, ~0); |
| 329 | vxm->tunnel_index_by_sw_if_index[sw_if_index] = t - vxm->tunnels; |
| 330 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 331 | if (a->decap_next_index == VXLAN_INPUT_NEXT_L2_INPUT) |
| 332 | { |
| 333 | l2input_main_t * l2im = &l2input_main; |
| 334 | /* setup l2 input config with l2 feature and bd 0 to drop packet */ |
| 335 | vec_validate (l2im->configs, sw_if_index); |
| 336 | l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP; |
| 337 | l2im->configs[sw_if_index].bd_index = 0; |
| 338 | } |
John Lo | 3ef822e | 2016-06-07 09:14:07 -0400 | [diff] [blame] | 339 | |
| 340 | /* |
| 341 | * Directs the l2 output path to work out the interface |
| 342 | * output next-arc itself. Needed when recycling a tunnel. |
| 343 | */ |
| 344 | vec_validate_init_empty(l2om->next_nodes.output_node_index_vec, |
| 345 | sw_if_index, ~0); |
| 346 | l2om->next_nodes.output_node_index_vec[t->sw_if_index] |
| 347 | = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 348 | vnet_sw_interface_set_flags (vnm, sw_if_index, |
| 349 | VNET_SW_INTERFACE_FLAG_ADMIN_UP); |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 350 | if (!a->is_ip6) { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame^] | 351 | vec_validate (im4->fib_index_by_sw_if_index, sw_if_index); |
| 352 | im4->fib_index_by_sw_if_index[sw_if_index] = t->encap_fib_index; |
| 353 | ip4_sw_interface_enable_disable(sw_if_index, 1); |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 354 | } else { |
| 355 | vec_validate (im6->fib_index_by_sw_if_index, sw_if_index); |
| 356 | im6->fib_index_by_sw_if_index[sw_if_index] = t->encap_fib_index; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame^] | 357 | ip6_sw_interface_enable_disable(sw_if_index, 1); |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 358 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 359 | } |
| 360 | else |
| 361 | { |
| 362 | /* deleting a tunnel: tunnel must exist */ |
| 363 | if (!p) |
| 364 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 365 | |
| 366 | t = pool_elt_at_index (vxm->tunnels, p[0]); |
| 367 | |
| 368 | vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */); |
| 369 | /* make sure tunnel is removed from l2 bd or xconnect */ |
| 370 | set_int_l2_mode(vxm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0, 0); |
| 371 | vec_add1 (vxm->free_vxlan_tunnel_hw_if_indices, t->hw_if_index); |
| 372 | |
Dave Wallace | 60231f3 | 2015-12-17 21:04:30 -0500 | [diff] [blame] | 373 | vxm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0; |
| 374 | |
John Lo | 3ef822e | 2016-06-07 09:14:07 -0400 | [diff] [blame] | 375 | /* Directs the l2 path to turf packets sent to this sw_if_index */ |
| 376 | l2om->next_nodes.output_node_index_vec[t->sw_if_index] |
| 377 | = L2OUTPUT_NEXT_DEL_TUNNEL; |
| 378 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 379 | if (!a->is_ip6) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame^] | 380 | { |
| 381 | hash_unset (vxm->vxlan4_tunnel_by_key, key4.as_u64); |
| 382 | ip4_sw_interface_enable_disable(sw_if_index, 1); |
| 383 | } |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 384 | else |
John Lo | cb9f3d7 | 2016-08-30 00:10:09 -0400 | [diff] [blame] | 385 | { |
| 386 | hash_unset_mem (vxm->vxlan6_tunnel_by_key, t->key6); |
| 387 | clib_mem_free (t->key6); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame^] | 388 | ip6_sw_interface_enable_disable(sw_if_index, 1); |
John Lo | cb9f3d7 | 2016-08-30 00:10:09 -0400 | [diff] [blame] | 389 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 390 | vec_free (t->rewrite); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 391 | pool_put (vxm->tunnels, t); |
| 392 | } |
| 393 | |
| 394 | if (sw_if_indexp) |
| 395 | *sw_if_indexp = sw_if_index; |
| 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 400 | static u32 fib4_index_from_fib_id (u32 fib_id) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 401 | { |
| 402 | ip4_main_t * im = &ip4_main; |
| 403 | uword * p; |
| 404 | |
| 405 | p = hash_get (im->fib_index_by_table_id, fib_id); |
| 406 | if (!p) |
| 407 | return ~0; |
| 408 | |
| 409 | return p[0]; |
| 410 | } |
| 411 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 412 | static u32 fib6_index_from_fib_id (u32 fib_id) |
| 413 | { |
| 414 | ip6_main_t * im = &ip6_main; |
| 415 | uword * p; |
| 416 | |
| 417 | p = hash_get (im->fib_index_by_table_id, fib_id); |
| 418 | if (!p) |
| 419 | return ~0; |
| 420 | |
| 421 | return p[0]; |
| 422 | } |
| 423 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 424 | static uword unformat_decap_next (unformat_input_t * input, va_list * args) |
| 425 | { |
| 426 | u32 * result = va_arg (*args, u32 *); |
| 427 | u32 tmp; |
| 428 | |
| 429 | if (unformat (input, "l2")) |
| 430 | *result = VXLAN_INPUT_NEXT_L2_INPUT; |
| 431 | else if (unformat (input, "drop")) |
| 432 | *result = VXLAN_INPUT_NEXT_DROP; |
| 433 | else if (unformat (input, "ip4")) |
| 434 | *result = VXLAN_INPUT_NEXT_IP4_INPUT; |
| 435 | else if (unformat (input, "ip6")) |
| 436 | *result = VXLAN_INPUT_NEXT_IP6_INPUT; |
| 437 | else if (unformat (input, "%d", &tmp)) |
| 438 | *result = tmp; |
| 439 | else |
| 440 | return 0; |
| 441 | return 1; |
| 442 | } |
| 443 | |
| 444 | static clib_error_t * |
| 445 | vxlan_add_del_tunnel_command_fn (vlib_main_t * vm, |
| 446 | unformat_input_t * input, |
| 447 | vlib_cli_command_t * cmd) |
| 448 | { |
| 449 | unformat_input_t _line_input, * line_input = &_line_input; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 450 | ip46_address_t src, dst; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 451 | u8 is_add = 1; |
| 452 | u8 src_set = 0; |
| 453 | u8 dst_set = 0; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 454 | u8 ipv4_set = 0; |
| 455 | u8 ipv6_set = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 456 | u32 encap_fib_index = 0; |
| 457 | u32 decap_next_index = ~0; |
| 458 | u32 vni = 0; |
| 459 | u32 tmp; |
| 460 | int rv; |
| 461 | vnet_vxlan_add_del_tunnel_args_t _a, * a = &_a; |
Pierre Pfister | 78ea9c2 | 2016-05-23 12:51:54 +0100 | [diff] [blame] | 462 | u32 sw_if_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 463 | |
| 464 | /* Get a line of input. */ |
| 465 | if (! unformat_user (input, unformat_line_input, line_input)) |
| 466 | return 0; |
| 467 | |
| 468 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { |
| 469 | if (unformat (line_input, "del")) |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 470 | { |
| 471 | is_add = 0; |
| 472 | } |
| 473 | else if (unformat (line_input, "src %U", |
| 474 | unformat_ip4_address, &src.ip4)) |
| 475 | { |
| 476 | src_set = 1; |
| 477 | ipv4_set = 1; |
| 478 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 479 | else if (unformat (line_input, "dst %U", |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 480 | unformat_ip4_address, &dst.ip4)) |
| 481 | { |
| 482 | dst_set = 1; |
| 483 | ipv4_set = 1; |
| 484 | } |
| 485 | else if (unformat (line_input, "src %U", |
| 486 | unformat_ip6_address, &src.ip6)) |
| 487 | { |
| 488 | src_set = 1; |
| 489 | ipv6_set = 1; |
| 490 | } |
| 491 | else if (unformat (line_input, "dst %U", |
| 492 | unformat_ip6_address, &dst.ip6)) |
| 493 | { |
| 494 | dst_set = 1; |
| 495 | ipv6_set = 1; |
| 496 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 497 | else if (unformat (line_input, "encap-vrf-id %d", &tmp)) |
| 498 | { |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 499 | if (ipv6_set) |
| 500 | encap_fib_index = fib6_index_from_fib_id (tmp); |
| 501 | else |
| 502 | encap_fib_index = fib4_index_from_fib_id (tmp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 503 | if (encap_fib_index == ~0) |
| 504 | return clib_error_return (0, "nonexistent encap-vrf-id %d", tmp); |
| 505 | } |
| 506 | else if (unformat (line_input, "decap-next %U", unformat_decap_next, |
| 507 | &decap_next_index)) |
| 508 | ; |
| 509 | else if (unformat (line_input, "vni %d", &vni)) |
| 510 | { |
| 511 | if (vni >> 24) |
| 512 | return clib_error_return (0, "vni %d out of range", vni); |
| 513 | } |
| 514 | else |
| 515 | return clib_error_return (0, "parse error: '%U'", |
| 516 | format_unformat_error, line_input); |
| 517 | } |
| 518 | |
| 519 | unformat_free (line_input); |
| 520 | |
| 521 | if (src_set == 0) |
| 522 | return clib_error_return (0, "tunnel src address not specified"); |
| 523 | |
| 524 | if (dst_set == 0) |
| 525 | return clib_error_return (0, "tunnel dst address not specified"); |
| 526 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 527 | if (ipv4_set && ipv6_set) |
| 528 | return clib_error_return (0, "both IPv4 and IPv6 addresses specified"); |
| 529 | |
| 530 | if ((ipv4_set && memcmp(&src.ip4, &dst.ip4, sizeof(src.ip4)) == 0) || |
| 531 | (ipv6_set && memcmp(&src.ip6, &dst.ip6, sizeof(src.ip6)) == 0)) |
| 532 | return clib_error_return (0, "src and dst addresses are identical"); |
| 533 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 534 | if (vni == 0) |
| 535 | return clib_error_return (0, "vni not specified"); |
| 536 | |
| 537 | memset (a, 0, sizeof (*a)); |
| 538 | |
| 539 | a->is_add = is_add; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 540 | a->is_ip6 = ipv6_set; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 541 | |
| 542 | #define _(x) a->x = x; |
| 543 | foreach_copy_field; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 544 | if (ipv4_set) foreach_copy_ipv4 |
| 545 | else foreach_copy_ipv6 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 546 | #undef _ |
| 547 | |
Pierre Pfister | 78ea9c2 | 2016-05-23 12:51:54 +0100 | [diff] [blame] | 548 | rv = vnet_vxlan_add_del_tunnel (a, &sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 549 | |
| 550 | switch(rv) |
| 551 | { |
| 552 | case 0: |
Pierre Pfister | 78ea9c2 | 2016-05-23 12:51:54 +0100 | [diff] [blame] | 553 | if (is_add) |
| 554 | vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 555 | break; |
| 556 | case VNET_API_ERROR_INVALID_DECAP_NEXT: |
| 557 | return clib_error_return (0, "invalid decap-next..."); |
| 558 | |
| 559 | case VNET_API_ERROR_TUNNEL_EXIST: |
| 560 | return clib_error_return (0, "tunnel already exists..."); |
| 561 | |
| 562 | case VNET_API_ERROR_NO_SUCH_ENTRY: |
| 563 | return clib_error_return (0, "tunnel does not exist..."); |
| 564 | |
| 565 | default: |
| 566 | return clib_error_return |
| 567 | (0, "vnet_vxlan_add_del_tunnel returned %d", rv); |
| 568 | } |
| 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
Billy McFall | c5d9cda | 2016-09-14 10:39:58 -0400 | [diff] [blame] | 573 | /*? |
| 574 | * Add or delete a VXLAN Tunnel. |
| 575 | * |
| 576 | * VXLAN provides the features needed to allow L2 bridge domains (BDs) |
| 577 | * to span multiple servers. This is done by building an L2 overlay on |
| 578 | * top of an L3 network underlay using VXLAN tunnels. |
| 579 | * |
| 580 | * This makes it possible for servers to be co-located in the same data |
| 581 | * center or be separated geographically as long as they are reachable |
| 582 | * through the underlay L3 network. |
| 583 | * |
| 584 | * You can refer to this kind of L2 overlay bridge domain as a VXLAN |
| 585 | * (Virtual eXtensible VLAN) segment. |
| 586 | * |
| 587 | * @cliexpar |
| 588 | * Example of how to create a VXLAN Tunnel: |
| 589 | * @cliexcmd{create vxlan tunnel src 10.0.3.1 dst 10.0.3.3 vni 13 encap-vrf-id 7 decap-next l2} |
| 590 | * Example of how to delete a VXLAN Tunnel: |
| 591 | * @cliexcmd{create vxlan tunnel src 10.0.3.1 dst 10.0.3.3 vni 13 del} |
| 592 | ?*/ |
| 593 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 594 | VLIB_CLI_COMMAND (create_vxlan_tunnel_command, static) = { |
| 595 | .path = "create vxlan tunnel", |
| 596 | .short_help = |
| 597 | "create vxlan tunnel src <local-vtep-addr> dst <remote-vtep-addr> vni <nn>" |
Billy McFall | c5d9cda | 2016-09-14 10:39:58 -0400 | [diff] [blame] | 598 | " [encap-vrf-id <nn>] [decap-next [l2|ip4|ip6]] [del]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 599 | .function = vxlan_add_del_tunnel_command_fn, |
| 600 | }; |
Billy McFall | c5d9cda | 2016-09-14 10:39:58 -0400 | [diff] [blame] | 601 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 602 | |
| 603 | static clib_error_t * |
| 604 | show_vxlan_tunnel_command_fn (vlib_main_t * vm, |
| 605 | unformat_input_t * input, |
| 606 | vlib_cli_command_t * cmd) |
| 607 | { |
| 608 | vxlan_main_t * vxm = &vxlan_main; |
| 609 | vxlan_tunnel_t * t; |
| 610 | |
| 611 | if (pool_elts (vxm->tunnels) == 0) |
| 612 | vlib_cli_output (vm, "No vxlan tunnels configured..."); |
| 613 | |
| 614 | pool_foreach (t, vxm->tunnels, |
| 615 | ({ |
| 616 | vlib_cli_output (vm, "%U", format_vxlan_tunnel, t); |
| 617 | })); |
| 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | |
Billy McFall | c5d9cda | 2016-09-14 10:39:58 -0400 | [diff] [blame] | 622 | /*? |
| 623 | * Display all the VXLAN Tunnel entries. |
| 624 | * |
| 625 | * @cliexpar |
| 626 | * Example of how to display the VXLAN Tunnel entries: |
| 627 | * @cliexstart{show vxlan tunnel} |
| 628 | * [0] 10.0.3.1 (src) 10.0.3.3 (dst) vni 13 encap_fib_index 1 decap_next l2 |
| 629 | * @cliexend |
| 630 | ?*/ |
| 631 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 632 | VLIB_CLI_COMMAND (show_vxlan_tunnel_command, static) = { |
| 633 | .path = "show vxlan tunnel", |
Billy McFall | c5d9cda | 2016-09-14 10:39:58 -0400 | [diff] [blame] | 634 | .short_help = "show vxlan tunnel", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 635 | .function = show_vxlan_tunnel_command_fn, |
| 636 | }; |
Billy McFall | c5d9cda | 2016-09-14 10:39:58 -0400 | [diff] [blame] | 637 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 638 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 639 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 640 | clib_error_t *vxlan_init (vlib_main_t *vm) |
| 641 | { |
| 642 | vxlan_main_t * vxm = &vxlan_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 643 | |
| 644 | vxm->vnet_main = vnet_get_main(); |
| 645 | vxm->vlib_main = vm; |
| 646 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 647 | /* initialize the ip6 hash */ |
| 648 | vxm->vxlan6_tunnel_by_key = hash_create_mem(0, |
| 649 | sizeof(vxlan6_tunnel_key_t), |
| 650 | sizeof(uword)); |
| 651 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 652 | udp_register_dst_port (vm, UDP_DST_PORT_vxlan, |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 653 | vxlan4_input_node.index, /* is_ip4 */ 1); |
| 654 | udp_register_dst_port (vm, UDP_DST_PORT_vxlan6, |
| 655 | vxlan6_input_node.index, /* is_ip4 */ 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | VLIB_INIT_FUNCTION(vxlan_init); |