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 gbp_bridge_domain_db_t gbp_bridge_domain_db;
39 vlib_log_class_t gb_logger;
41 #define GBP_BD_DBG(...) \
42 vlib_log_debug (gb_logger, __VA_ARGS__);
45 gbp_bridge_domain_index (const gbp_bridge_domain_t * gbd)
47 return (gbd - gbp_bridge_domain_pool);
51 gbp_bridge_domain_lock (index_t i)
53 gbp_bridge_domain_t *gb;
55 gb = gbp_bridge_domain_get (i);
60 gbp_bridge_domain_get_bd_id (index_t gbdi)
62 gbp_bridge_domain_t *gb;
64 gb = gbp_bridge_domain_get (gbdi);
66 return (gb->gb_bd_id);
70 gbp_bridge_domain_find (u32 bd_id)
74 p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
79 return (INDEX_INVALID);
83 gbp_bridge_domain_find_and_lock (u32 bd_id)
87 p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
91 gbp_bridge_domain_lock (p[0]);
94 return (INDEX_INVALID);
98 gbp_bridge_domain_db_add (gbp_bridge_domain_t * gb)
100 index_t gbi = gb - gbp_bridge_domain_pool;
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;
109 gbp_bridge_domain_db_remove (gbp_bridge_domain_t * gb)
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;
116 format_gbp_bridge_domain_ptr (u8 * s, va_list * args)
118 gbp_bridge_domain_t *gb = va_arg (*args, gbp_bridge_domain_t *);
119 vnet_main_t *vnm = vnet_get_main ();
122 s = format (s, "[%d] bd:[%d,%d], bvi:%U uu-flood:%U locks:%d",
123 gb - gbp_bridge_domain_pool,
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,
130 s = format (s, "NULL");
136 format_gbp_bridge_domain (u8 * s, va_list * args)
138 index_t gbi = va_arg (*args, index_t);
141 format (s, "%U", format_gbp_bridge_domain_ptr,
142 gbp_bridge_domain_get (gbi));
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)
152 gbp_bridge_domain_t *gb;
155 gbi = gbp_bridge_domain_find (bd_id);
157 if (INDEX_INVALID == gbi)
161 bd_index = bd_find_index (&bd_main, bd_id);
164 return (VNET_API_ERROR_BD_NOT_MODIFIABLE);
167 * unset learning in the bridge
169 bd_set_flags (vlib_get_main (), bd_index, L2_LEARN, 0);
171 pool_get (gbp_bridge_domain_pool, gb);
172 memset (gb, 0, sizeof (*gb));
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;
179 gb->gb_flags = flags;
182 * Set the BVI and uu-flood interfaces into the BD
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);
193 * Add the BVI's MAC to the L2FIB
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));
201 gbp_bridge_domain_db_add (gb);
205 gb = gbp_bridge_domain_get (gbi);
209 GBP_BD_DBG ("add: %U", format_gbp_bridge_domain_ptr, gb);
215 gbp_bridge_domain_unlock (index_t index)
217 gbp_bridge_domain_t *gb;
219 gb = gbp_bridge_domain_get (index);
223 if (0 == gb->gb_locks)
225 GBP_BD_DBG ("destroy: %U", format_gbp_bridge_domain_ptr, gb);
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);
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);
239 gbp_bridge_domain_db_remove (gb);
241 pool_put (gbp_bridge_domain_pool, gb);
246 gbp_bridge_domain_delete (u32 bd_id)
250 GBP_BD_DBG ("del: %d", bd_id);
251 gbi = gbp_bridge_domain_find (bd_id);
253 if (INDEX_INVALID != gbi)
255 GBP_BD_DBG ("del: %U", format_gbp_bridge_domain, gbi);
256 gbp_bridge_domain_unlock (gbi);
261 return (VNET_API_ERROR_NO_SUCH_ENTRY);
265 gbp_bridge_domain_walk (gbp_bridge_domain_cb_t cb, void *ctx)
267 gbp_bridge_domain_t *gbpe;
270 pool_foreach(gbpe, gbp_bridge_domain_pool,
278 static clib_error_t *
279 gbp_bridge_domain_cli (vlib_main_t * vm,
280 unformat_input_t * input, vlib_cli_command_t * cmd)
282 vnet_main_t *vnm = vnet_get_main ();
283 u32 uu_fwd_sw_if_index = ~0;
284 u32 bvi_sw_if_index = ~0;
288 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
290 if (unformat (input, "bvi %U", unformat_vnet_sw_interface,
291 vnm, &bvi_sw_if_index))
293 else if (unformat (input, "uu-flood %U", unformat_vnet_sw_interface,
294 vnm, &uu_fwd_sw_if_index))
296 else if (unformat (input, "add"))
298 else if (unformat (input, "del"))
300 else if (unformat (input, "bd %d", &bd_id))
307 return clib_error_return (0, "EPG-ID must be specified");
311 if (~0 == bvi_sw_if_index)
312 return clib_error_return (0, "interface must be specified");
314 gbp_bridge_domain_add_and_lock (bd_id, GBP_BD_FLAG_NONE,
315 bvi_sw_if_index, uu_fwd_sw_if_index);
318 gbp_bridge_domain_delete (bd_id);
324 * Configure a GBP bridge-domain
327 * @cliexstart{set gbp bridge-domain [del] bd <ID> bvi <interface> uu-flood <interface>}
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,
338 gbp_bridge_domain_show_one (gbp_bridge_domain_t *gb, void *ctx)
343 vlib_cli_output (vm, " %U", format_gbp_bridge_domain_ptr, gb);
348 static clib_error_t *
349 gbp_bridge_domain_show (vlib_main_t * vm,
350 unformat_input_t * input, vlib_cli_command_t * cmd)
352 vlib_cli_output (vm, "Bridge-Domains:");
353 gbp_bridge_domain_walk (gbp_bridge_domain_show_one, vm);
360 * Show Group Based Policy Bridge_Domains and derived information
363 * @cliexstart{show gbp bridge_domain}
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,
374 static clib_error_t *
375 gbp_bridge_domain_init (vlib_main_t * vm)
377 gb_logger = vlib_log_register_class ("gbp", "bd");
382 VLIB_INIT_FUNCTION (gbp_bridge_domain_init);
385 * fd.io coding-style-patch-verification: ON
388 * eval: (c-set-style "gnu")