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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/lisp-gpe/lisp_gpe_tenant.h>
19 * The pool of all tenants
21 static lisp_gpe_tenant_t *lisp_gpe_tenant_pool;
24 * The hash table of all tenants: key:{VNI}.
26 uword *lisp_gpe_tenant_db;
28 static lisp_gpe_tenant_t *
29 lisp_gpe_tenant_find (u32 vni)
33 p = hash_get (lisp_gpe_tenant_db, vni);
38 return (pool_elt_at_index (lisp_gpe_tenant_pool, p[0]));
41 static lisp_gpe_tenant_t *
42 lisp_gpe_tenant_find_or_create_i (u32 vni)
44 lisp_gpe_tenant_t *lt;
46 lt = lisp_gpe_tenant_find (vni);
50 pool_get (lisp_gpe_tenant_pool, lt);
51 memset (lt, 0, sizeof (*lt));
57 hash_set (lisp_gpe_tenant_db, vni, lt - lisp_gpe_tenant_pool);
64 * @brief Find or create a tenant for the given VNI
67 lisp_gpe_tenant_find_or_create (u32 vni)
69 lisp_gpe_tenant_t *lt;
71 lt = lisp_gpe_tenant_find (vni);
75 lt = lisp_gpe_tenant_find_or_create_i (vni);
78 return (lt - lisp_gpe_tenant_pool);
82 * @brief If there are no more locks/users of te tenant, then delete it
85 lisp_gpe_tenant_delete_if_empty (lisp_gpe_tenant_t * lt)
89 for (i = 0; i < LISP_GPE_TENANT_LOCK_NUM; i++)
95 hash_unset (lisp_gpe_tenant_db, lt->lt_vni);
96 pool_put (lisp_gpe_tenant_pool, lt);
100 * @brief Add/create and lock a new or find and lock the existing L3
101 * interface for the tenant
103 * @paran vni The tenant's VNI
104 * @param table_id the Tenant's L3 table ID.
105 * @param with_default_route Install default route for the interface
107 * @return the SW IF index of the L3 interface
110 lisp_gpe_tenant_l3_iface_add_or_lock (u32 vni, u32 table_id,
111 u8 with_default_route)
113 lisp_gpe_tenant_t *lt;
115 lt = lisp_gpe_tenant_find_or_create_i (vni);
117 if (~0 == lt->lt_table_id)
118 lt->lt_table_id = table_id;
120 ASSERT (lt->lt_table_id == table_id);
122 if (0 == lt->lt_locks[LISP_GPE_TENANT_LOCK_L3_IFACE])
124 /* create the l3 interface since there are currently no users of it */
125 lt->lt_l3_sw_if_index =
126 lisp_gpe_add_l3_iface (&lisp_gpe_main, vni, table_id,
130 lt->lt_locks[LISP_GPE_TENANT_LOCK_L3_IFACE]++;
132 return (lt->lt_l3_sw_if_index);
136 * @brief Release the lock held on the tenant's L3 interface
139 lisp_gpe_tenant_l3_iface_unlock (u32 vni)
141 lisp_gpe_tenant_t *lt;
143 lt = lisp_gpe_tenant_find (vni);
147 clib_warning ("No tenant for VNI %d", vni);
151 if (0 == lt->lt_locks[LISP_GPE_TENANT_LOCK_L3_IFACE])
153 clib_warning ("No L3 interface for tenant VNI %d", vni);
157 lt->lt_locks[LISP_GPE_TENANT_LOCK_L3_IFACE]--;
159 if (0 == lt->lt_locks[LISP_GPE_TENANT_LOCK_L3_IFACE])
161 /* the last user has gone, so delete the l3 interface */
162 lisp_gpe_del_l3_iface (&lisp_gpe_main, vni, lt->lt_table_id);
166 * If there are no more locks on any tenant managed resource, then
167 * this tenant is toast.
169 lisp_gpe_tenant_delete_if_empty (lt);
173 * @brief Add/create and lock a new or find and lock the existing L2
174 * interface for the tenant
176 * @paran vni The tenant's VNI
177 * @param table_id the Tenant's L2 Bridge Domain ID.
179 * @return the SW IF index of the L2 interface
182 lisp_gpe_tenant_l2_iface_add_or_lock (u32 vni, u32 bd_id)
184 lisp_gpe_tenant_t *lt;
186 lt = lisp_gpe_tenant_find_or_create_i (vni);
190 clib_warning ("No tenant for VNI %d", vni);
194 if (~0 == lt->lt_bd_id)
195 lt->lt_bd_id = bd_id;
197 ASSERT (lt->lt_bd_id == bd_id);
199 if (0 == lt->lt_locks[LISP_GPE_TENANT_LOCK_L2_IFACE])
201 /* create the l2 interface since there are currently no users of it */
202 lt->lt_l2_sw_if_index =
203 lisp_gpe_add_l2_iface (&lisp_gpe_main, vni, bd_id);
206 lt->lt_locks[LISP_GPE_TENANT_LOCK_L2_IFACE]++;
208 return (lt->lt_l2_sw_if_index);
212 * @brief Release the lock held on the tenant's L3 interface
215 lisp_gpe_tenant_l2_iface_unlock (u32 vni)
217 lisp_gpe_tenant_t *lt;
219 lt = lisp_gpe_tenant_find (vni);
223 clib_warning ("No tenant for VNI %d", vni);
227 if (0 == lt->lt_locks[LISP_GPE_TENANT_LOCK_L2_IFACE])
229 clib_warning ("No L2 interface for tenant VNI %d", vni);
233 lt->lt_locks[LISP_GPE_TENANT_LOCK_L2_IFACE]--;
235 if (0 == lt->lt_locks[LISP_GPE_TENANT_LOCK_L2_IFACE])
237 /* the last user has gone, so delete the l2 interface */
238 lisp_gpe_del_l2_iface (&lisp_gpe_main, vni, lt->lt_bd_id);
242 * If there are no more locks on any tenant managed resource, then
243 * this tenant is toast.
245 lisp_gpe_tenant_delete_if_empty (lt);
249 * @brief get a const pointer to the tenant object
251 const lisp_gpe_tenant_t *
252 lisp_gpe_tenant_get (u32 index)
254 return (pool_elt_at_index (lisp_gpe_tenant_pool, index));
258 * @brief Flush/delete ALL the tenants
261 lisp_gpe_tenant_flush (void)
263 lisp_gpe_tenant_t *lt;
266 pool_foreach(lt, lisp_gpe_tenant_pool,
268 lisp_gpe_tenant_l2_iface_unlock(lt->lt_vni);
269 lisp_gpe_tenant_l3_iface_unlock(lt->lt_vni);
275 * @brif Show/display one tenant
278 format_lisp_gpe_tenant (u8 * s, va_list * ap)
280 const lisp_gpe_tenant_t *lt = va_arg (*ap, lisp_gpe_tenant_t *);
282 s = format (s, "VNI:%d ", lt->lt_vni);
284 if (lt->lt_table_id != ~0)
286 s = format (s, "VRF:%d ", lt->lt_table_id);
287 s = format (s, "L3-SW-IF:%d ", lt->lt_l3_sw_if_index);
290 if (lt->lt_bd_id != ~0)
292 s = format (s, "BD-ID:%d ", lt->lt_bd_id);
293 s = format (s, "L2-SW-IF:%d ", lt->lt_l2_sw_if_index);
300 * @brief CLI command to show LISP-GPE tenant.
302 static clib_error_t *
303 lisp_gpe_tenant_show (vlib_main_t * vm,
304 unformat_input_t * input, vlib_cli_command_t * cmd)
306 lisp_gpe_tenant_t *lt;
309 pool_foreach (lt, lisp_gpe_tenant_pool,
311 vlib_cli_output (vm, "%U", format_lisp_gpe_tenant, lt);
319 VLIB_CLI_COMMAND (lisp_gpe_tenant_command) = {
320 .path = "show gpe tenant",
321 .short_help = "show gpe tenant",
322 .function = lisp_gpe_tenant_show,
328 * fd.io coding-style-patch-verification: ON
331 * eval: (c-set-style "gnu")