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