GBP Endpoint Learning
[vpp.git] / src / plugins / gbp / gbp_bridge_domain.c
1 /*
2  * Copyright (c) 2018 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 #include <plugins/gbp/gbp_bridge_domain.h>
17 #include <plugins/gbp/gbp_endpoint.h>
18
19 #include <vnet/dpo/dvr_dpo.h>
20 #include <vnet/fib/fib_table.h>
21 #include <vnet/l2/l2_input.h>
22 #include <vnet/l2/feat_bitmap.h>
23 #include <vnet/l2/l2_bvi.h>
24 #include <vnet/l2/l2_fib.h>
25
26 /**
27  * Pool of GBP bridge_domains
28  */
29 gbp_bridge_domain_t *gbp_bridge_domain_pool;
30
31 /**
32  * DB of bridge_domains
33  */
34 typedef struct gbp_bridge_domain_db_t
35 {
36   uword *gbd_by_bd_id;
37 } gbp_bridge_domain_db_t;
38
39 static gbp_bridge_domain_db_t gbp_bridge_domain_db;
40
41 /**
42  * logger
43  */
44 vlib_log_class_t gb_logger;
45
46 #define GBP_BD_DBG(...)                           \
47     vlib_log_debug (gb_logger, __VA_ARGS__);
48
49 gbp_bridge_domain_t *
50 gbp_bridge_domain_get (index_t i)
51 {
52   return (pool_elt_at_index (gbp_bridge_domain_pool, i));
53 }
54
55 static void
56 gbp_bridge_domain_lock (index_t i)
57 {
58   gbp_bridge_domain_t *gb;
59
60   gb = gbp_bridge_domain_get (i);
61   gb->gb_locks++;
62 }
63
64 static index_t
65 gbp_bridge_domain_find (u32 bd_id)
66 {
67   uword *p;
68
69   p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
70
71   if (NULL != p)
72     return p[0];
73
74   return (INDEX_INVALID);
75 }
76
77 index_t
78 gbp_bridge_domain_find_and_lock (u32 bd_id)
79 {
80   uword *p;
81
82   p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
83
84   if (NULL != p)
85     {
86       gbp_bridge_domain_lock (p[0]);
87       return p[0];
88     }
89   return (INDEX_INVALID);
90 }
91
92 static void
93 gbp_bridge_domain_db_add (gbp_bridge_domain_t * gb)
94 {
95   index_t gbi = gb - gbp_bridge_domain_pool;
96
97   hash_set (gbp_bridge_domain_db.gbd_by_bd_id, gb->gb_bd_id, gbi);
98 }
99
100 static void
101 gbp_bridge_domain_db_remove (gbp_bridge_domain_t * gb)
102 {
103   hash_unset (gbp_bridge_domain_db.gbd_by_bd_id, gb->gb_bd_id);
104 }
105
106 int
107 gbp_bridge_domain_add_and_lock (u32 bd_id,
108                                 u32 bvi_sw_if_index, u32 uu_fwd_sw_if_index)
109 {
110   gbp_bridge_domain_t *gb;
111   index_t gbi;
112
113   gbi = gbp_bridge_domain_find (bd_id);
114
115   if (INDEX_INVALID == gbi)
116     {
117       u32 bd_index;
118
119       bd_index = bd_find_index (&bd_main, bd_id);
120
121       if (~0 == bd_index)
122         return (VNET_API_ERROR_BD_NOT_MODIFIABLE);
123
124       /*
125        * unset learning in the bridge
126        */
127       bd_set_flags (vlib_get_main (), bd_index, L2_LEARN, 0);
128
129       pool_get (gbp_bridge_domain_pool, gb);
130       memset (gb, 0, sizeof (*gb));
131
132       gb->gb_bd_id = bd_id;
133       gb->gb_bd_index = bd_index;
134       gb->gb_uu_fwd_sw_if_index = uu_fwd_sw_if_index;
135       gb->gb_bvi_sw_if_index = bvi_sw_if_index;
136       gb->gb_locks = 1;
137
138       /*
139        * Set the BVI and uu-flood interfaces into the BD
140        */
141       set_int_l2_mode (vlib_get_main (), vnet_get_main (),
142                        MODE_L2_BRIDGE, gb->gb_bvi_sw_if_index,
143                        bd_index, L2_BD_PORT_TYPE_BVI, 0, 0);
144       if (~0 != gb->gb_uu_fwd_sw_if_index)
145         set_int_l2_mode (vlib_get_main (), vnet_get_main (),
146                          MODE_L2_BRIDGE, gb->gb_uu_fwd_sw_if_index,
147                          bd_index, L2_BD_PORT_TYPE_UU_FWD, 0, 0);
148
149       /*
150        * Add the BVI's MAC to the L2FIB
151        */
152       l2fib_add_entry (vnet_sw_interface_get_hw_address
153                        (vnet_get_main (), gb->gb_bvi_sw_if_index),
154                        gb->gb_bd_index, gb->gb_bvi_sw_if_index,
155                        (L2FIB_ENTRY_RESULT_FLAG_STATIC |
156                         L2FIB_ENTRY_RESULT_FLAG_BVI));
157
158       gbp_bridge_domain_db_add (gb);
159     }
160   else
161     {
162       gb = gbp_bridge_domain_get (gbi);
163       gb->gb_locks++;
164     }
165
166   GBP_BD_DBG ("add: %U", format_gbp_bridge_domain, gb);
167
168   return (0);
169 }
170
171 void
172 gbp_bridge_domain_unlock (index_t index)
173 {
174   gbp_bridge_domain_t *gb;
175
176   gb = gbp_bridge_domain_get (index);
177
178   gb->gb_locks--;
179
180   if (0 == gb->gb_locks)
181     {
182       GBP_BD_DBG ("destroy: %U", format_gbp_bridge_domain, gb);
183
184       l2fib_del_entry (vnet_sw_interface_get_hw_address
185                        (vnet_get_main (), gb->gb_bvi_sw_if_index),
186                        gb->gb_bd_index, gb->gb_bvi_sw_if_index);
187
188       set_int_l2_mode (vlib_get_main (), vnet_get_main (),
189                        MODE_L3, gb->gb_bvi_sw_if_index,
190                        gb->gb_bd_index, L2_BD_PORT_TYPE_BVI, 0, 0);
191       if (~0 != gb->gb_uu_fwd_sw_if_index)
192         set_int_l2_mode (vlib_get_main (), vnet_get_main (),
193                          MODE_L3, gb->gb_uu_fwd_sw_if_index,
194                          gb->gb_bd_index, L2_BD_PORT_TYPE_UU_FWD, 0, 0);
195
196       gbp_bridge_domain_db_remove (gb);
197
198       pool_put (gbp_bridge_domain_pool, gb);
199     }
200 }
201
202 int
203 gbp_bridge_domain_delete (u32 bd_id)
204 {
205   index_t gbi;
206
207   GBP_BD_DBG ("del: %d", bd_id);
208   gbi = gbp_bridge_domain_find (bd_id);
209
210   if (INDEX_INVALID != gbi)
211     {
212       GBP_BD_DBG ("del: %U", format_gbp_bridge_domain,
213                   gbp_bridge_domain_get (gbi));
214       gbp_bridge_domain_unlock (gbi);
215
216       return (0);
217     }
218
219   return (VNET_API_ERROR_NO_SUCH_ENTRY);
220 }
221
222 void
223 gbp_bridge_domain_walk (gbp_bridge_domain_cb_t cb, void *ctx)
224 {
225   gbp_bridge_domain_t *gbpe;
226
227   /* *INDENT-OFF* */
228   pool_foreach(gbpe, gbp_bridge_domain_pool,
229   {
230     if (!cb(gbpe, ctx))
231       break;
232   });
233   /* *INDENT-ON* */
234 }
235
236 static clib_error_t *
237 gbp_bridge_domain_cli (vlib_main_t * vm,
238                        unformat_input_t * input, vlib_cli_command_t * cmd)
239 {
240   vnet_main_t *vnm = vnet_get_main ();
241   u32 uu_fwd_sw_if_index = ~0;
242   u32 bvi_sw_if_index = ~0;
243   u32 bd_id = ~0;
244   u8 add = 1;
245
246   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
247     {
248       if (unformat (input, "bvi %U", unformat_vnet_sw_interface,
249                     vnm, &bvi_sw_if_index))
250         ;
251       else if (unformat (input, "uu-flood %U", unformat_vnet_sw_interface,
252                          vnm, &uu_fwd_sw_if_index))
253         ;
254       else if (unformat (input, "add"))
255         add = 1;
256       else if (unformat (input, "del"))
257         add = 0;
258       else if (unformat (input, "bd %d", &bd_id))
259         ;
260       else
261         break;
262     }
263
264   if (~0 == bd_id)
265     return clib_error_return (0, "EPG-ID must be specified");
266
267   if (add)
268     {
269       if (~0 == bvi_sw_if_index)
270         return clib_error_return (0, "interface must be specified");
271
272       gbp_bridge_domain_add_and_lock (bd_id,
273                                       bvi_sw_if_index, uu_fwd_sw_if_index);
274     }
275   else
276     gbp_bridge_domain_delete (bd_id);
277
278   return (NULL);
279 }
280
281 /*?
282  * Configure a GBP bridge-domain
283  *
284  * @cliexpar
285  * @cliexstart{set gbp bridge-domain [del] bd <ID> bvi <interface> uu-flood <interface>}
286  * @cliexend
287  ?*/
288 /* *INDENT-OFF* */
289 VLIB_CLI_COMMAND (gbp_bridge_domain_cli_node, static) = {
290   .path = "gbp bridge-domain",
291   .short_help = "gbp bridge-domain [del] epg bd <ID> bvi <interface> uu-flood <interface>",
292   .function = gbp_bridge_domain_cli,
293 };
294
295 u8 *
296 format_gbp_bridge_domain (u8 * s, va_list * args)
297 {
298   gbp_bridge_domain_t *gb = va_arg (*args, gbp_bridge_domain_t*);
299   vnet_main_t *vnm = vnet_get_main ();
300
301   if (NULL != gb)
302     s = format (s, "[%d] bd:[%d,%d], bvi:%U uu-flood:%U locks:%d",
303                 gb - gbp_bridge_domain_pool,
304                 gb->gb_bd_id,
305                 gb->gb_bd_index,
306                 format_vnet_sw_if_index_name, vnm, gb->gb_bvi_sw_if_index,
307                 format_vnet_sw_if_index_name, vnm, gb->gb_uu_fwd_sw_if_index,
308                 gb->gb_locks);
309   else
310     s = format (s, "NULL");
311
312   return (s);
313 }
314
315 static int
316 gbp_bridge_domain_show_one (gbp_bridge_domain_t *gb, void *ctx)
317 {
318   vlib_main_t *vm;
319
320   vm = ctx;
321   vlib_cli_output (vm, "  %U",format_gbp_bridge_domain, gb);
322
323   return (1);
324 }
325
326 static clib_error_t *
327 gbp_bridge_domain_show (vlib_main_t * vm,
328                    unformat_input_t * input, vlib_cli_command_t * cmd)
329 {
330   vlib_cli_output (vm, "Bridge-Domains:");
331   gbp_bridge_domain_walk (gbp_bridge_domain_show_one, vm);
332
333   return (NULL);
334 }
335
336
337 /*?
338  * Show Group Based Policy Bridge_Domains and derived information
339  *
340  * @cliexpar
341  * @cliexstart{show gbp bridge_domain}
342  * @cliexend
343  ?*/
344 /* *INDENT-OFF* */
345 VLIB_CLI_COMMAND (gbp_bridge_domain_show_node, static) = {
346   .path = "show gbp bridge-domain",
347   .short_help = "show gbp bridge-domain\n",
348   .function = gbp_bridge_domain_show,
349 };
350 /* *INDENT-ON* */
351
352 static clib_error_t *
353 gbp_bridge_domain_init (vlib_main_t * vm)
354 {
355   gb_logger = vlib_log_register_class ("gbp", "bd");
356
357   return (NULL);
358 }
359
360 VLIB_INIT_FUNCTION (gbp_bridge_domain_init);
361
362 /*
363  * fd.io coding-style-patch-verification: ON
364  *
365  * Local Variables:
366  * eval: (c-set-style "gnu")
367  * End:
368  */