A Protocol Independent Hierarchical FIB (VPP-352)
[vpp.git] / vnet / vnet / lisp-gpe / lisp_gpe_sub_interface.c
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 <vnet/lisp-gpe/lisp_gpe_sub_interface.h>
21 #include <vnet/fib/fib_table.h>
22 #include <vnet/interface.h>
23
24 /**
25  * @brief Pool of all l3-sub-interfaces
26  */
27 static lisp_gpe_sub_interface_t *lisp_gpe_sub_interface_pool;
28
29 /**
30  * A DB of all LISP L3 sub-interfaces. The key is:{VNI,l-RLOC}
31  */
32 static uword *lisp_gpe_sub_interfaces;
33
34 /**
35  * A DB of all VNET L3 sub-interfaces. The key is:{VNI,l-RLOC}
36  * Used in the data-plane for interface lookup on decap.
37  */
38 uword *lisp_gpe_sub_interfaces_sw_if_index;
39
40 /**
41  * The next available sub-interface ID. FIXME
42  */
43 static u32 lisp_gpe_sub_interface_id;
44
45
46 static index_t
47 lisp_gpe_sub_interface_db_find (const ip_address_t * lrloc, u32 vni)
48 {
49   uword *p;
50
51   lisp_gpe_sub_interface_key_t key = {
52     .local_rloc = *lrloc,
53     .vni = clib_host_to_net_u32 (vni),
54   };
55
56   p = hash_get_mem (lisp_gpe_sub_interfaces, &key);
57
58   if (NULL == p)
59     return (INDEX_INVALID);
60   else
61     return (p[0]);
62 }
63
64 static void
65 lisp_gpe_sub_interface_db_insert (const lisp_gpe_sub_interface_t * l3s)
66 {
67   hash_set_mem (lisp_gpe_sub_interfaces,
68                 &l3s->key, l3s - lisp_gpe_sub_interface_pool);
69   hash_set_mem (lisp_gpe_sub_interfaces_sw_if_index,
70                 &l3s->key, l3s->sw_if_index);
71 }
72
73 static void
74 lisp_gpe_sub_interface_db_remove (const lisp_gpe_sub_interface_t * l3s)
75 {
76   hash_unset_mem (lisp_gpe_sub_interfaces, &l3s->key);
77   hash_unset_mem (lisp_gpe_sub_interfaces_sw_if_index, &l3s->key);
78 }
79
80 lisp_gpe_sub_interface_t *
81 lisp_gpe_sub_interface_get_i (index_t l3si)
82 {
83   return (pool_elt_at_index (lisp_gpe_sub_interface_pool, l3si));
84 }
85
86 static void
87 lisp_gpe_sub_interface_set_table (u32 sw_if_index, u32 table_id)
88 {
89   fib_node_index_t fib_index;
90
91   fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, table_id);
92   ASSERT (FIB_NODE_INDEX_INVALID != fib_index);
93
94   vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
95   ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
96   // FIXME. enable When we get an adj
97   ip4_sw_interface_enable_disable (sw_if_index, 1);
98
99   fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, table_id);
100   ASSERT (FIB_NODE_INDEX_INVALID != fib_index);
101
102   vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
103   ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
104   // FIXME. enable When we get an adj
105   ip6_sw_interface_enable_disable (sw_if_index, 1);
106 }
107
108 static void
109 lisp_gpe_sub_interface_unset_table (u32 sw_if_index, u32 table_id)
110 {
111   ip4_main.fib_index_by_sw_if_index[sw_if_index] = 0;
112   ip4_sw_interface_enable_disable (sw_if_index, 0);
113
114   ip6_main.fib_index_by_sw_if_index[sw_if_index] = 0;
115   ip6_sw_interface_enable_disable (sw_if_index, 0);
116 }
117
118 index_t
119 lisp_gpe_sub_interface_find_or_create_and_lock (const ip_address_t * lrloc,
120                                                 u32 overlay_table_id, u32 vni)
121 {
122   lisp_gpe_sub_interface_t *l3s;
123   lisp_gpe_main_t *lgm = &lisp_gpe_main;
124   index_t l3si;
125
126   l3si = lisp_gpe_sub_interface_db_find (lrloc, vni);
127
128   if (INDEX_INVALID == l3si)
129     {
130       vnet_hw_interface_t *hi;
131       clib_error_t *error;
132       u32 sub_sw_if_index;
133       uword *p;
134
135       /*
136        * find the main interface from the VNI
137        */
138       p = hash_get (lgm->l3_ifaces.sw_if_index_by_vni, vni);
139
140       if (NULL == p)
141         return (INDEX_INVALID);
142
143       hi = vnet_get_hw_interface (vnet_get_main (), p[0]);
144
145       if (NULL == hi)
146         return (INDEX_INVALID);
147
148       vnet_sw_interface_t sub_itf_template = {
149         .type = VNET_SW_INTERFACE_TYPE_SUB,
150         .sup_sw_if_index = hi->sw_if_index,
151         .sub.id = lisp_gpe_sub_interface_id++,
152       };
153
154       error = vnet_create_sw_interface (vnet_get_main (),
155                                         &sub_itf_template, &sub_sw_if_index);
156
157       if (NULL != error)
158         return (INDEX_INVALID);
159
160       pool_get (lisp_gpe_sub_interface_pool, l3s);
161       memset (l3s, 0, sizeof (*l3s));
162       l3s->key = clib_mem_alloc (sizeof (*l3s->key));
163       memset (l3s->key, 0, sizeof (*l3s->key));
164
165       l3s->key->local_rloc = *lrloc;
166       l3s->key->vni = clib_host_to_net_u32 (vni);
167       l3s->main_sw_if_index = hi->sw_if_index;
168       l3s->sw_if_index = sub_sw_if_index;
169       l3s->eid_table_id = overlay_table_id;
170
171       l3si = (l3s - lisp_gpe_sub_interface_pool);
172
173       lisp_gpe_sub_interface_set_table (l3s->sw_if_index, l3s->eid_table_id);
174       vnet_sw_interface_set_flags (vnet_get_main (),
175                                    l3s->sw_if_index,
176                                    VNET_SW_INTERFACE_FLAG_ADMIN_UP);
177
178       lisp_gpe_sub_interface_db_insert (l3s);
179     }
180   else
181     {
182       l3s = lisp_gpe_sub_interface_get_i (l3si);
183     }
184
185   l3s->locks++;
186
187   return (l3si);
188 }
189
190 void
191 lisp_gpe_sub_interface_unlock (index_t l3si)
192 {
193   lisp_gpe_sub_interface_t *l3s;
194
195   l3s = lisp_gpe_sub_interface_get_i (l3si);
196
197   l3s->locks--;
198
199   if (0 == l3s->locks)
200     {
201       lisp_gpe_sub_interface_unset_table (l3s->sw_if_index,
202                                           l3s->eid_table_id);
203
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 *
221 format_lisp_gpe_sub_interface (u8 * s, va_list ap)
222 {
223   lisp_gpe_sub_interface_t *l3s = va_arg (ap, lisp_gpe_sub_interface_t *);
224   vnet_main_t *vnm = vnet_get_main ();
225
226   s = format (s, "%=16U",
227               format_vnet_sw_interface_name,
228               vnm, vnet_get_sw_interface (vnm, l3s->sw_if_index));
229   s = format (s, "%=10d", clib_net_to_host_u32 (l3s->key->vni));
230   s = format (s, "%=12d", l3s->sw_if_index);
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
244   vlib_cli_output (vm, "%=16s%=10s%=12s%s", "Name", "VNI", "SW IF Index",
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) = {
259   .path = "show lisp gpe sub-interface",
260   .short_help = "show lisp gpe sub-interface",
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  */