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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <plugins/gbp/gbp_bridge_domain.h>
17 #include <plugins/gbp/gbp_endpoint.h>
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>
27 * Pool of GBP bridge_domains
29 gbp_bridge_domain_t *gbp_bridge_domain_pool;
32 * DB of bridge_domains
34 typedef struct gbp_bridge_domain_db_t
37 } gbp_bridge_domain_db_t;
39 static gbp_bridge_domain_db_t gbp_bridge_domain_db;
44 vlib_log_class_t gb_logger;
46 #define GBP_BD_DBG(...) \
47 vlib_log_debug (gb_logger, __VA_ARGS__);
50 gbp_bridge_domain_get (index_t i)
52 return (pool_elt_at_index (gbp_bridge_domain_pool, i));
56 gbp_bridge_domain_lock (index_t i)
58 gbp_bridge_domain_t *gb;
60 gb = gbp_bridge_domain_get (i);
65 gbp_bridge_domain_find (u32 bd_id)
69 p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
74 return (INDEX_INVALID);
78 gbp_bridge_domain_find_and_lock (u32 bd_id)
82 p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
86 gbp_bridge_domain_lock (p[0]);
89 return (INDEX_INVALID);
93 gbp_bridge_domain_db_add (gbp_bridge_domain_t * gb)
95 index_t gbi = gb - gbp_bridge_domain_pool;
97 hash_set (gbp_bridge_domain_db.gbd_by_bd_id, gb->gb_bd_id, gbi);
101 gbp_bridge_domain_db_remove (gbp_bridge_domain_t * gb)
103 hash_unset (gbp_bridge_domain_db.gbd_by_bd_id, gb->gb_bd_id);
107 gbp_bridge_domain_add_and_lock (u32 bd_id,
108 u32 bvi_sw_if_index, u32 uu_fwd_sw_if_index)
110 gbp_bridge_domain_t *gb;
113 gbi = gbp_bridge_domain_find (bd_id);
115 if (INDEX_INVALID == gbi)
119 bd_index = bd_find_index (&bd_main, bd_id);
122 return (VNET_API_ERROR_BD_NOT_MODIFIABLE);
125 * unset learning in the bridge
127 bd_set_flags (vlib_get_main (), bd_index, L2_LEARN, 0);
129 pool_get (gbp_bridge_domain_pool, gb);
130 memset (gb, 0, sizeof (*gb));
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;
139 * Set the BVI and uu-flood interfaces into the BD
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);
150 * Add the BVI's MAC to the L2FIB
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));
158 gbp_bridge_domain_db_add (gb);
162 gb = gbp_bridge_domain_get (gbi);
166 GBP_BD_DBG ("add: %U", format_gbp_bridge_domain, gb);
172 gbp_bridge_domain_unlock (index_t index)
174 gbp_bridge_domain_t *gb;
176 gb = gbp_bridge_domain_get (index);
180 if (0 == gb->gb_locks)
182 GBP_BD_DBG ("destroy: %U", format_gbp_bridge_domain, gb);
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);
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);
196 gbp_bridge_domain_db_remove (gb);
198 pool_put (gbp_bridge_domain_pool, gb);
203 gbp_bridge_domain_delete (u32 bd_id)
207 GBP_BD_DBG ("del: %d", bd_id);
208 gbi = gbp_bridge_domain_find (bd_id);
210 if (INDEX_INVALID != gbi)
212 GBP_BD_DBG ("del: %U", format_gbp_bridge_domain,
213 gbp_bridge_domain_get (gbi));
214 gbp_bridge_domain_unlock (gbi);
219 return (VNET_API_ERROR_NO_SUCH_ENTRY);
223 gbp_bridge_domain_walk (gbp_bridge_domain_cb_t cb, void *ctx)
225 gbp_bridge_domain_t *gbpe;
228 pool_foreach(gbpe, gbp_bridge_domain_pool,
236 static clib_error_t *
237 gbp_bridge_domain_cli (vlib_main_t * vm,
238 unformat_input_t * input, vlib_cli_command_t * cmd)
240 vnet_main_t *vnm = vnet_get_main ();
241 u32 uu_fwd_sw_if_index = ~0;
242 u32 bvi_sw_if_index = ~0;
246 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
248 if (unformat (input, "bvi %U", unformat_vnet_sw_interface,
249 vnm, &bvi_sw_if_index))
251 else if (unformat (input, "uu-flood %U", unformat_vnet_sw_interface,
252 vnm, &uu_fwd_sw_if_index))
254 else if (unformat (input, "add"))
256 else if (unformat (input, "del"))
258 else if (unformat (input, "bd %d", &bd_id))
265 return clib_error_return (0, "EPG-ID must be specified");
269 if (~0 == bvi_sw_if_index)
270 return clib_error_return (0, "interface must be specified");
272 gbp_bridge_domain_add_and_lock (bd_id,
273 bvi_sw_if_index, uu_fwd_sw_if_index);
276 gbp_bridge_domain_delete (bd_id);
282 * Configure a GBP bridge-domain
285 * @cliexstart{set gbp bridge-domain [del] bd <ID> bvi <interface> uu-flood <interface>}
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,
296 format_gbp_bridge_domain (u8 * s, va_list * args)
298 gbp_bridge_domain_t *gb = va_arg (*args, gbp_bridge_domain_t*);
299 vnet_main_t *vnm = vnet_get_main ();
302 s = format (s, "[%d] bd:[%d,%d], bvi:%U uu-flood:%U locks:%d",
303 gb - gbp_bridge_domain_pool,
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,
310 s = format (s, "NULL");
316 gbp_bridge_domain_show_one (gbp_bridge_domain_t *gb, void *ctx)
321 vlib_cli_output (vm, " %U",format_gbp_bridge_domain, gb);
326 static clib_error_t *
327 gbp_bridge_domain_show (vlib_main_t * vm,
328 unformat_input_t * input, vlib_cli_command_t * cmd)
330 vlib_cli_output (vm, "Bridge-Domains:");
331 gbp_bridge_domain_walk (gbp_bridge_domain_show_one, vm);
338 * Show Group Based Policy Bridge_Domains and derived information
341 * @cliexstart{show gbp bridge_domain}
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,
352 static clib_error_t *
353 gbp_bridge_domain_init (vlib_main_t * vm)
355 gb_logger = vlib_log_register_class ("gbp", "bd");
360 VLIB_INIT_FUNCTION (gbp_bridge_domain_init);
363 * fd.io coding-style-patch-verification: ON
366 * eval: (c-set-style "gnu")