Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 | /** |
| 16 | * @file |
| 17 | * @brief LISP sub-interfaces. |
| 18 | * |
| 19 | */ |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 20 | #include <vnet/lisp-gpe/lisp_gpe_tenant.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 21 | #include <vnet/lisp-gpe/lisp_gpe_sub_interface.h> |
| 22 | #include <vnet/fib/fib_table.h> |
| 23 | #include <vnet/interface.h> |
| 24 | |
| 25 | /** |
| 26 | * @brief Pool of all l3-sub-interfaces |
| 27 | */ |
| 28 | static lisp_gpe_sub_interface_t *lisp_gpe_sub_interface_pool; |
| 29 | |
| 30 | /** |
| 31 | * A DB of all LISP L3 sub-interfaces. The key is:{VNI,l-RLOC} |
| 32 | */ |
| 33 | static uword *lisp_gpe_sub_interfaces; |
| 34 | |
| 35 | /** |
| 36 | * A DB of all VNET L3 sub-interfaces. The key is:{VNI,l-RLOC} |
| 37 | * Used in the data-plane for interface lookup on decap. |
| 38 | */ |
| 39 | uword *lisp_gpe_sub_interfaces_sw_if_index; |
| 40 | |
| 41 | /** |
| 42 | * The next available sub-interface ID. FIXME |
| 43 | */ |
| 44 | static u32 lisp_gpe_sub_interface_id; |
| 45 | |
| 46 | |
| 47 | static index_t |
| 48 | lisp_gpe_sub_interface_db_find (const ip_address_t * lrloc, u32 vni) |
| 49 | { |
| 50 | uword *p; |
| 51 | |
Filip Tehlar | 6786f1b | 2017-03-07 10:23:01 +0100 | [diff] [blame] | 52 | lisp_gpe_sub_interface_key_t key; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 53 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame^] | 54 | clib_memset (&key, 0, sizeof (key)); |
Filip Tehlar | 6786f1b | 2017-03-07 10:23:01 +0100 | [diff] [blame] | 55 | ip_address_copy (&key.local_rloc, lrloc); |
Florin Coras | af8c8e5 | 2017-08-17 15:57:20 -0700 | [diff] [blame] | 56 | key.vni = vni; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 57 | p = hash_get_mem (lisp_gpe_sub_interfaces, &key); |
| 58 | |
| 59 | if (NULL == p) |
| 60 | return (INDEX_INVALID); |
| 61 | else |
| 62 | return (p[0]); |
| 63 | } |
| 64 | |
| 65 | static void |
| 66 | lisp_gpe_sub_interface_db_insert (const lisp_gpe_sub_interface_t * l3s) |
| 67 | { |
| 68 | hash_set_mem (lisp_gpe_sub_interfaces, |
Filip Tehlar | 6786f1b | 2017-03-07 10:23:01 +0100 | [diff] [blame] | 69 | l3s->key, l3s - lisp_gpe_sub_interface_pool); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 70 | hash_set_mem (lisp_gpe_sub_interfaces_sw_if_index, |
Filip Tehlar | 6786f1b | 2017-03-07 10:23:01 +0100 | [diff] [blame] | 71 | l3s->key, l3s->sw_if_index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static void |
| 75 | lisp_gpe_sub_interface_db_remove (const lisp_gpe_sub_interface_t * l3s) |
| 76 | { |
Filip Tehlar | 6786f1b | 2017-03-07 10:23:01 +0100 | [diff] [blame] | 77 | hash_unset_mem (lisp_gpe_sub_interfaces, l3s->key); |
| 78 | hash_unset_mem (lisp_gpe_sub_interfaces_sw_if_index, l3s->key); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | lisp_gpe_sub_interface_t * |
| 82 | lisp_gpe_sub_interface_get_i (index_t l3si) |
| 83 | { |
| 84 | return (pool_elt_at_index (lisp_gpe_sub_interface_pool, l3si)); |
| 85 | } |
| 86 | |
| 87 | static void |
| 88 | lisp_gpe_sub_interface_set_table (u32 sw_if_index, u32 table_id) |
| 89 | { |
| 90 | fib_node_index_t fib_index; |
| 91 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 92 | fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, table_id, |
| 93 | FIB_SOURCE_LISP); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 94 | ASSERT (FIB_NODE_INDEX_INVALID != fib_index); |
| 95 | |
| 96 | vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index); |
| 97 | ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 98 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 99 | fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, table_id, |
| 100 | FIB_SOURCE_LISP); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 101 | ASSERT (FIB_NODE_INDEX_INVALID != fib_index); |
| 102 | |
| 103 | vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index); |
| 104 | ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static void |
| 108 | lisp_gpe_sub_interface_unset_table (u32 sw_if_index, u32 table_id) |
| 109 | { |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 110 | fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index], |
| 111 | FIB_PROTOCOL_IP4, FIB_SOURCE_LISP); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 112 | ip4_main.fib_index_by_sw_if_index[sw_if_index] = 0; |
| 113 | ip4_sw_interface_enable_disable (sw_if_index, 0); |
| 114 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 115 | fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index], |
| 116 | FIB_PROTOCOL_IP6, FIB_SOURCE_LISP); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 117 | ip6_main.fib_index_by_sw_if_index[sw_if_index] = 0; |
| 118 | ip6_sw_interface_enable_disable (sw_if_index, 0); |
| 119 | } |
| 120 | |
| 121 | index_t |
| 122 | lisp_gpe_sub_interface_find_or_create_and_lock (const ip_address_t * lrloc, |
| 123 | u32 overlay_table_id, u32 vni) |
| 124 | { |
| 125 | lisp_gpe_sub_interface_t *l3s; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 126 | index_t l3si; |
| 127 | |
Florin Coras | af8c8e5 | 2017-08-17 15:57:20 -0700 | [diff] [blame] | 128 | l3si = lisp_gpe_sub_interface_db_find (lrloc, vni); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 129 | |
| 130 | if (INDEX_INVALID == l3si) |
| 131 | { |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 132 | u32 main_sw_if_index, sub_sw_if_index; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 133 | |
| 134 | /* |
| 135 | * find the main interface from the VNI |
| 136 | */ |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 137 | main_sw_if_index = |
Filip Tehlar | 0a8840d | 2017-10-16 05:48:23 -0700 | [diff] [blame] | 138 | lisp_gpe_tenant_l3_iface_add_or_lock (vni, overlay_table_id, |
| 139 | 1 /* with_default_route */ ); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 140 | |
| 141 | vnet_sw_interface_t sub_itf_template = { |
| 142 | .type = VNET_SW_INTERFACE_TYPE_SUB, |
Eyal Bari | c5b1360 | 2016-11-24 19:42:43 +0200 | [diff] [blame] | 143 | .flood_class = VNET_FLOOD_CLASS_NORMAL, |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 144 | .sup_sw_if_index = main_sw_if_index, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 145 | .sub.id = lisp_gpe_sub_interface_id++, |
| 146 | }; |
| 147 | |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 148 | if (NULL != vnet_create_sw_interface (vnet_get_main (), |
| 149 | &sub_itf_template, |
| 150 | &sub_sw_if_index)) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 151 | return (INDEX_INVALID); |
| 152 | |
| 153 | pool_get (lisp_gpe_sub_interface_pool, l3s); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame^] | 154 | clib_memset (l3s, 0, sizeof (*l3s)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 155 | l3s->key = clib_mem_alloc (sizeof (*l3s->key)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame^] | 156 | clib_memset (l3s->key, 0, sizeof (*l3s->key)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 157 | |
Florin Coras | af8c8e5 | 2017-08-17 15:57:20 -0700 | [diff] [blame] | 158 | ip_address_copy (&l3s->key->local_rloc, lrloc); |
| 159 | l3s->key->vni = vni; |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 160 | l3s->main_sw_if_index = main_sw_if_index; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 161 | l3s->sw_if_index = sub_sw_if_index; |
| 162 | l3s->eid_table_id = overlay_table_id; |
| 163 | |
| 164 | l3si = (l3s - lisp_gpe_sub_interface_pool); |
| 165 | |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 166 | // FIXME. enable When we get an adj |
| 167 | ip6_sw_interface_enable_disable (l3s->sw_if_index, 1); |
| 168 | ip4_sw_interface_enable_disable (l3s->sw_if_index, 1); |
| 169 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 170 | vnet_sw_interface_set_flags (vnet_get_main (), |
| 171 | l3s->sw_if_index, |
| 172 | VNET_SW_INTERFACE_FLAG_ADMIN_UP); |
| 173 | |
| 174 | lisp_gpe_sub_interface_db_insert (l3s); |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | l3s = lisp_gpe_sub_interface_get_i (l3si); |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 179 | l3s->eid_table_id = overlay_table_id; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 182 | lisp_gpe_sub_interface_set_table (l3s->sw_if_index, l3s->eid_table_id); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 183 | l3s->locks++; |
| 184 | |
| 185 | return (l3si); |
| 186 | } |
| 187 | |
| 188 | void |
| 189 | lisp_gpe_sub_interface_unlock (index_t l3si) |
| 190 | { |
| 191 | lisp_gpe_sub_interface_t *l3s; |
| 192 | |
| 193 | l3s = lisp_gpe_sub_interface_get_i (l3si); |
| 194 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 195 | ASSERT (0 != l3s->locks); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 196 | l3s->locks--; |
| 197 | |
| 198 | if (0 == l3s->locks) |
| 199 | { |
| 200 | lisp_gpe_sub_interface_unset_table (l3s->sw_if_index, |
| 201 | l3s->eid_table_id); |
| 202 | |
Filip Tehlar | 809bc74 | 2017-08-14 19:15:36 +0200 | [diff] [blame] | 203 | lisp_gpe_tenant_l3_iface_unlock (l3s->key->vni); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 204 | vnet_sw_interface_set_flags (vnet_get_main (), l3s->sw_if_index, 0); |
| 205 | vnet_delete_sub_interface (l3s->sw_if_index); |
| 206 | |
| 207 | lisp_gpe_sub_interface_db_remove (l3s); |
| 208 | |
| 209 | clib_mem_free (l3s->key); |
| 210 | pool_put (lisp_gpe_sub_interface_pool, l3s); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | const lisp_gpe_sub_interface_t * |
| 215 | lisp_gpe_sub_interface_get (index_t l3si) |
| 216 | { |
| 217 | return (lisp_gpe_sub_interface_get_i (l3si)); |
| 218 | } |
| 219 | |
| 220 | u8 * |
Christophe Fontaine | d3c008d | 2017-10-02 18:10:54 +0200 | [diff] [blame] | 221 | format_lisp_gpe_sub_interface (u8 * s, va_list * ap) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 222 | { |
Christophe Fontaine | d3c008d | 2017-10-02 18:10:54 +0200 | [diff] [blame] | 223 | lisp_gpe_sub_interface_t *l3s = va_arg (*ap, lisp_gpe_sub_interface_t *); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 224 | vnet_main_t *vnm = vnet_get_main (); |
| 225 | |
Florin Coras | af8c8e5 | 2017-08-17 15:57:20 -0700 | [diff] [blame] | 226 | s = format (s, "%-16U", |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 227 | format_vnet_sw_interface_name, |
| 228 | vnm, vnet_get_sw_interface (vnm, l3s->sw_if_index)); |
Florin Coras | af8c8e5 | 2017-08-17 15:57:20 -0700 | [diff] [blame] | 229 | s = format (s, "%=8d", l3s->key->vni); |
| 230 | s = format (s, "%=15d", l3s->sw_if_index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 231 | s = format (s, "%U", format_ip_address, &l3s->key->local_rloc); |
| 232 | |
| 233 | return (s); |
| 234 | } |
| 235 | |
| 236 | /** CLI command to show LISP-GPE interfaces. */ |
| 237 | static clib_error_t * |
| 238 | lisp_gpe_sub_interface_show (vlib_main_t * vm, |
| 239 | unformat_input_t * input, |
| 240 | vlib_cli_command_t * cmd) |
| 241 | { |
| 242 | lisp_gpe_sub_interface_t *l3s; |
| 243 | |
Florin Coras | af8c8e5 | 2017-08-17 15:57:20 -0700 | [diff] [blame] | 244 | vlib_cli_output (vm, "%-16s%=8s%=15s%s", "Name", "VNI", "sw_if_index", |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 245 | "local RLOC"); |
| 246 | |
| 247 | /* *INDENT-OFF* */ |
| 248 | pool_foreach (l3s, lisp_gpe_sub_interface_pool, |
| 249 | ({ |
| 250 | vlib_cli_output (vm, "%U", format_lisp_gpe_sub_interface, l3s); |
| 251 | })); |
| 252 | /* *INDENT-ON* */ |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | /* *INDENT-OFF* */ |
| 258 | VLIB_CLI_COMMAND (lisp_gpe_sub_interface_command) = { |
Filip Tehlar | 82786c4 | 2017-02-20 15:20:37 +0100 | [diff] [blame] | 259 | .path = "show gpe sub-interface", |
| 260 | .short_help = "show gpe sub-interface", |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 261 | .function = lisp_gpe_sub_interface_show, |
| 262 | }; |
| 263 | /* *INDENT-ON* */ |
| 264 | |
| 265 | static clib_error_t * |
| 266 | lisp_gpe_sub_interface_module_init (vlib_main_t * vm) |
| 267 | { |
| 268 | lisp_gpe_sub_interfaces = |
| 269 | hash_create_mem (0, |
| 270 | sizeof (lisp_gpe_sub_interface_key_t), sizeof (uword)); |
| 271 | lisp_gpe_sub_interfaces_sw_if_index = |
| 272 | hash_create_mem (0, |
| 273 | sizeof (lisp_gpe_sub_interface_key_t), sizeof (uword)); |
| 274 | |
| 275 | return (NULL); |
| 276 | } |
| 277 | |
| 278 | VLIB_INIT_FUNCTION (lisp_gpe_sub_interface_module_init); |
| 279 | |
| 280 | /* |
| 281 | * fd.io coding-style-patch-verification: ON |
| 282 | * |
| 283 | * Local Variables: |
| 284 | * eval: (c-set-style "gnu") |
| 285 | * End: |
| 286 | */ |