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.
17 * @brief LISP sub-interfaces.
20 #include <vnet/lisp-gpe/lisp_gpe_tenant.h>
21 #include <vnet/lisp-gpe/lisp_gpe_sub_interface.h>
22 #include <vnet/fib/fib_table.h>
23 #include <vnet/interface.h>
26 * @brief Pool of all l3-sub-interfaces
28 static lisp_gpe_sub_interface_t *lisp_gpe_sub_interface_pool;
31 * A DB of all LISP L3 sub-interfaces. The key is:{VNI,l-RLOC}
33 static uword *lisp_gpe_sub_interfaces;
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.
39 uword *lisp_gpe_sub_interfaces_sw_if_index;
42 * The next available sub-interface ID. FIXME
44 static u32 lisp_gpe_sub_interface_id;
48 lisp_gpe_sub_interface_db_find (const ip_address_t * lrloc, u32 vni)
52 lisp_gpe_sub_interface_key_t key;
54 memset (&key, 0, sizeof (key));
55 ip_address_copy (&key.local_rloc, lrloc);
56 key.vni = clib_host_to_net_u32 (vni);
57 p = hash_get_mem (lisp_gpe_sub_interfaces, &key);
60 return (INDEX_INVALID);
66 lisp_gpe_sub_interface_db_insert (const lisp_gpe_sub_interface_t * l3s)
68 hash_set_mem (lisp_gpe_sub_interfaces,
69 l3s->key, l3s - lisp_gpe_sub_interface_pool);
70 hash_set_mem (lisp_gpe_sub_interfaces_sw_if_index,
71 l3s->key, l3s->sw_if_index);
75 lisp_gpe_sub_interface_db_remove (const lisp_gpe_sub_interface_t * l3s)
77 hash_unset_mem (lisp_gpe_sub_interfaces, l3s->key);
78 hash_unset_mem (lisp_gpe_sub_interfaces_sw_if_index, l3s->key);
81 lisp_gpe_sub_interface_t *
82 lisp_gpe_sub_interface_get_i (index_t l3si)
84 return (pool_elt_at_index (lisp_gpe_sub_interface_pool, l3si));
88 lisp_gpe_sub_interface_set_table (u32 sw_if_index, u32 table_id)
90 fib_node_index_t fib_index;
92 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, table_id);
93 ASSERT (FIB_NODE_INDEX_INVALID != fib_index);
95 vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
96 ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
98 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, table_id);
99 ASSERT (FIB_NODE_INDEX_INVALID != fib_index);
101 vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
102 ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
106 lisp_gpe_sub_interface_unset_table (u32 sw_if_index, u32 table_id)
108 ip4_main.fib_index_by_sw_if_index[sw_if_index] = 0;
109 ip4_sw_interface_enable_disable (sw_if_index, 0);
111 ip6_main.fib_index_by_sw_if_index[sw_if_index] = 0;
112 ip6_sw_interface_enable_disable (sw_if_index, 0);
116 lisp_gpe_sub_interface_find_or_create_and_lock (const ip_address_t * lrloc,
117 u32 overlay_table_id, u32 vni)
119 lisp_gpe_sub_interface_t *l3s;
122 l3si = lisp_gpe_sub_interface_db_find (lrloc, clib_host_to_net_u32 (vni));
124 if (INDEX_INVALID == l3si)
126 u32 main_sw_if_index, sub_sw_if_index;
129 * find the main interface from the VNI
132 lisp_gpe_tenant_l3_iface_add_or_lock (vni, overlay_table_id);
134 vnet_sw_interface_t sub_itf_template = {
135 .type = VNET_SW_INTERFACE_TYPE_SUB,
136 .flood_class = VNET_FLOOD_CLASS_NORMAL,
137 .sup_sw_if_index = main_sw_if_index,
138 .sub.id = lisp_gpe_sub_interface_id++,
141 if (NULL != vnet_create_sw_interface (vnet_get_main (),
144 return (INDEX_INVALID);
146 pool_get (lisp_gpe_sub_interface_pool, l3s);
147 memset (l3s, 0, sizeof (*l3s));
148 l3s->key = clib_mem_alloc (sizeof (*l3s->key));
149 memset (l3s->key, 0, sizeof (*l3s->key));
151 l3s->key->local_rloc = *lrloc;
152 l3s->key->vni = clib_host_to_net_u32 (vni);
153 l3s->main_sw_if_index = main_sw_if_index;
154 l3s->sw_if_index = sub_sw_if_index;
155 l3s->eid_table_id = overlay_table_id;
157 l3si = (l3s - lisp_gpe_sub_interface_pool);
159 // FIXME. enable When we get an adj
160 ip6_sw_interface_enable_disable (l3s->sw_if_index, 1);
161 ip4_sw_interface_enable_disable (l3s->sw_if_index, 1);
163 vnet_sw_interface_set_flags (vnet_get_main (),
165 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
167 lisp_gpe_sub_interface_db_insert (l3s);
171 l3s = lisp_gpe_sub_interface_get_i (l3si);
172 l3s->eid_table_id = overlay_table_id;
175 lisp_gpe_sub_interface_set_table (l3s->sw_if_index, l3s->eid_table_id);
182 lisp_gpe_sub_interface_unlock (index_t l3si)
184 lisp_gpe_sub_interface_t *l3s;
186 l3s = lisp_gpe_sub_interface_get_i (l3si);
192 lisp_gpe_sub_interface_unset_table (l3s->sw_if_index,
195 lisp_gpe_tenant_l3_iface_unlock (clib_net_to_host_u32 (l3s->key->vni));
196 vnet_sw_interface_set_flags (vnet_get_main (), l3s->sw_if_index, 0);
197 vnet_delete_sub_interface (l3s->sw_if_index);
199 lisp_gpe_sub_interface_db_remove (l3s);
201 clib_mem_free (l3s->key);
202 pool_put (lisp_gpe_sub_interface_pool, l3s);
206 const lisp_gpe_sub_interface_t *
207 lisp_gpe_sub_interface_get (index_t l3si)
209 return (lisp_gpe_sub_interface_get_i (l3si));
213 format_lisp_gpe_sub_interface (u8 * s, va_list ap)
215 lisp_gpe_sub_interface_t *l3s = va_arg (ap, lisp_gpe_sub_interface_t *);
216 vnet_main_t *vnm = vnet_get_main ();
218 s = format (s, "%=16U",
219 format_vnet_sw_interface_name,
220 vnm, vnet_get_sw_interface (vnm, l3s->sw_if_index));
221 s = format (s, "%=10d", clib_net_to_host_u32 (l3s->key->vni));
222 s = format (s, "%=12d", l3s->sw_if_index);
223 s = format (s, "%U", format_ip_address, &l3s->key->local_rloc);
228 /** CLI command to show LISP-GPE interfaces. */
229 static clib_error_t *
230 lisp_gpe_sub_interface_show (vlib_main_t * vm,
231 unformat_input_t * input,
232 vlib_cli_command_t * cmd)
234 lisp_gpe_sub_interface_t *l3s;
236 vlib_cli_output (vm, "%=16s%=10s%=12s%s", "Name", "VNI", "SW IF Index",
240 pool_foreach (l3s, lisp_gpe_sub_interface_pool,
242 vlib_cli_output (vm, "%U", format_lisp_gpe_sub_interface, l3s);
250 VLIB_CLI_COMMAND (lisp_gpe_sub_interface_command) = {
251 .path = "show gpe sub-interface",
252 .short_help = "show gpe sub-interface",
253 .function = lisp_gpe_sub_interface_show,
257 static clib_error_t *
258 lisp_gpe_sub_interface_module_init (vlib_main_t * vm)
260 lisp_gpe_sub_interfaces =
262 sizeof (lisp_gpe_sub_interface_key_t), sizeof (uword));
263 lisp_gpe_sub_interfaces_sw_if_index =
265 sizeof (lisp_gpe_sub_interface_key_t), sizeof (uword));
270 VLIB_INIT_FUNCTION (lisp_gpe_sub_interface_module_init);
273 * fd.io coding-style-patch-verification: ON
276 * eval: (c-set-style "gnu")