Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -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 | */ |
Keith Burns (alagalah) | d46cca1 | 2016-08-25 11:21:39 -0700 | [diff] [blame] | 15 | /** |
| 16 | * @file |
| 17 | * @brief Common utility functions for IPv4 and IPv6 VXLAN GPE tunnels |
| 18 | * |
| 19 | */ |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 20 | #include <vnet/vxlan-gpe/vxlan_gpe.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 21 | #include <vnet/fib/fib.h> |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 22 | #include <vnet/ip/format.h> |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 23 | #include <vnet/fib/fib_entry.h> |
| 24 | #include <vnet/fib/fib_table.h> |
| 25 | #include <vnet/mfib/mfib_table.h> |
| 26 | #include <vnet/adj/adj_mcast.h> |
| 27 | #include <vnet/interface.h> |
| 28 | #include <vlib/vlib.h> |
| 29 | |
| 30 | /** |
| 31 | * @file |
| 32 | * @brief VXLAN-GPE. |
| 33 | * |
| 34 | * VXLAN-GPE provides the features needed to allow L2 bridge domains (BDs) |
| 35 | * to span multiple servers. This is done by building an L2 overlay on |
| 36 | * top of an L3 network underlay using VXLAN-GPE tunnels. |
| 37 | * |
| 38 | * This makes it possible for servers to be co-located in the same data |
| 39 | * center or be separated geographically as long as they are reachable |
| 40 | * through the underlay L3 network. |
| 41 | * |
| 42 | * You can refer to this kind of L2 overlay bridge domain as a VXLAN-GPE segment. |
| 43 | */ |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 44 | |
| 45 | vxlan_gpe_main_t vxlan_gpe_main; |
| 46 | |
Keith Burns (alagalah) | d46cca1 | 2016-08-25 11:21:39 -0700 | [diff] [blame] | 47 | /** |
| 48 | * @brief Tracing function for VXLAN GPE tunnel packets |
| 49 | * |
| 50 | * @param *s formatting string |
| 51 | * @param *args |
| 52 | * |
| 53 | * @return *s formatted string |
| 54 | * |
| 55 | */ |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 56 | u8 * format_vxlan_gpe_tunnel (u8 * s, va_list * args) |
| 57 | { |
| 58 | vxlan_gpe_tunnel_t * t = va_arg (*args, vxlan_gpe_tunnel_t *); |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 59 | vxlan_gpe_main_t * ngm = &vxlan_gpe_main; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 60 | |
| 61 | s = format (s, "[%d] local: %U remote: %U ", |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 62 | t - ngm->tunnels, |
Hongjun Ni | c8af24f | 2016-08-05 00:58:20 +0800 | [diff] [blame] | 63 | format_ip46_address, &t->local, IP46_TYPE_ANY, |
| 64 | format_ip46_address, &t->remote, IP46_TYPE_ANY); |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 65 | |
| 66 | s = format (s, " vxlan VNI %d ", t->vni); |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 67 | |
| 68 | switch (t->protocol) |
| 69 | { |
| 70 | case VXLAN_GPE_PROTOCOL_IP4: |
| 71 | s = format (s, "next-protocol ip4"); |
Hongjun Ni | 4a71314 | 2016-05-24 02:54:10 +0800 | [diff] [blame] | 72 | break; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 73 | case VXLAN_GPE_PROTOCOL_IP6: |
| 74 | s = format (s, "next-protocol ip6"); |
Hongjun Ni | 4a71314 | 2016-05-24 02:54:10 +0800 | [diff] [blame] | 75 | break; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 76 | case VXLAN_GPE_PROTOCOL_ETHERNET: |
| 77 | s = format (s, "next-protocol ethernet"); |
Hongjun Ni | 4a71314 | 2016-05-24 02:54:10 +0800 | [diff] [blame] | 78 | break; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 79 | case VXLAN_GPE_PROTOCOL_NSH: |
| 80 | s = format (s, "next-protocol nsh"); |
Hongjun Ni | 4a71314 | 2016-05-24 02:54:10 +0800 | [diff] [blame] | 81 | break; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 82 | default: |
| 83 | s = format (s, "next-protocol unknown %d", t->protocol); |
| 84 | } |
| 85 | |
Hongjun Ni | 04ffd0ad | 2017-06-23 00:18:40 +0800 | [diff] [blame] | 86 | if (ip46_address_is_multicast (&t->remote)) |
| 87 | s = format (s, "mcast_sw_if_index %d ", t->mcast_sw_if_index); |
| 88 | |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 89 | s = format (s, " fibs: (encap %d, decap %d)", |
| 90 | t->encap_fib_index, |
| 91 | t->decap_fib_index); |
| 92 | |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 93 | return s; |
| 94 | } |
| 95 | |
Keith Burns (alagalah) | d46cca1 | 2016-08-25 11:21:39 -0700 | [diff] [blame] | 96 | /** |
| 97 | * @brief Naming for VXLAN GPE tunnel |
| 98 | * |
| 99 | * @param *s formatting string |
| 100 | * @param *args |
| 101 | * |
| 102 | * @return *s formatted string |
| 103 | * |
| 104 | */ |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 105 | static u8 * format_vxlan_gpe_name (u8 * s, va_list * args) |
| 106 | { |
Hongjun Ni | 0e06e2b | 2016-05-30 19:45:51 +0800 | [diff] [blame] | 107 | u32 dev_instance = va_arg (*args, u32); |
| 108 | return format (s, "vxlan_gpe_tunnel%d", dev_instance); |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 111 | static uword dummy_interface_tx (vlib_main_t * vm, |
| 112 | vlib_node_runtime_t * node, |
| 113 | vlib_frame_t * frame) |
| 114 | { |
| 115 | clib_warning ("you shouldn't be here, leaking buffers..."); |
| 116 | return frame->n_vectors; |
| 117 | } |
Keith Burns (alagalah) | d46cca1 | 2016-08-25 11:21:39 -0700 | [diff] [blame] | 118 | |
| 119 | /** |
| 120 | * @brief CLI function for VXLAN GPE admin up/down |
| 121 | * |
| 122 | * @param *vnm |
| 123 | * @param hw_if_index |
| 124 | * @param flag |
| 125 | * |
| 126 | * @return *rc |
| 127 | * |
| 128 | */ |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 129 | static clib_error_t * |
| 130 | vxlan_gpe_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) |
| 131 | { |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 132 | u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? |
| 133 | VNET_HW_INTERFACE_FLAG_LINK_UP : 0; |
| 134 | vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags); |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
Hongjun Ni | 0e06e2b | 2016-05-30 19:45:51 +0800 | [diff] [blame] | 139 | VNET_DEVICE_CLASS (vxlan_gpe_device_class,static) = { |
| 140 | .name = "VXLAN_GPE", |
| 141 | .format_device_name = format_vxlan_gpe_name, |
| 142 | .format_tx_trace = format_vxlan_gpe_encap_trace, |
| 143 | .tx_function = dummy_interface_tx, |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 144 | .admin_up_down_function = vxlan_gpe_interface_admin_up_down, |
Hongjun Ni | 0e06e2b | 2016-05-30 19:45:51 +0800 | [diff] [blame] | 145 | }; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 146 | |
Vengada Govindan | 6d403a0 | 2016-10-12 05:54:09 -0700 | [diff] [blame] | 147 | |
Keith Burns (alagalah) | d46cca1 | 2016-08-25 11:21:39 -0700 | [diff] [blame] | 148 | /** |
| 149 | * @brief Formatting function for tracing VXLAN GPE with length |
| 150 | * |
| 151 | * @param *s |
| 152 | * @param *args |
| 153 | * |
| 154 | * @return *s |
| 155 | * |
| 156 | */ |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 157 | static u8 * format_vxlan_gpe_header_with_length (u8 * s, va_list * args) |
| 158 | { |
| 159 | u32 dev_instance = va_arg (*args, u32); |
| 160 | s = format (s, "unimplemented dev %u", dev_instance); |
| 161 | return s; |
| 162 | } |
| 163 | |
| 164 | VNET_HW_INTERFACE_CLASS (vxlan_gpe_hw_class) = { |
| 165 | .name = "VXLAN_GPE", |
| 166 | .format_header = format_vxlan_gpe_header_with_length, |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 167 | .build_rewrite = default_build_rewrite, |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 170 | static void |
| 171 | vxlan_gpe_tunnel_restack_dpo(vxlan_gpe_tunnel_t * t) |
| 172 | { |
| 173 | dpo_id_t dpo = DPO_INVALID; |
| 174 | u32 encap_index = vxlan_gpe_encap_node.index; |
| 175 | fib_forward_chain_type_t forw_type = ip46_address_is_ip4(&t->remote) ? |
| 176 | FIB_FORW_CHAIN_TYPE_UNICAST_IP4 : FIB_FORW_CHAIN_TYPE_UNICAST_IP6; |
| 177 | |
| 178 | fib_entry_contribute_forwarding (t->fib_entry_index, forw_type, &dpo); |
| 179 | dpo_stack_from_node (encap_index, &t->next_dpo, &dpo); |
| 180 | dpo_reset(&dpo); |
| 181 | } |
| 182 | |
| 183 | static vxlan_gpe_tunnel_t * |
| 184 | vxlan_gpe_tunnel_from_fib_node (fib_node_t *node) |
| 185 | { |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 186 | ASSERT(FIB_NODE_TYPE_VXLAN_GPE_TUNNEL == node->fn_type); |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 187 | return ((vxlan_gpe_tunnel_t*) (((char*)node) - |
| 188 | STRUCT_OFFSET_OF(vxlan_gpe_tunnel_t, node))); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Function definition to backwalk a FIB node - |
| 193 | * Here we will restack the new dpo of VXLAN_GPE DIP to encap node. |
| 194 | */ |
| 195 | static fib_node_back_walk_rc_t |
| 196 | vxlan_gpe_tunnel_back_walk (fib_node_t *node, |
| 197 | fib_node_back_walk_ctx_t *ctx) |
| 198 | { |
| 199 | vxlan_gpe_tunnel_restack_dpo(vxlan_gpe_tunnel_from_fib_node(node)); |
| 200 | return (FIB_NODE_BACK_WALK_CONTINUE); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Function definition to get a FIB node from its index |
| 205 | */ |
| 206 | static fib_node_t* |
| 207 | vxlan_gpe_tunnel_fib_node_get (fib_node_index_t index) |
| 208 | { |
| 209 | vxlan_gpe_tunnel_t * t; |
| 210 | vxlan_gpe_main_t * ngm = &vxlan_gpe_main; |
| 211 | |
| 212 | t = pool_elt_at_index(ngm->tunnels, index); |
| 213 | |
| 214 | return (&t->node); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Function definition to inform the FIB node that its last lock has gone. |
| 219 | */ |
| 220 | static void |
| 221 | vxlan_gpe_tunnel_last_lock_gone (fib_node_t *node) |
| 222 | { |
| 223 | /* |
| 224 | * The VXLAN_GPE tunnel is a root of the graph. As such |
| 225 | * it never has children and thus is never locked. |
| 226 | */ |
| 227 | ASSERT(0); |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * Virtual function table registered by VXLAN_GPE tunnels |
| 232 | * for participation in the FIB object graph. |
| 233 | */ |
| 234 | const static fib_node_vft_t vxlan_gpe_vft = { |
| 235 | .fnv_get = vxlan_gpe_tunnel_fib_node_get, |
| 236 | .fnv_last_lock = vxlan_gpe_tunnel_last_lock_gone, |
| 237 | .fnv_back_walk = vxlan_gpe_tunnel_back_walk, |
| 238 | }; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 239 | |
| 240 | #define foreach_gpe_copy_field \ |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 241 | _(vni) \ |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 242 | _(protocol) \ |
| 243 | _(mcast_sw_if_index) \ |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 244 | _(encap_fib_index) \ |
Hongjun Ni | 7deb139 | 2016-06-15 22:49:23 +0800 | [diff] [blame] | 245 | _(decap_fib_index) |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 246 | |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 247 | #define foreach_copy_ipv4 { \ |
| 248 | _(local.ip4.as_u32) \ |
| 249 | _(remote.ip4.as_u32) \ |
| 250 | } |
| 251 | |
| 252 | #define foreach_copy_ipv6 { \ |
| 253 | _(local.ip6.as_u64[0]) \ |
| 254 | _(local.ip6.as_u64[1]) \ |
| 255 | _(remote.ip6.as_u64[0]) \ |
| 256 | _(remote.ip6.as_u64[1]) \ |
| 257 | } |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 258 | |
| 259 | |
Keith Burns (alagalah) | d46cca1 | 2016-08-25 11:21:39 -0700 | [diff] [blame] | 260 | /** |
| 261 | * @brief Calculate IPv4 VXLAN GPE rewrite header |
| 262 | * |
| 263 | * @param *t |
| 264 | * |
| 265 | * @return rc |
| 266 | * |
| 267 | */ |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 268 | int vxlan4_gpe_rewrite (vxlan_gpe_tunnel_t * t, u32 extension_size, |
Vengada Govindan | 6d403a0 | 2016-10-12 05:54:09 -0700 | [diff] [blame] | 269 | u8 protocol_override, uword encap_next_node) |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 270 | { |
| 271 | u8 *rw = 0; |
| 272 | ip4_header_t * ip0; |
| 273 | ip4_vxlan_gpe_header_t * h0; |
| 274 | int len; |
| 275 | |
Vengada Govindan | 6d403a0 | 2016-10-12 05:54:09 -0700 | [diff] [blame] | 276 | len = sizeof (*h0) + extension_size; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 277 | |
Vengada Govindan | 6d403a0 | 2016-10-12 05:54:09 -0700 | [diff] [blame] | 278 | vec_free(t->rewrite); |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 279 | vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES); |
| 280 | |
| 281 | h0 = (ip4_vxlan_gpe_header_t *) rw; |
| 282 | |
| 283 | /* Fixed portion of the (outer) ip4 header */ |
| 284 | ip0 = &h0->ip4; |
| 285 | ip0->ip_version_and_header_length = 0x45; |
| 286 | ip0->ttl = 254; |
| 287 | ip0->protocol = IP_PROTOCOL_UDP; |
| 288 | |
| 289 | /* we fix up the ip4 header length and checksum after-the-fact */ |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 290 | ip0->src_address.as_u32 = t->local.ip4.as_u32; |
| 291 | ip0->dst_address.as_u32 = t->remote.ip4.as_u32; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 292 | ip0->checksum = ip4_header_checksum (ip0); |
| 293 | |
| 294 | /* UDP header, randomize src port on something, maybe? */ |
| 295 | h0->udp.src_port = clib_host_to_net_u16 (4790); |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 296 | h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_VXLAN_GPE); |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 297 | |
| 298 | /* VXLAN header. Are we having fun yet? */ |
| 299 | h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P; |
| 300 | h0->vxlan.ver_res = VXLAN_GPE_VERSION; |
Vengada Govindan | 6d403a0 | 2016-10-12 05:54:09 -0700 | [diff] [blame] | 301 | if (protocol_override) |
| 302 | { |
| 303 | h0->vxlan.protocol = protocol_override; |
| 304 | } |
| 305 | else |
| 306 | { |
| 307 | h0->vxlan.protocol = t->protocol; |
| 308 | } |
| 309 | t->rewrite_size = sizeof(ip4_vxlan_gpe_header_t) + extension_size; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 310 | h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8); |
| 311 | |
| 312 | t->rewrite = rw; |
Vengada Govindan | 6d403a0 | 2016-10-12 05:54:09 -0700 | [diff] [blame] | 313 | t->encap_next_node = encap_next_node; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 314 | return (0); |
| 315 | } |
| 316 | |
Keith Burns (alagalah) | d46cca1 | 2016-08-25 11:21:39 -0700 | [diff] [blame] | 317 | /** |
| 318 | * @brief Calculate IPv6 VXLAN GPE rewrite header |
| 319 | * |
| 320 | * @param *t |
| 321 | * |
| 322 | * @return rc |
| 323 | * |
| 324 | */ |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 325 | int vxlan6_gpe_rewrite (vxlan_gpe_tunnel_t * t, u32 extension_size, |
Vengada Govindan | 6d403a0 | 2016-10-12 05:54:09 -0700 | [diff] [blame] | 326 | u8 protocol_override, uword encap_next_node) |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 327 | { |
| 328 | u8 *rw = 0; |
| 329 | ip6_header_t * ip0; |
| 330 | ip6_vxlan_gpe_header_t * h0; |
| 331 | int len; |
| 332 | |
Vengada Govindan | 6d403a0 | 2016-10-12 05:54:09 -0700 | [diff] [blame] | 333 | len = sizeof (*h0) + extension_size; |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 334 | |
Vengada Govindan | 6d403a0 | 2016-10-12 05:54:09 -0700 | [diff] [blame] | 335 | vec_free(t->rewrite); |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 336 | vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES); |
| 337 | |
| 338 | h0 = (ip6_vxlan_gpe_header_t *) rw; |
| 339 | |
| 340 | /* Fixed portion of the (outer) ip4 header */ |
| 341 | ip0 = &h0->ip6; |
| 342 | ip0->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32(6 << 28); |
| 343 | ip0->hop_limit = 255; |
| 344 | ip0->protocol = IP_PROTOCOL_UDP; |
| 345 | |
| 346 | ip0->src_address.as_u64[0] = t->local.ip6.as_u64[0]; |
| 347 | ip0->src_address.as_u64[1] = t->local.ip6.as_u64[1]; |
| 348 | ip0->dst_address.as_u64[0] = t->remote.ip6.as_u64[0]; |
| 349 | ip0->dst_address.as_u64[1] = t->remote.ip6.as_u64[1]; |
| 350 | |
| 351 | /* UDP header, randomize src port on something, maybe? */ |
| 352 | h0->udp.src_port = clib_host_to_net_u16 (4790); |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 353 | h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_VXLAN_GPE); |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 354 | |
| 355 | /* VXLAN header. Are we having fun yet? */ |
| 356 | h0->vxlan.flags = VXLAN_GPE_FLAGS_I | VXLAN_GPE_FLAGS_P; |
| 357 | h0->vxlan.ver_res = VXLAN_GPE_VERSION; |
Vengada Govindan | 6d403a0 | 2016-10-12 05:54:09 -0700 | [diff] [blame] | 358 | if (protocol_override) |
| 359 | { |
| 360 | h0->vxlan.protocol = t->protocol; |
| 361 | } |
| 362 | else |
| 363 | { |
| 364 | h0->vxlan.protocol = protocol_override; |
| 365 | } |
| 366 | t->rewrite_size = sizeof(ip4_vxlan_gpe_header_t) + extension_size; |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 367 | h0->vxlan.vni_res = clib_host_to_net_u32 (t->vni<<8); |
| 368 | |
| 369 | t->rewrite = rw; |
Vengada Govindan | 6d403a0 | 2016-10-12 05:54:09 -0700 | [diff] [blame] | 370 | t->encap_next_node = encap_next_node; |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 371 | return (0); |
| 372 | } |
| 373 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 374 | static void |
| 375 | hash_set_key_copy (uword ** h, void * key, uword v) { |
| 376 | size_t ksz = hash_header(*h)->user; |
| 377 | void * copy = clib_mem_alloc (ksz); |
| 378 | clib_memcpy (copy, key, ksz); |
| 379 | hash_set_mem (*h, copy, v); |
| 380 | } |
| 381 | |
| 382 | static void |
| 383 | hash_unset_key_free (uword ** h, void * key) { |
| 384 | hash_pair_t * hp = hash_get_pair_mem (*h, key); |
| 385 | ASSERT (hp); |
| 386 | key = uword_to_pointer (hp->key, void *); |
| 387 | hash_unset_mem (*h, key); |
| 388 | clib_mem_free (key); |
| 389 | } |
| 390 | |
| 391 | static uword |
| 392 | vtep_addr_ref(ip46_address_t *ip) |
| 393 | { |
| 394 | uword *vtep = ip46_address_is_ip4(ip) ? |
| 395 | hash_get (vxlan_gpe_main.vtep4, ip->ip4.as_u32) : |
| 396 | hash_get_mem (vxlan_gpe_main.vtep6, &ip->ip6); |
| 397 | if (vtep) |
| 398 | return ++(*vtep); |
| 399 | ip46_address_is_ip4(ip) ? |
| 400 | hash_set (vxlan_gpe_main.vtep4, ip->ip4.as_u32, 1) : |
| 401 | hash_set_key_copy (&vxlan_gpe_main.vtep6, &ip->ip6, 1); |
| 402 | return 1; |
| 403 | } |
| 404 | |
| 405 | static uword |
| 406 | vtep_addr_unref(ip46_address_t *ip) |
| 407 | { |
| 408 | uword *vtep = ip46_address_is_ip4(ip) ? |
| 409 | hash_get (vxlan_gpe_main.vtep4, ip->ip4.as_u32) : |
| 410 | hash_get_mem (vxlan_gpe_main.vtep6, &ip->ip6); |
| 411 | ASSERT(vtep); |
| 412 | if (--(*vtep) != 0) |
| 413 | return *vtep; |
| 414 | ip46_address_is_ip4(ip) ? |
| 415 | hash_unset (vxlan_gpe_main.vtep4, ip->ip4.as_u32) : |
| 416 | hash_unset_key_free (&vxlan_gpe_main.vtep6, &ip->ip6); |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | typedef CLIB_PACKED(union { |
| 421 | struct { |
| 422 | fib_node_index_t mfib_entry_index; |
| 423 | adj_index_t mcast_adj_index; |
| 424 | }; |
| 425 | u64 as_u64; |
| 426 | }) mcast_shared_t; |
| 427 | |
| 428 | static inline mcast_shared_t |
| 429 | mcast_shared_get(ip46_address_t * ip) |
| 430 | { |
| 431 | ASSERT(ip46_address_is_multicast(ip)); |
| 432 | uword * p = hash_get_mem (vxlan_gpe_main.mcast_shared, ip); |
| 433 | ASSERT(p); |
| 434 | return (mcast_shared_t) { .as_u64 = *p }; |
| 435 | } |
| 436 | |
| 437 | static inline void |
| 438 | mcast_shared_add(ip46_address_t *remote, |
| 439 | fib_node_index_t mfei, |
| 440 | adj_index_t ai) |
| 441 | { |
| 442 | mcast_shared_t new_ep = { |
| 443 | .mcast_adj_index = ai, |
| 444 | .mfib_entry_index = mfei, |
| 445 | }; |
| 446 | |
| 447 | hash_set_key_copy (&vxlan_gpe_main.mcast_shared, remote, new_ep.as_u64); |
| 448 | } |
| 449 | |
| 450 | static inline void |
| 451 | mcast_shared_remove(ip46_address_t *remote) |
| 452 | { |
| 453 | mcast_shared_t ep = mcast_shared_get(remote); |
| 454 | |
| 455 | adj_unlock(ep.mcast_adj_index); |
| 456 | mfib_table_entry_delete_index(ep.mfib_entry_index, |
| 457 | MFIB_SOURCE_VXLAN_GPE); |
| 458 | |
| 459 | hash_unset_key_free (&vxlan_gpe_main.mcast_shared, remote); |
| 460 | } |
| 461 | |
| 462 | static inline fib_protocol_t |
| 463 | fib_ip_proto(bool is_ip6) |
| 464 | { |
| 465 | return (is_ip6) ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4; |
| 466 | } |
| 467 | |
Keith Burns (alagalah) | d46cca1 | 2016-08-25 11:21:39 -0700 | [diff] [blame] | 468 | /** |
| 469 | * @brief Add or Del a VXLAN GPE tunnel |
| 470 | * |
| 471 | * @param *a |
| 472 | * @param *sw_if_index |
| 473 | * |
| 474 | * @return rc |
| 475 | * |
| 476 | */ |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 477 | int vnet_vxlan_gpe_add_del_tunnel |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 478 | (vnet_vxlan_gpe_add_del_tunnel_args_t *a, u32 * sw_if_indexp) |
| 479 | { |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 480 | vxlan_gpe_main_t * ngm = &vxlan_gpe_main; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 481 | vxlan_gpe_tunnel_t *t = 0; |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 482 | vnet_main_t * vnm = ngm->vnet_main; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 483 | vnet_hw_interface_t * hi; |
| 484 | uword * p; |
| 485 | u32 hw_if_index = ~0; |
| 486 | u32 sw_if_index = ~0; |
| 487 | int rv; |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 488 | vxlan4_gpe_tunnel_key_t key4, *key4_copy; |
| 489 | vxlan6_gpe_tunnel_key_t key6, *key6_copy; |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 490 | u32 is_ip6 = a->is_ip6; |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 491 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 492 | if (!is_ip6) |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 493 | { |
| 494 | key4.local = a->local.ip4.as_u32; |
| 495 | key4.remote = a->remote.ip4.as_u32; |
| 496 | key4.vni = clib_host_to_net_u32 (a->vni << 8); |
| 497 | key4.pad = 0; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 498 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 499 | p = hash_get_mem(ngm->vxlan4_gpe_tunnel_by_key, &key4); |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 500 | } |
| 501 | else |
| 502 | { |
| 503 | key6.local.as_u64[0] = a->local.ip6.as_u64[0]; |
| 504 | key6.local.as_u64[1] = a->local.ip6.as_u64[1]; |
| 505 | key6.remote.as_u64[0] = a->remote.ip6.as_u64[0]; |
| 506 | key6.remote.as_u64[1] = a->remote.ip6.as_u64[1]; |
| 507 | key6.vni = clib_host_to_net_u32 (a->vni << 8); |
| 508 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 509 | p = hash_get_mem(ngm->vxlan6_gpe_tunnel_by_key, &key6); |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 510 | } |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 511 | |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 512 | if (a->is_add) |
| 513 | { |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 514 | l2input_main_t * l2im = &l2input_main; |
| 515 | |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 516 | /* adding a tunnel: tunnel must not already exist */ |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 517 | if (p) |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 518 | return VNET_API_ERROR_TUNNEL_EXIST; |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 519 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 520 | pool_get_aligned (ngm->tunnels, t, CLIB_CACHE_LINE_BYTES); |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 521 | memset (t, 0, sizeof (*t)); |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 522 | |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 523 | /* copy from arg structure */ |
| 524 | #define _(x) t->x = a->x; |
| 525 | foreach_gpe_copy_field; |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 526 | if (!a->is_ip6) foreach_copy_ipv4 |
| 527 | else foreach_copy_ipv6 |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 528 | #undef _ |
| 529 | |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 530 | if (!a->is_ip6) t->flags |= VXLAN_GPE_TUNNEL_IS_IPV4; |
| 531 | |
| 532 | if (!a->is_ip6) { |
Vengada Govindan | 6d403a0 | 2016-10-12 05:54:09 -0700 | [diff] [blame] | 533 | rv = vxlan4_gpe_rewrite (t, 0, 0, VXLAN_GPE_ENCAP_NEXT_IP4_LOOKUP); |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 534 | } else { |
Vengada Govindan | 6d403a0 | 2016-10-12 05:54:09 -0700 | [diff] [blame] | 535 | rv = vxlan6_gpe_rewrite (t, 0, 0, VXLAN_GPE_ENCAP_NEXT_IP6_LOOKUP); |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 536 | } |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 537 | |
| 538 | if (rv) |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 539 | { |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 540 | pool_put (ngm->tunnels, t); |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 541 | return rv; |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 542 | } |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 543 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 544 | if (!is_ip6) |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 545 | { |
| 546 | key4_copy = clib_mem_alloc (sizeof (*key4_copy)); |
| 547 | clib_memcpy (key4_copy, &key4, sizeof (*key4_copy)); |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 548 | hash_set_mem (ngm->vxlan4_gpe_tunnel_by_key, key4_copy, |
| 549 | t - ngm->tunnels); |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 550 | } |
| 551 | else |
| 552 | { |
| 553 | key6_copy = clib_mem_alloc (sizeof (*key6_copy)); |
Florin Coras | 1d3b2ab | 2016-05-31 15:53:44 +0300 | [diff] [blame] | 554 | clib_memcpy (key6_copy, &key6, sizeof (*key6_copy)); |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 555 | hash_set_mem (ngm->vxlan6_gpe_tunnel_by_key, key6_copy, |
| 556 | t - ngm->tunnels); |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 557 | } |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 558 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 559 | if (vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices) > 0) |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 560 | { |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 561 | vnet_interface_main_t * im = &vnm->interface_main; |
| 562 | hw_if_index = ngm->free_vxlan_gpe_tunnel_hw_if_indices |
| 563 | [vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices)-1]; |
| 564 | _vec_len (ngm->free_vxlan_gpe_tunnel_hw_if_indices) -= 1; |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 565 | |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 566 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 567 | hi->dev_instance = t - ngm->tunnels; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 568 | hi->hw_instance = hi->dev_instance; |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 569 | /* clear old stats of freed tunnel before reuse */ |
| 570 | sw_if_index = hi->sw_if_index; |
| 571 | vnet_interface_counter_lock(im); |
| 572 | vlib_zero_combined_counter |
| 573 | (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX], sw_if_index); |
| 574 | vlib_zero_combined_counter |
| 575 | (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX], sw_if_index); |
| 576 | vlib_zero_simple_counter |
| 577 | (&im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP], sw_if_index); |
| 578 | vnet_interface_counter_unlock(im); |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 579 | } |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 580 | else |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 581 | { |
| 582 | hw_if_index = vnet_register_interface |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 583 | (vnm, vxlan_gpe_device_class.index, t - ngm->tunnels, |
| 584 | vxlan_gpe_hw_class.index, t - ngm->tunnels); |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 585 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 586 | hi->output_node_index = vxlan_gpe_encap_node.index; |
| 587 | } |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 588 | |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 589 | t->hw_if_index = hw_if_index; |
| 590 | t->sw_if_index = sw_if_index = hi->sw_if_index; |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 591 | vec_validate_init_empty (ngm->tunnel_index_by_sw_if_index, sw_if_index, ~0); |
| 592 | ngm->tunnel_index_by_sw_if_index[sw_if_index] = t - ngm->tunnels; |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 593 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 594 | /* setup l2 input config with l2 feature and bd 0 to drop packet */ |
| 595 | vec_validate (l2im->configs, sw_if_index); |
| 596 | l2im->configs[sw_if_index].feature_bitmap = L2INPUT_FEAT_DROP; |
| 597 | l2im->configs[sw_if_index].bd_index = 0; |
| 598 | |
| 599 | vnet_sw_interface_t * si = vnet_get_sw_interface (vnm, sw_if_index); |
| 600 | si->flags &= ~VNET_SW_INTERFACE_FLAG_HIDDEN; |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 601 | vnet_sw_interface_set_flags (vnm, hi->sw_if_index, |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 602 | VNET_SW_INTERFACE_FLAG_ADMIN_UP); |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 603 | fib_node_init(&t->node, FIB_NODE_TYPE_VXLAN_GPE_TUNNEL); |
| 604 | fib_prefix_t tun_remote_pfx; |
| 605 | u32 encap_index = vxlan_gpe_encap_node.index; |
| 606 | vnet_flood_class_t flood_class = VNET_FLOOD_CLASS_TUNNEL_NORMAL; |
| 607 | |
| 608 | fib_prefix_from_ip46_addr(&t->remote, &tun_remote_pfx); |
| 609 | if (!ip46_address_is_multicast(&t->remote)) |
| 610 | { |
| 611 | /* Unicast tunnel - |
| 612 | * source the FIB entry for the tunnel's destination |
| 613 | * and become a child thereof. The tunnel will then get poked |
| 614 | * when the forwarding for the entry updates, and the tunnel can |
| 615 | * re-stack accordingly |
| 616 | */ |
| 617 | vtep_addr_ref(&t->local); |
| 618 | t->fib_entry_index = fib_table_entry_special_add |
| 619 | (t->encap_fib_index, &tun_remote_pfx, FIB_SOURCE_RR, |
| 620 | FIB_ENTRY_FLAG_NONE); |
| 621 | t->sibling_index = fib_entry_child_add |
| 622 | (t->fib_entry_index, FIB_NODE_TYPE_VXLAN_GPE_TUNNEL, t - ngm->tunnels); |
| 623 | vxlan_gpe_tunnel_restack_dpo(t); |
| 624 | } |
| 625 | else |
| 626 | { |
| 627 | /* Multicast tunnel - |
| 628 | * as the same mcast group can be used for mutiple mcast tunnels |
| 629 | * with different VNIs, create the output fib adjecency only if |
| 630 | * it does not already exist |
| 631 | */ |
| 632 | fib_protocol_t fp = fib_ip_proto(is_ip6); |
| 633 | |
| 634 | if (vtep_addr_ref(&t->remote) == 1) |
| 635 | { |
| 636 | fib_node_index_t mfei; |
| 637 | adj_index_t ai; |
| 638 | fib_route_path_t path = { |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 639 | .frp_proto = fib_proto_to_dpo(fp), |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 640 | .frp_addr = zero_addr, |
| 641 | .frp_sw_if_index = 0xffffffff, |
| 642 | .frp_fib_index = ~0, |
| 643 | .frp_weight = 0, |
| 644 | .frp_flags = FIB_ROUTE_PATH_LOCAL, |
| 645 | }; |
| 646 | const mfib_prefix_t mpfx = { |
| 647 | .fp_proto = fp, |
| 648 | .fp_len = (is_ip6 ? 128 : 32), |
| 649 | .fp_grp_addr = tun_remote_pfx.fp_addr, |
| 650 | }; |
| 651 | |
| 652 | /* |
| 653 | * Setup the (*,G) to receive traffic on the mcast group |
| 654 | * - the forwarding interface is for-us |
| 655 | * - the accepting interface is that from the API |
| 656 | */ |
| 657 | mfib_table_entry_path_update(t->encap_fib_index, |
| 658 | &mpfx, |
| 659 | MFIB_SOURCE_VXLAN_GPE, |
| 660 | &path, |
| 661 | MFIB_ITF_FLAG_FORWARD); |
| 662 | |
| 663 | path.frp_sw_if_index = a->mcast_sw_if_index; |
| 664 | path.frp_flags = FIB_ROUTE_PATH_FLAG_NONE; |
| 665 | mfei = mfib_table_entry_path_update(t->encap_fib_index, |
| 666 | &mpfx, |
| 667 | MFIB_SOURCE_VXLAN_GPE, |
| 668 | &path, |
| 669 | MFIB_ITF_FLAG_ACCEPT); |
| 670 | |
| 671 | /* |
| 672 | * Create the mcast adjacency to send traffic to the group |
| 673 | */ |
| 674 | ai = adj_mcast_add_or_lock(fp, |
| 675 | fib_proto_to_link(fp), |
| 676 | a->mcast_sw_if_index); |
| 677 | |
| 678 | /* |
| 679 | * create a new end-point |
| 680 | */ |
| 681 | mcast_shared_add(&t->remote, mfei, ai); |
| 682 | } |
| 683 | |
| 684 | dpo_id_t dpo = DPO_INVALID; |
| 685 | mcast_shared_t ep = mcast_shared_get(&t->remote); |
| 686 | |
| 687 | /* Stack shared mcast remote mac addr rewrite on encap */ |
| 688 | dpo_set (&dpo, DPO_ADJACENCY_MCAST, |
| 689 | fib_proto_to_dpo(fp), |
| 690 | ep.mcast_adj_index); |
| 691 | |
| 692 | dpo_stack_from_node (encap_index, &t->next_dpo, &dpo); |
| 693 | dpo_reset (&dpo); |
| 694 | flood_class = VNET_FLOOD_CLASS_TUNNEL_MASTER; |
| 695 | } |
| 696 | |
| 697 | /* Set vxlan tunnel output node */ |
| 698 | hi->output_node_index = encap_index; |
| 699 | |
| 700 | vnet_get_sw_interface (vnet_get_main(), sw_if_index)->flood_class = flood_class; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 701 | } |
| 702 | else |
| 703 | { |
| 704 | /* deleting a tunnel: tunnel must exist */ |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 705 | if (!p) |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 706 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 707 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 708 | t = pool_elt_at_index (ngm->tunnels, p[0]); |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 709 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 710 | sw_if_index = t->sw_if_index; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 711 | vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */); |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 712 | vnet_sw_interface_t * si = vnet_get_sw_interface (vnm, t->sw_if_index); |
| 713 | si->flags |= VNET_SW_INTERFACE_FLAG_HIDDEN; |
| 714 | set_int_l2_mode(ngm->vlib_main, vnm, MODE_L3, t->sw_if_index, 0, 0, 0, 0); |
| 715 | vec_add1 (ngm->free_vxlan_gpe_tunnel_hw_if_indices, t->hw_if_index); |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 716 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 717 | ngm->tunnel_index_by_sw_if_index[t->sw_if_index] = ~0; |
Hongjun Ni | c0959c9 | 2016-06-16 20:18:15 +0800 | [diff] [blame] | 718 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 719 | if (!is_ip6) |
| 720 | hash_unset (ngm->vxlan4_gpe_tunnel_by_key, key4.as_u64); |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 721 | else |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 722 | hash_unset_key_free (&ngm->vxlan6_gpe_tunnel_by_key, &key6); |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 723 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 724 | if (!ip46_address_is_multicast(&t->remote)) |
| 725 | { |
| 726 | vtep_addr_unref(&t->local); |
| 727 | fib_entry_child_remove(t->fib_entry_index, t->sibling_index); |
| 728 | fib_table_entry_delete_index(t->fib_entry_index, FIB_SOURCE_RR); |
| 729 | } |
| 730 | else if (vtep_addr_unref(&t->remote) == 0) |
| 731 | { |
| 732 | mcast_shared_remove(&t->remote); |
| 733 | } |
| 734 | |
| 735 | fib_node_deinit(&t->node); |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 736 | vec_free (t->rewrite); |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 737 | pool_put (ngm->tunnels, t); |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | if (sw_if_indexp) |
| 741 | *sw_if_indexp = sw_if_index; |
| 742 | |
| 743 | return 0; |
| 744 | } |
| 745 | |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 746 | static clib_error_t * |
| 747 | vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm, |
| 748 | unformat_input_t * input, |
| 749 | vlib_cli_command_t * cmd) |
| 750 | { |
| 751 | unformat_input_t _line_input, * line_input = &_line_input; |
| 752 | u8 is_add = 1; |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 753 | ip46_address_t local, remote; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 754 | u8 local_set = 0; |
| 755 | u8 remote_set = 0; |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 756 | u8 grp_set = 0; |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 757 | u8 ipv4_set = 0; |
| 758 | u8 ipv6_set = 0; |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 759 | u32 mcast_sw_if_index = ~0; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 760 | u32 encap_fib_index = 0; |
| 761 | u32 decap_fib_index = 0; |
| 762 | u8 protocol = VXLAN_GPE_PROTOCOL_IP4; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 763 | u32 vni; |
| 764 | u8 vni_set = 0; |
| 765 | int rv; |
| 766 | u32 tmp; |
| 767 | vnet_vxlan_gpe_add_del_tunnel_args_t _a, * a = &_a; |
Pierre Pfister | 78ea9c2 | 2016-05-23 12:51:54 +0100 | [diff] [blame] | 768 | u32 sw_if_index; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 769 | clib_error_t *error = NULL; |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 770 | |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 771 | /* Get a line of input. */ |
| 772 | if (! unformat_user (input, unformat_line_input, line_input)) |
| 773 | return 0; |
| 774 | |
| 775 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { |
| 776 | if (unformat (line_input, "del")) |
| 777 | is_add = 0; |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 778 | else if (unformat (line_input, "local %U", |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 779 | unformat_ip4_address, &local.ip4)) |
| 780 | { |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 781 | local_set = 1; |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 782 | ipv4_set = 1; |
| 783 | } |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 784 | else if (unformat (line_input, "remote %U", |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 785 | unformat_ip4_address, &remote.ip4)) |
| 786 | { |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 787 | remote_set = 1; |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 788 | ipv4_set = 1; |
| 789 | } |
| 790 | else if (unformat (line_input, "local %U", |
| 791 | unformat_ip6_address, &local.ip6)) |
| 792 | { |
| 793 | local_set = 1; |
| 794 | ipv6_set = 1; |
| 795 | } |
| 796 | else if (unformat (line_input, "remote %U", |
| 797 | unformat_ip6_address, &remote.ip6)) |
| 798 | { |
| 799 | remote_set = 1; |
| 800 | ipv6_set = 1; |
| 801 | } |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 802 | else if (unformat (line_input, "group %U %U", |
| 803 | unformat_ip4_address, &remote.ip4, |
| 804 | unformat_vnet_sw_interface, |
| 805 | vnet_get_main(), &mcast_sw_if_index)) |
| 806 | { |
| 807 | grp_set = remote_set = 1; |
| 808 | ipv4_set = 1; |
| 809 | } |
| 810 | else if (unformat (line_input, "group %U %U", |
| 811 | unformat_ip6_address, &remote.ip6, |
| 812 | unformat_vnet_sw_interface, |
| 813 | vnet_get_main(), &mcast_sw_if_index)) |
| 814 | { |
| 815 | grp_set = remote_set = 1; |
| 816 | ipv6_set = 1; |
| 817 | } |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 818 | else if (unformat (line_input, "encap-vrf-id %d", &tmp)) |
| 819 | { |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 820 | if (ipv6_set) |
Neale Ranns | 630198f | 2017-05-22 09:20:20 -0400 | [diff] [blame] | 821 | encap_fib_index = fib_table_find (FIB_PROTOCOL_IP6, tmp); |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 822 | else |
Neale Ranns | 630198f | 2017-05-22 09:20:20 -0400 | [diff] [blame] | 823 | encap_fib_index = fib_table_find (FIB_PROTOCOL_IP4, tmp); |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 824 | |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 825 | if (encap_fib_index == ~0) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 826 | { |
| 827 | error = clib_error_return (0, "nonexistent encap fib id %d", tmp); |
| 828 | goto done; |
| 829 | } |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 830 | } |
| 831 | else if (unformat (line_input, "decap-vrf-id %d", &tmp)) |
| 832 | { |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 833 | if (ipv6_set) |
Neale Ranns | 630198f | 2017-05-22 09:20:20 -0400 | [diff] [blame] | 834 | decap_fib_index = fib_table_find (FIB_PROTOCOL_IP6, tmp); |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 835 | else |
Neale Ranns | 630198f | 2017-05-22 09:20:20 -0400 | [diff] [blame] | 836 | decap_fib_index = fib_table_find (FIB_PROTOCOL_IP4, tmp); |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 837 | |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 838 | if (decap_fib_index == ~0) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 839 | { |
| 840 | error = clib_error_return (0, "nonexistent decap fib id %d", tmp); |
| 841 | goto done; |
| 842 | } |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 843 | } |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 844 | else if (unformat (line_input, "vni %d", &vni)) |
| 845 | vni_set = 1; |
| 846 | else if (unformat(line_input, "next-ip4")) |
| 847 | protocol = VXLAN_GPE_PROTOCOL_IP4; |
| 848 | else if (unformat(line_input, "next-ip6")) |
| 849 | protocol = VXLAN_GPE_PROTOCOL_IP6; |
| 850 | else if (unformat(line_input, "next-ethernet")) |
| 851 | protocol = VXLAN_GPE_PROTOCOL_ETHERNET; |
| 852 | else if (unformat(line_input, "next-nsh")) |
| 853 | protocol = VXLAN_GPE_PROTOCOL_NSH; |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 854 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 855 | { |
| 856 | error = clib_error_return (0, "parse error: '%U'", |
| 857 | format_unformat_error, line_input); |
| 858 | goto done; |
| 859 | } |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 862 | if (local_set == 0) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 863 | { |
| 864 | error = clib_error_return (0, "tunnel local address not specified"); |
| 865 | goto done; |
| 866 | } |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 867 | |
| 868 | if (remote_set == 0) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 869 | { |
| 870 | error = clib_error_return (0, "tunnel remote address not specified"); |
| 871 | goto done; |
| 872 | } |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 873 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 874 | if (grp_set && !ip46_address_is_multicast(&remote)) |
| 875 | { |
| 876 | error = clib_error_return (0, "tunnel group address not multicast"); |
| 877 | goto done; |
| 878 | } |
| 879 | |
| 880 | if (grp_set == 0 && ip46_address_is_multicast(&remote)) |
| 881 | { |
| 882 | error = clib_error_return (0, "remote address must be unicast"); |
| 883 | goto done; |
| 884 | } |
| 885 | |
| 886 | if (grp_set && mcast_sw_if_index == ~0) |
| 887 | { |
| 888 | error = clib_error_return (0, "tunnel nonexistent multicast device"); |
| 889 | goto done; |
| 890 | } |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 891 | if (ipv4_set && ipv6_set) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 892 | { |
| 893 | error = clib_error_return (0, "both IPv4 and IPv6 addresses specified"); |
| 894 | goto done; |
| 895 | } |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 896 | |
| 897 | if ((ipv4_set && memcmp(&local.ip4, &remote.ip4, sizeof(local.ip4)) == 0) || |
| 898 | (ipv6_set && memcmp(&local.ip6, &remote.ip6, sizeof(local.ip6)) == 0)) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 899 | { |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 900 | error = clib_error_return (0, "src and remote addresses are identical"); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 901 | goto done; |
| 902 | } |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 903 | |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 904 | if (vni_set == 0) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 905 | { |
| 906 | error = clib_error_return (0, "vni not specified"); |
| 907 | goto done; |
| 908 | } |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 909 | |
| 910 | memset (a, 0, sizeof (*a)); |
| 911 | |
| 912 | a->is_add = is_add; |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 913 | a->is_ip6 = ipv6_set; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 914 | |
| 915 | #define _(x) a->x = x; |
| 916 | foreach_gpe_copy_field; |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 917 | if (ipv4_set) foreach_copy_ipv4 |
| 918 | else foreach_copy_ipv6 |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 919 | #undef _ |
| 920 | |
Pierre Pfister | 78ea9c2 | 2016-05-23 12:51:54 +0100 | [diff] [blame] | 921 | rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index); |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 922 | |
| 923 | switch(rv) |
| 924 | { |
| 925 | case 0: |
Pierre Pfister | 78ea9c2 | 2016-05-23 12:51:54 +0100 | [diff] [blame] | 926 | vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index); |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 927 | break; |
| 928 | case VNET_API_ERROR_INVALID_DECAP_NEXT: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 929 | error = clib_error_return (0, "invalid decap-next..."); |
| 930 | goto done; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 931 | |
| 932 | case VNET_API_ERROR_TUNNEL_EXIST: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 933 | error = clib_error_return (0, "tunnel already exists..."); |
| 934 | goto done; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 935 | |
| 936 | case VNET_API_ERROR_NO_SUCH_ENTRY: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 937 | error = clib_error_return (0, "tunnel does not exist..."); |
| 938 | goto done; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 939 | |
| 940 | default: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 941 | error = clib_error_return |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 942 | (0, "vnet_vxlan_gpe_add_del_tunnel returned %d", rv); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 943 | goto done; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 944 | } |
| 945 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 946 | done: |
| 947 | unformat_free (line_input); |
| 948 | |
| 949 | return error; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 950 | } |
| 951 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 952 | /*? |
| 953 | * Add or delete a VXLAN-GPE Tunnel. |
| 954 | * |
| 955 | * VXLAN-GPE provides the features needed to allow L2 bridge domains (BDs) |
| 956 | * to span multiple servers. This is done by building an L2 overlay on |
| 957 | * top of an L3 network underlay using VXLAN-GPE tunnels. |
| 958 | * |
| 959 | * This makes it possible for servers to be co-located in the same data |
| 960 | * center or be separated geographically as long as they are reachable |
| 961 | * through the underlay L3 network. |
| 962 | * |
| 963 | * You can refer to this kind of L2 overlay bridge domain as a VXLAN-GPE sengment. |
| 964 | * |
| 965 | * @cliexpar |
| 966 | * Example of how to create a VXLAN-GPE Tunnel: |
| 967 | * @cliexcmd{create vxlan-gpe tunnel local 10.0.3.1 local 10.0.3.3 vni 13 encap-vrf-id 7} |
| 968 | * Example of how to delete a VXLAN Tunnel: |
| 969 | * @cliexcmd{create vxlan tunnel src 10.0.3.1 remote 10.0.3.3 vni 13 del} |
| 970 | ?*/ |
| 971 | /* *INDENT-OFF* */ |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 972 | VLIB_CLI_COMMAND (create_vxlan_gpe_tunnel_command, static) = { |
| 973 | .path = "create vxlan-gpe tunnel", |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 974 | .short_help = |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 975 | "create vxlan-gpe tunnel local <local-addr> " |
| 976 | " {remote <remote-addr>|group <mcast-addr> <intf-name>}" |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 977 | " vni <nn> [next-ip4][next-ip6][next-ethernet][next-nsh]" |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 978 | " [encap-vrf-id <nn>] [decap-vrf-id <nn>] [del]\n", |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 979 | .function = vxlan_gpe_add_del_tunnel_command_fn, |
| 980 | }; |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 981 | /* *INDENT-ON* */ |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 982 | |
Keith Burns (alagalah) | d46cca1 | 2016-08-25 11:21:39 -0700 | [diff] [blame] | 983 | /** |
| 984 | * @brief CLI function for showing VXLAN GPE tunnels |
| 985 | * |
| 986 | * @param *vm |
| 987 | * @param *input |
| 988 | * @param *cmd |
| 989 | * |
| 990 | * @return error |
| 991 | * |
| 992 | */ |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 993 | static clib_error_t * |
| 994 | show_vxlan_gpe_tunnel_command_fn (vlib_main_t * vm, |
| 995 | unformat_input_t * input, |
| 996 | vlib_cli_command_t * cmd) |
| 997 | { |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 998 | vxlan_gpe_main_t * ngm = &vxlan_gpe_main; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 999 | vxlan_gpe_tunnel_t * t; |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 1000 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 1001 | if (pool_elts (ngm->tunnels) == 0) |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 1002 | vlib_cli_output (vm, "No vxlan-gpe tunnels configured."); |
| 1003 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 1004 | pool_foreach (t, ngm->tunnels, |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 1005 | ({ |
| 1006 | vlib_cli_output (vm, "%U", format_vxlan_gpe_tunnel, t); |
| 1007 | })); |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 1008 | |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 1009 | return 0; |
| 1010 | } |
| 1011 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 1012 | /*? |
| 1013 | * Display all the VXLAN-GPE Tunnel entries. |
| 1014 | * |
| 1015 | * @cliexpar |
| 1016 | * Example of how to display the VXLAN-GPE Tunnel entries: |
| 1017 | * @cliexstart{show vxlan-gpe tunnel} |
| 1018 | * [0] local 10.0.3.1 remote 10.0.3.3 vni 13 encap_fib_index 0 sw_if_index 5 decap_next l2 |
| 1019 | * @cliexend |
| 1020 | ?*/ |
| 1021 | /* *INDENT-OFF* */ |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 1022 | VLIB_CLI_COMMAND (show_vxlan_gpe_tunnel_command, static) = { |
| 1023 | .path = "show vxlan-gpe", |
| 1024 | .function = show_vxlan_gpe_tunnel_command_fn, |
| 1025 | }; |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 1026 | /* *INDENT-ON* */ |
| 1027 | |
| 1028 | void vnet_int_vxlan_gpe_bypass_mode (u32 sw_if_index, |
| 1029 | u8 is_ip6, |
| 1030 | u8 is_enable) |
| 1031 | { |
| 1032 | if (is_ip6) |
| 1033 | vnet_feature_enable_disable ("ip6-unicast", "ip6-vxlan-gpe-bypass", |
| 1034 | sw_if_index, is_enable, 0, 0); |
| 1035 | else |
| 1036 | vnet_feature_enable_disable ("ip4-unicast", "ip4-vxlan-gpe-bypass", |
| 1037 | sw_if_index, is_enable, 0, 0); |
| 1038 | } |
| 1039 | |
| 1040 | |
| 1041 | static clib_error_t * |
| 1042 | set_ip_vxlan_gpe_bypass (u32 is_ip6, |
| 1043 | unformat_input_t * input, |
| 1044 | vlib_cli_command_t * cmd) |
| 1045 | { |
| 1046 | unformat_input_t _line_input, * line_input = &_line_input; |
| 1047 | vnet_main_t * vnm = vnet_get_main(); |
| 1048 | clib_error_t * error = 0; |
| 1049 | u32 sw_if_index, is_enable; |
| 1050 | |
| 1051 | sw_if_index = ~0; |
| 1052 | is_enable = 1; |
| 1053 | |
| 1054 | if (! unformat_user (input, unformat_line_input, line_input)) |
| 1055 | return 0; |
| 1056 | |
| 1057 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 1058 | { |
| 1059 | if (unformat_user (line_input, unformat_vnet_sw_interface, vnm, &sw_if_index)) |
| 1060 | ; |
| 1061 | else if (unformat (line_input, "del")) |
| 1062 | is_enable = 0; |
| 1063 | else |
| 1064 | { |
| 1065 | error = unformat_parse_error (line_input); |
| 1066 | goto done; |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | if (~0 == sw_if_index) |
| 1071 | { |
| 1072 | error = clib_error_return (0, "unknown interface `%U'", |
| 1073 | format_unformat_error, line_input); |
| 1074 | goto done; |
| 1075 | } |
| 1076 | |
| 1077 | vnet_int_vxlan_gpe_bypass_mode (sw_if_index, is_ip6, is_enable); |
| 1078 | |
| 1079 | done: |
| 1080 | unformat_free (line_input); |
| 1081 | |
| 1082 | return error; |
| 1083 | } |
| 1084 | |
| 1085 | static clib_error_t * |
| 1086 | set_ip4_vxlan_gpe_bypass (vlib_main_t * vm, |
| 1087 | unformat_input_t * input, |
| 1088 | vlib_cli_command_t * cmd) |
| 1089 | { |
| 1090 | return set_ip_vxlan_gpe_bypass (0, input, cmd); |
| 1091 | } |
| 1092 | |
| 1093 | /*? |
| 1094 | * This command adds the 'ip4-vxlan-gpe-bypass' graph node for a given interface. |
| 1095 | * By adding the IPv4 vxlan-gpe-bypass graph node to an interface, the node checks |
| 1096 | * for and validate input vxlan_gpe packet and bypass ip4-lookup, ip4-local, |
| 1097 | * ip4-udp-lookup nodes to speedup vxlan_gpe packet forwarding. This node will |
| 1098 | * cause extra overhead to for non-vxlan_gpe packets which is kept at a minimum. |
| 1099 | * |
| 1100 | * @cliexpar |
| 1101 | * @parblock |
| 1102 | * Example of graph node before ip4-vxlan-gpe-bypass is enabled: |
| 1103 | * @cliexstart{show vlib graph ip4-vxlan-gpe-bypass} |
| 1104 | * Name Next Previous |
| 1105 | * ip4-vxlan-gpe-bypass error-drop [0] |
| 1106 | * vxlan4-gpe-input [1] |
| 1107 | * ip4-lookup [2] |
| 1108 | * @cliexend |
| 1109 | * |
| 1110 | * Example of how to enable ip4-vxlan-gpe-bypass on an interface: |
| 1111 | * @cliexcmd{set interface ip vxlan-gpe-bypass GigabitEthernet2/0/0} |
| 1112 | * |
| 1113 | * Example of graph node after ip4-vxlan-gpe-bypass is enabled: |
| 1114 | * @cliexstart{show vlib graph ip4-vxlan-gpe-bypass} |
| 1115 | * Name Next Previous |
| 1116 | * ip4-vxlan-gpe-bypass error-drop [0] ip4-input |
| 1117 | * vxlan4-gpe-input [1] ip4-input-no-checksum |
| 1118 | * ip4-lookup [2] |
| 1119 | * @cliexend |
| 1120 | * |
| 1121 | * Example of how to display the feature enabed on an interface: |
| 1122 | * @cliexstart{show ip interface features GigabitEthernet2/0/0} |
| 1123 | * IP feature paths configured on GigabitEthernet2/0/0... |
| 1124 | * ... |
| 1125 | * ipv4 unicast: |
| 1126 | * ip4-vxlan-gpe-bypass |
| 1127 | * ip4-lookup |
| 1128 | * ... |
| 1129 | * @cliexend |
| 1130 | * |
| 1131 | * Example of how to disable ip4-vxlan-gpe-bypass on an interface: |
| 1132 | * @cliexcmd{set interface ip vxlan-gpe-bypass GigabitEthernet2/0/0 del} |
| 1133 | * @endparblock |
| 1134 | ?*/ |
| 1135 | /* *INDENT-OFF* */ |
| 1136 | VLIB_CLI_COMMAND (set_interface_ip_vxlan_gpe_bypass_command, static) = { |
| 1137 | .path = "set interface ip vxlan-gpe-bypass", |
| 1138 | .function = set_ip4_vxlan_gpe_bypass, |
| 1139 | .short_help = "set interface ip vxlan-gpe-bypass <interface> [del]", |
| 1140 | }; |
| 1141 | /* *INDENT-ON* */ |
| 1142 | |
| 1143 | static clib_error_t * |
| 1144 | set_ip6_vxlan_gpe_bypass (vlib_main_t * vm, |
| 1145 | unformat_input_t * input, |
| 1146 | vlib_cli_command_t * cmd) |
| 1147 | { |
| 1148 | return set_ip_vxlan_gpe_bypass (1, input, cmd); |
| 1149 | } |
| 1150 | |
| 1151 | /*? |
| 1152 | * This command adds the 'ip6-vxlan-gpe-bypass' graph node for a given interface. |
| 1153 | * By adding the IPv6 vxlan-gpe-bypass graph node to an interface, the node checks |
| 1154 | * for and validate input vxlan_gpe packet and bypass ip6-lookup, ip6-local, |
| 1155 | * ip6-udp-lookup nodes to speedup vxlan_gpe packet forwarding. This node will |
| 1156 | * cause extra overhead to for non-vxlan_gpe packets which is kept at a minimum. |
| 1157 | * |
| 1158 | * @cliexpar |
| 1159 | * @parblock |
| 1160 | * Example of graph node before ip6-vxlan-gpe-bypass is enabled: |
| 1161 | * @cliexstart{show vlib graph ip6-vxlan-gpe-bypass} |
| 1162 | * Name Next Previous |
| 1163 | * ip6-vxlan-gpe-bypass error-drop [0] |
| 1164 | * vxlan6-gpe-input [1] |
| 1165 | * ip6-lookup [2] |
| 1166 | * @cliexend |
| 1167 | * |
| 1168 | * Example of how to enable ip6-vxlan-gpe-bypass on an interface: |
| 1169 | * @cliexcmd{set interface ip6 vxlan-gpe-bypass GigabitEthernet2/0/0} |
| 1170 | * |
| 1171 | * Example of graph node after ip6-vxlan-gpe-bypass is enabled: |
| 1172 | * @cliexstart{show vlib graph ip6-vxlan-gpe-bypass} |
| 1173 | * Name Next Previous |
| 1174 | * ip6-vxlan-gpe-bypass error-drop [0] ip6-input |
| 1175 | * vxlan6-gpe-input [1] ip4-input-no-checksum |
| 1176 | * ip6-lookup [2] |
| 1177 | * @cliexend |
| 1178 | * |
| 1179 | * Example of how to display the feature enabed on an interface: |
| 1180 | * @cliexstart{show ip interface features GigabitEthernet2/0/0} |
| 1181 | * IP feature paths configured on GigabitEthernet2/0/0... |
| 1182 | * ... |
| 1183 | * ipv6 unicast: |
| 1184 | * ip6-vxlan-gpe-bypass |
| 1185 | * ip6-lookup |
| 1186 | * ... |
| 1187 | * @cliexend |
| 1188 | * |
| 1189 | * Example of how to disable ip6-vxlan-gpe-bypass on an interface: |
| 1190 | * @cliexcmd{set interface ip6 vxlan-gpe-bypass GigabitEthernet2/0/0 del} |
| 1191 | * @endparblock |
| 1192 | ?*/ |
| 1193 | /* *INDENT-OFF* */ |
| 1194 | VLIB_CLI_COMMAND (set_interface_ip6_vxlan_gpe_bypass_command, static) = { |
| 1195 | .path = "set interface ip6 vxlan-gpe-bypass", |
| 1196 | .function = set_ip6_vxlan_gpe_bypass, |
| 1197 | .short_help = "set interface ip vxlan-gpe-bypass <interface> [del]", |
| 1198 | }; |
| 1199 | /* *INDENT-ON* */ |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 1200 | |
Hongjun Ni | e1bf572 | 2017-08-03 20:34:45 +0800 | [diff] [blame] | 1201 | /* *INDENT-OFF* */ |
| 1202 | VNET_FEATURE_INIT (ip4_vxlan_gpe_bypass, static) = |
| 1203 | { |
| 1204 | .arc_name = "ip4-unicast", |
| 1205 | .node_name = "ip4-vxlan-gpe-bypass", |
| 1206 | .runs_before = VNET_FEATURES ("ip4-lookup"), |
| 1207 | }; |
| 1208 | |
| 1209 | VNET_FEATURE_INIT (ip6_vxlan_gpe_bypass, static) = |
| 1210 | { |
| 1211 | .arc_name = "ip6-unicast", |
| 1212 | .node_name = "ip6-vxlan-gpe-bypass", |
| 1213 | .runs_before = VNET_FEATURES ("ip6-lookup"), |
| 1214 | }; |
| 1215 | /* *INDENT-ON* */ |
| 1216 | |
Keith Burns (alagalah) | d46cca1 | 2016-08-25 11:21:39 -0700 | [diff] [blame] | 1217 | /** |
| 1218 | * @brief Feature init function for VXLAN GPE |
| 1219 | * |
| 1220 | * @param *vm |
| 1221 | * |
| 1222 | * @return error |
| 1223 | * |
| 1224 | */ |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 1225 | clib_error_t *vxlan_gpe_init (vlib_main_t *vm) |
| 1226 | { |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 1227 | vxlan_gpe_main_t *ngm = &vxlan_gpe_main; |
Hongjun Ni | cccd1bf | 2016-06-07 18:43:05 +0800 | [diff] [blame] | 1228 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 1229 | ngm->vnet_main = vnet_get_main(); |
| 1230 | ngm->vlib_main = vm; |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 1231 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 1232 | ngm->vxlan4_gpe_tunnel_by_key |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 1233 | = hash_create_mem (0, sizeof(vxlan4_gpe_tunnel_key_t), sizeof (uword)); |
| 1234 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 1235 | ngm->vxlan6_gpe_tunnel_by_key |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 1236 | = hash_create_mem (0, sizeof(vxlan6_gpe_tunnel_key_t), sizeof (uword)); |
| 1237 | |
| 1238 | |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 1239 | ngm->mcast_shared = hash_create_mem(0, |
| 1240 | sizeof(ip46_address_t), |
| 1241 | sizeof(mcast_shared_t)); |
| 1242 | |
| 1243 | udp_register_dst_port (vm, UDP_DST_PORT_VXLAN_GPE, |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 1244 | vxlan4_gpe_input_node.index, 1 /* is_ip4 */); |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 1245 | udp_register_dst_port (vm, UDP_DST_PORT_VXLAN6_GPE, |
Hongjun Ni | df921cc | 2016-05-25 01:16:19 +0800 | [diff] [blame] | 1246 | vxlan6_gpe_input_node.index, 0 /* is_ip4 */); |
Vengada Govindan | 6d403a0 | 2016-10-12 05:54:09 -0700 | [diff] [blame] | 1247 | |
| 1248 | /* Register the list of standard decap protocols supported */ |
| 1249 | vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_IP4, |
| 1250 | VXLAN_GPE_INPUT_NEXT_IP4_INPUT); |
| 1251 | vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_IP6, |
| 1252 | VXLAN_GPE_INPUT_NEXT_IP6_INPUT); |
| 1253 | vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_ETHERNET, |
| 1254 | VXLAN_GPE_INPUT_NEXT_ETHERNET_INPUT); |
Hongjun Ni | 8a0a0ae | 2017-05-27 20:23:09 +0800 | [diff] [blame] | 1255 | |
| 1256 | fib_node_register_type(FIB_NODE_TYPE_VXLAN_GPE_TUNNEL, &vxlan_gpe_vft); |
| 1257 | |
Keith Burns (alagalah) | 94b1442 | 2016-05-05 18:16:50 -0700 | [diff] [blame] | 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | VLIB_INIT_FUNCTION(vxlan_gpe_init); |
| 1262 | |