Line data Source code
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 : */
20 : #include <lisp/lisp-gpe/lisp_gpe_tenant.h>
21 : #include <lisp/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 1 : lisp_gpe_sub_interface_db_find (const ip_address_t * lrloc, u32 vni)
49 : {
50 : uword *p;
51 :
52 : lisp_gpe_sub_interface_key_t key;
53 :
54 1 : clib_memset (&key, 0, sizeof (key));
55 1 : ip_address_copy (&key.local_rloc, lrloc);
56 1 : key.vni = vni;
57 1 : p = hash_get_mem (lisp_gpe_sub_interfaces, &key);
58 :
59 1 : if (NULL == p)
60 1 : return (INDEX_INVALID);
61 : else
62 0 : return (p[0]);
63 : }
64 :
65 : static void
66 1 : lisp_gpe_sub_interface_db_insert (const lisp_gpe_sub_interface_t * l3s)
67 : {
68 2 : hash_set_mem (lisp_gpe_sub_interfaces,
69 : l3s->key, l3s - lisp_gpe_sub_interface_pool);
70 2 : hash_set_mem (lisp_gpe_sub_interfaces_sw_if_index,
71 : l3s->key, l3s->sw_if_index);
72 1 : }
73 :
74 : static void
75 1 : lisp_gpe_sub_interface_db_remove (const lisp_gpe_sub_interface_t * l3s)
76 : {
77 2 : hash_unset_mem (lisp_gpe_sub_interfaces, l3s->key);
78 2 : hash_unset_mem (lisp_gpe_sub_interfaces_sw_if_index, l3s->key);
79 1 : }
80 :
81 : lisp_gpe_sub_interface_t *
82 2 : lisp_gpe_sub_interface_get_i (index_t l3si)
83 : {
84 2 : return (pool_elt_at_index (lisp_gpe_sub_interface_pool, l3si));
85 : }
86 :
87 : static void
88 1 : lisp_gpe_sub_interface_set_table (u32 sw_if_index, u32 table_id)
89 : {
90 : fib_node_index_t fib_index;
91 :
92 1 : fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, table_id,
93 : FIB_SOURCE_LISP);
94 1 : ASSERT (FIB_NODE_INDEX_INVALID != fib_index);
95 :
96 1 : ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
97 :
98 1 : fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, table_id,
99 : FIB_SOURCE_LISP);
100 1 : ASSERT (FIB_NODE_INDEX_INVALID != fib_index);
101 :
102 1 : ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
103 1 : }
104 :
105 : static void
106 1 : lisp_gpe_sub_interface_unset_table (u32 sw_if_index, u32 table_id)
107 : {
108 1 : fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index],
109 : FIB_PROTOCOL_IP4, FIB_SOURCE_LISP);
110 1 : ip4_main.fib_index_by_sw_if_index[sw_if_index] = 0;
111 1 : ip4_sw_interface_enable_disable (sw_if_index, 0);
112 :
113 1 : fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index],
114 : FIB_PROTOCOL_IP6, FIB_SOURCE_LISP);
115 1 : ip6_main.fib_index_by_sw_if_index[sw_if_index] = 0;
116 1 : ip6_sw_interface_enable_disable (sw_if_index, 0);
117 1 : }
118 :
119 : index_t
120 1 : lisp_gpe_sub_interface_find_or_create_and_lock (const ip_address_t * lrloc,
121 : u32 overlay_table_id, u32 vni)
122 : {
123 : lisp_gpe_sub_interface_t *l3s;
124 : index_t l3si;
125 :
126 1 : l3si = lisp_gpe_sub_interface_db_find (lrloc, vni);
127 :
128 1 : if (INDEX_INVALID == l3si)
129 : {
130 : u32 main_sw_if_index, sub_sw_if_index;
131 :
132 : /*
133 : * find the main interface from the VNI
134 : */
135 : main_sw_if_index =
136 1 : lisp_gpe_tenant_l3_iface_add_or_lock (vni, overlay_table_id,
137 : 1 /* with_default_route */ );
138 :
139 1 : vnet_sw_interface_t sub_itf_template = {
140 : .type = VNET_SW_INTERFACE_TYPE_SUB,
141 : .flood_class = VNET_FLOOD_CLASS_NORMAL,
142 : .sup_sw_if_index = main_sw_if_index,
143 1 : .sub.id = lisp_gpe_sub_interface_id++,
144 : };
145 :
146 1 : if (NULL != vnet_create_sw_interface (vnet_get_main (),
147 : &sub_itf_template,
148 : &sub_sw_if_index))
149 0 : return (INDEX_INVALID);
150 :
151 1 : pool_get (lisp_gpe_sub_interface_pool, l3s);
152 1 : clib_memset (l3s, 0, sizeof (*l3s));
153 1 : l3s->key = clib_mem_alloc (sizeof (*l3s->key));
154 1 : clib_memset (l3s->key, 0, sizeof (*l3s->key));
155 :
156 1 : ip_address_copy (&l3s->key->local_rloc, lrloc);
157 1 : l3s->key->vni = vni;
158 1 : l3s->main_sw_if_index = main_sw_if_index;
159 1 : l3s->sw_if_index = sub_sw_if_index;
160 1 : l3s->eid_table_id = overlay_table_id;
161 :
162 1 : l3si = (l3s - lisp_gpe_sub_interface_pool);
163 :
164 : // FIXME. enable When we get an adj
165 1 : ip6_sw_interface_enable_disable (l3s->sw_if_index, 1);
166 1 : ip4_sw_interface_enable_disable (l3s->sw_if_index, 1);
167 :
168 1 : vnet_sw_interface_set_flags (vnet_get_main (),
169 1 : l3s->sw_if_index,
170 : VNET_SW_INTERFACE_FLAG_ADMIN_UP);
171 1 : vnet_set_interface_l3_output_node (vlib_get_main (), l3s->sw_if_index,
172 : (u8 *) "lisp-tunnel-output");
173 :
174 1 : lisp_gpe_sub_interface_db_insert (l3s);
175 : }
176 : else
177 : {
178 0 : l3s = lisp_gpe_sub_interface_get_i (l3si);
179 0 : l3s->eid_table_id = overlay_table_id;
180 : }
181 :
182 1 : lisp_gpe_sub_interface_set_table (l3s->sw_if_index, l3s->eid_table_id);
183 1 : l3s->locks++;
184 :
185 1 : return (l3si);
186 : }
187 :
188 : void
189 1 : lisp_gpe_sub_interface_unlock (index_t l3si)
190 : {
191 : lisp_gpe_sub_interface_t *l3s;
192 :
193 1 : l3s = lisp_gpe_sub_interface_get_i (l3si);
194 :
195 1 : ASSERT (0 != l3s->locks);
196 1 : l3s->locks--;
197 :
198 1 : if (0 == l3s->locks)
199 : {
200 1 : lisp_gpe_sub_interface_unset_table (l3s->sw_if_index,
201 : l3s->eid_table_id);
202 :
203 1 : lisp_gpe_tenant_l3_iface_unlock (l3s->key->vni);
204 1 : vnet_sw_interface_set_flags (vnet_get_main (), l3s->sw_if_index, 0);
205 1 : vnet_reset_interface_l3_output_node (vlib_get_main (), l3s->sw_if_index);
206 1 : vnet_delete_sub_interface (l3s->sw_if_index);
207 :
208 1 : lisp_gpe_sub_interface_db_remove (l3s);
209 :
210 1 : clib_mem_free (l3s->key);
211 1 : pool_put (lisp_gpe_sub_interface_pool, l3s);
212 : }
213 1 : }
214 :
215 : const lisp_gpe_sub_interface_t *
216 1 : lisp_gpe_sub_interface_get (index_t l3si)
217 : {
218 1 : return (lisp_gpe_sub_interface_get_i (l3si));
219 : }
220 :
221 : u8 *
222 0 : format_lisp_gpe_sub_interface (u8 * s, va_list * ap)
223 : {
224 0 : lisp_gpe_sub_interface_t *l3s = va_arg (*ap, lisp_gpe_sub_interface_t *);
225 0 : vnet_main_t *vnm = vnet_get_main ();
226 :
227 0 : s = format (s, "%-16U",
228 : format_vnet_sw_interface_name,
229 : vnm, vnet_get_sw_interface (vnm, l3s->sw_if_index));
230 0 : s = format (s, "%=8d", l3s->key->vni);
231 0 : s = format (s, "%=15d", l3s->sw_if_index);
232 0 : s = format (s, "%U", format_ip_address, &l3s->key->local_rloc);
233 :
234 0 : return (s);
235 : }
236 :
237 : /** CLI command to show LISP-GPE interfaces. */
238 : static clib_error_t *
239 0 : lisp_gpe_sub_interface_show (vlib_main_t * vm,
240 : unformat_input_t * input,
241 : vlib_cli_command_t * cmd)
242 : {
243 : lisp_gpe_sub_interface_t *l3s;
244 :
245 0 : vlib_cli_output (vm, "%-16s%=8s%=15s%s", "Name", "VNI", "sw_if_index",
246 : "local RLOC");
247 :
248 : /* *INDENT-OFF* */
249 0 : pool_foreach (l3s, lisp_gpe_sub_interface_pool)
250 : {
251 0 : vlib_cli_output (vm, "%U", format_lisp_gpe_sub_interface, l3s);
252 : }
253 : /* *INDENT-ON* */
254 :
255 0 : return 0;
256 : }
257 :
258 : /* *INDENT-OFF* */
259 123991 : VLIB_CLI_COMMAND (lisp_gpe_sub_interface_command) = {
260 : .path = "show gpe sub-interface",
261 : .short_help = "show gpe sub-interface",
262 : .function = lisp_gpe_sub_interface_show,
263 : };
264 : /* *INDENT-ON* */
265 :
266 : static clib_error_t *
267 575 : lisp_gpe_sub_interface_module_init (vlib_main_t * vm)
268 : {
269 575 : lisp_gpe_sub_interfaces =
270 575 : hash_create_mem (0,
271 : sizeof (lisp_gpe_sub_interface_key_t), sizeof (uword));
272 575 : lisp_gpe_sub_interfaces_sw_if_index =
273 575 : hash_create_mem (0,
274 : sizeof (lisp_gpe_sub_interface_key_t), sizeof (uword));
275 :
276 575 : return (NULL);
277 : }
278 :
279 2303 : VLIB_INIT_FUNCTION (lisp_gpe_sub_interface_module_init);
280 :
281 : /*
282 : * fd.io coding-style-patch-verification: ON
283 : *
284 : * Local Variables:
285 : * eval: (c-set-style "gnu")
286 : * End:
287 : */
|