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