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