blob: 6e145f527fbb73cf66c6b9d1812dbf6a39811593 [file] [log] [blame]
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001/*
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 Ranns5e575b12016-10-03 09:40:25 +010020#include <vnet/lisp-gpe/lisp_gpe_tenant.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010021#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 */
28static 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 */
33static 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 */
39uword *lisp_gpe_sub_interfaces_sw_if_index;
40
41/**
42 * The next available sub-interface ID. FIXME
43 */
44static u32 lisp_gpe_sub_interface_id;
45
46
47static index_t
48lisp_gpe_sub_interface_db_find (const ip_address_t * lrloc, u32 vni)
49{
50 uword *p;
51
Filip Tehlar6786f1b2017-03-07 10:23:01 +010052 lisp_gpe_sub_interface_key_t key;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010053
Filip Tehlar6786f1b2017-03-07 10:23:01 +010054 memset (&key, 0, sizeof (key));
55 ip_address_copy (&key.local_rloc, lrloc);
Florin Corasaf8c8e52017-08-17 15:57:20 -070056 key.vni = vni;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010057 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
65static void
66lisp_gpe_sub_interface_db_insert (const lisp_gpe_sub_interface_t * l3s)
67{
68 hash_set_mem (lisp_gpe_sub_interfaces,
Filip Tehlar6786f1b2017-03-07 10:23:01 +010069 l3s->key, l3s - lisp_gpe_sub_interface_pool);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010070 hash_set_mem (lisp_gpe_sub_interfaces_sw_if_index,
Filip Tehlar6786f1b2017-03-07 10:23:01 +010071 l3s->key, l3s->sw_if_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010072}
73
74static void
75lisp_gpe_sub_interface_db_remove (const lisp_gpe_sub_interface_t * l3s)
76{
Filip Tehlar6786f1b2017-03-07 10:23:01 +010077 hash_unset_mem (lisp_gpe_sub_interfaces, l3s->key);
78 hash_unset_mem (lisp_gpe_sub_interfaces_sw_if_index, l3s->key);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010079}
80
81lisp_gpe_sub_interface_t *
82lisp_gpe_sub_interface_get_i (index_t l3si)
83{
84 return (pool_elt_at_index (lisp_gpe_sub_interface_pool, l3si));
85}
86
87static void
88lisp_gpe_sub_interface_set_table (u32 sw_if_index, u32 table_id)
89{
90 fib_node_index_t fib_index;
91
Neale Ranns15002542017-09-10 04:39:11 -070092 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, table_id,
93 FIB_SOURCE_LISP);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010094 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 Ranns0bfe5d82016-08-25 15:29:12 +010098
Neale Ranns15002542017-09-10 04:39:11 -070099 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, table_id,
100 FIB_SOURCE_LISP);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100101 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 Ranns0bfe5d82016-08-25 15:29:12 +0100105}
106
107static void
108lisp_gpe_sub_interface_unset_table (u32 sw_if_index, u32 table_id)
109{
Neale Ranns15002542017-09-10 04:39:11 -0700110 fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index],
111 FIB_PROTOCOL_IP4, FIB_SOURCE_LISP);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100112 ip4_main.fib_index_by_sw_if_index[sw_if_index] = 0;
113 ip4_sw_interface_enable_disable (sw_if_index, 0);
114
Neale Ranns15002542017-09-10 04:39:11 -0700115 fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index],
116 FIB_PROTOCOL_IP6, FIB_SOURCE_LISP);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100117 ip6_main.fib_index_by_sw_if_index[sw_if_index] = 0;
118 ip6_sw_interface_enable_disable (sw_if_index, 0);
119}
120
121index_t
122lisp_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 Ranns0bfe5d82016-08-25 15:29:12 +0100126 index_t l3si;
127
Florin Corasaf8c8e52017-08-17 15:57:20 -0700128 l3si = lisp_gpe_sub_interface_db_find (lrloc, vni);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100129
130 if (INDEX_INVALID == l3si)
131 {
Neale Ranns5e575b12016-10-03 09:40:25 +0100132 u32 main_sw_if_index, sub_sw_if_index;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100133
134 /*
135 * find the main interface from the VNI
136 */
Neale Ranns5e575b12016-10-03 09:40:25 +0100137 main_sw_if_index =
138 lisp_gpe_tenant_l3_iface_add_or_lock (vni, overlay_table_id);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100139
140 vnet_sw_interface_t sub_itf_template = {
141 .type = VNET_SW_INTERFACE_TYPE_SUB,
Eyal Baric5b13602016-11-24 19:42:43 +0200142 .flood_class = VNET_FLOOD_CLASS_NORMAL,
Neale Ranns5e575b12016-10-03 09:40:25 +0100143 .sup_sw_if_index = main_sw_if_index,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100144 .sub.id = lisp_gpe_sub_interface_id++,
145 };
146
Neale Ranns5e575b12016-10-03 09:40:25 +0100147 if (NULL != vnet_create_sw_interface (vnet_get_main (),
148 &sub_itf_template,
149 &sub_sw_if_index))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100150 return (INDEX_INVALID);
151
152 pool_get (lisp_gpe_sub_interface_pool, l3s);
153 memset (l3s, 0, sizeof (*l3s));
154 l3s->key = clib_mem_alloc (sizeof (*l3s->key));
155 memset (l3s->key, 0, sizeof (*l3s->key));
156
Florin Corasaf8c8e52017-08-17 15:57:20 -0700157 ip_address_copy (&l3s->key->local_rloc, lrloc);
158 l3s->key->vni = vni;
Neale Ranns5e575b12016-10-03 09:40:25 +0100159 l3s->main_sw_if_index = main_sw_if_index;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100160 l3s->sw_if_index = sub_sw_if_index;
161 l3s->eid_table_id = overlay_table_id;
162
163 l3si = (l3s - lisp_gpe_sub_interface_pool);
164
Neale Ranns5e575b12016-10-03 09:40:25 +0100165 // FIXME. enable When we get an adj
166 ip6_sw_interface_enable_disable (l3s->sw_if_index, 1);
167 ip4_sw_interface_enable_disable (l3s->sw_if_index, 1);
168
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100169 vnet_sw_interface_set_flags (vnet_get_main (),
170 l3s->sw_if_index,
171 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
172
173 lisp_gpe_sub_interface_db_insert (l3s);
174 }
175 else
176 {
177 l3s = lisp_gpe_sub_interface_get_i (l3si);
Neale Ranns5e575b12016-10-03 09:40:25 +0100178 l3s->eid_table_id = overlay_table_id;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100179 }
180
Neale Ranns5e575b12016-10-03 09:40:25 +0100181 lisp_gpe_sub_interface_set_table (l3s->sw_if_index, l3s->eid_table_id);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100182 l3s->locks++;
183
184 return (l3si);
185}
186
187void
188lisp_gpe_sub_interface_unlock (index_t l3si)
189{
190 lisp_gpe_sub_interface_t *l3s;
191
192 l3s = lisp_gpe_sub_interface_get_i (l3si);
193
Neale Ranns15002542017-09-10 04:39:11 -0700194 ASSERT (0 != l3s->locks);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100195 l3s->locks--;
196
197 if (0 == l3s->locks)
198 {
199 lisp_gpe_sub_interface_unset_table (l3s->sw_if_index,
200 l3s->eid_table_id);
201
Filip Tehlar809bc742017-08-14 19:15:36 +0200202 lisp_gpe_tenant_l3_iface_unlock (l3s->key->vni);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100203 vnet_sw_interface_set_flags (vnet_get_main (), l3s->sw_if_index, 0);
204 vnet_delete_sub_interface (l3s->sw_if_index);
205
206 lisp_gpe_sub_interface_db_remove (l3s);
207
208 clib_mem_free (l3s->key);
209 pool_put (lisp_gpe_sub_interface_pool, l3s);
210 }
211}
212
213const lisp_gpe_sub_interface_t *
214lisp_gpe_sub_interface_get (index_t l3si)
215{
216 return (lisp_gpe_sub_interface_get_i (l3si));
217}
218
219u8 *
Christophe Fontained3c008d2017-10-02 18:10:54 +0200220format_lisp_gpe_sub_interface (u8 * s, va_list * ap)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100221{
Christophe Fontained3c008d2017-10-02 18:10:54 +0200222 lisp_gpe_sub_interface_t *l3s = va_arg (*ap, lisp_gpe_sub_interface_t *);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100223 vnet_main_t *vnm = vnet_get_main ();
224
Florin Corasaf8c8e52017-08-17 15:57:20 -0700225 s = format (s, "%-16U",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100226 format_vnet_sw_interface_name,
227 vnm, vnet_get_sw_interface (vnm, l3s->sw_if_index));
Florin Corasaf8c8e52017-08-17 15:57:20 -0700228 s = format (s, "%=8d", l3s->key->vni);
229 s = format (s, "%=15d", l3s->sw_if_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100230 s = format (s, "%U", format_ip_address, &l3s->key->local_rloc);
231
232 return (s);
233}
234
235/** CLI command to show LISP-GPE interfaces. */
236static clib_error_t *
237lisp_gpe_sub_interface_show (vlib_main_t * vm,
238 unformat_input_t * input,
239 vlib_cli_command_t * cmd)
240{
241 lisp_gpe_sub_interface_t *l3s;
242
Florin Corasaf8c8e52017-08-17 15:57:20 -0700243 vlib_cli_output (vm, "%-16s%=8s%=15s%s", "Name", "VNI", "sw_if_index",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100244 "local RLOC");
245
246 /* *INDENT-OFF* */
247 pool_foreach (l3s, lisp_gpe_sub_interface_pool,
248 ({
249 vlib_cli_output (vm, "%U", format_lisp_gpe_sub_interface, l3s);
250 }));
251 /* *INDENT-ON* */
252
253 return 0;
254}
255
256/* *INDENT-OFF* */
257VLIB_CLI_COMMAND (lisp_gpe_sub_interface_command) = {
Filip Tehlar82786c42017-02-20 15:20:37 +0100258 .path = "show gpe sub-interface",
259 .short_help = "show gpe sub-interface",
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100260 .function = lisp_gpe_sub_interface_show,
261};
262/* *INDENT-ON* */
263
264static clib_error_t *
265lisp_gpe_sub_interface_module_init (vlib_main_t * vm)
266{
267 lisp_gpe_sub_interfaces =
268 hash_create_mem (0,
269 sizeof (lisp_gpe_sub_interface_key_t), sizeof (uword));
270 lisp_gpe_sub_interfaces_sw_if_index =
271 hash_create_mem (0,
272 sizeof (lisp_gpe_sub_interface_key_t), sizeof (uword));
273
274 return (NULL);
275}
276
277VLIB_INIT_FUNCTION (lisp_gpe_sub_interface_module_init);
278
279/*
280 * fd.io coding-style-patch-verification: ON
281 *
282 * Local Variables:
283 * eval: (c-set-style "gnu")
284 * End:
285 */