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_find (u32 bd_id)
64 p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
69 return (INDEX_INVALID);
73 gbp_bridge_domain_find_and_lock (u32 bd_id)
77 p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
81 gbp_bridge_domain_lock (p[0]);
84 return (INDEX_INVALID);
88 gbp_bridge_domain_db_add (gbp_bridge_domain_t * gb)
90 index_t gbi = gb - gbp_bridge_domain_pool;
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;
99 gbp_bridge_domain_db_remove (gbp_bridge_domain_t * gb)
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;
106 format_gbp_bridge_domain_ptr (u8 * s, va_list * args)
108 gbp_bridge_domain_t *gb = va_arg (*args, gbp_bridge_domain_t *);
109 vnet_main_t *vnm = vnet_get_main ();
112 s = format (s, "[%d] bd:[%d,%d], bvi:%U uu-flood:%U locks:%d",
113 gb - gbp_bridge_domain_pool,
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,
120 s = format (s, "NULL");
126 format_gbp_bridge_domain (u8 * s, va_list * args)
128 index_t gbi = va_arg (*args, index_t);
131 format (s, "%U", format_gbp_bridge_domain_ptr,
132 gbp_bridge_domain_get (gbi));
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)
142 gbp_bridge_domain_t *gb;
145 gbi = gbp_bridge_domain_find (bd_id);
147 if (INDEX_INVALID == gbi)
151 bd_index = bd_find_index (&bd_main, bd_id);
154 return (VNET_API_ERROR_BD_NOT_MODIFIABLE);
157 * unset learning in the bridge
159 bd_set_flags (vlib_get_main (), bd_index, L2_LEARN, 0);
161 pool_get (gbp_bridge_domain_pool, gb);
162 memset (gb, 0, sizeof (*gb));
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;
169 gb->gb_flags = flags;
172 * Set the BVI and uu-flood interfaces into the BD
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);
183 * Add the BVI's MAC to the L2FIB
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));
191 gbp_bridge_domain_db_add (gb);
195 gb = gbp_bridge_domain_get (gbi);
199 GBP_BD_DBG ("add: %U", format_gbp_bridge_domain_ptr, gb);
205 gbp_bridge_domain_unlock (index_t index)
207 gbp_bridge_domain_t *gb;
209 gb = gbp_bridge_domain_get (index);
213 if (0 == gb->gb_locks)
215 GBP_BD_DBG ("destroy: %U", format_gbp_bridge_domain_ptr, gb);
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);
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);
229 gbp_bridge_domain_db_remove (gb);
231 pool_put (gbp_bridge_domain_pool, gb);
236 gbp_bridge_domain_delete (u32 bd_id)
240 GBP_BD_DBG ("del: %d", bd_id);
241 gbi = gbp_bridge_domain_find (bd_id);
243 if (INDEX_INVALID != gbi)
245 GBP_BD_DBG ("del: %U", format_gbp_bridge_domain, gbi);
246 gbp_bridge_domain_unlock (gbi);
251 return (VNET_API_ERROR_NO_SUCH_ENTRY);
255 gbp_bridge_domain_walk (gbp_bridge_domain_cb_t cb, void *ctx)
257 gbp_bridge_domain_t *gbpe;
260 pool_foreach(gbpe, gbp_bridge_domain_pool,
268 static clib_error_t *
269 gbp_bridge_domain_cli (vlib_main_t * vm,
270 unformat_input_t * input, vlib_cli_command_t * cmd)
272 vnet_main_t *vnm = vnet_get_main ();
273 u32 uu_fwd_sw_if_index = ~0;
274 u32 bvi_sw_if_index = ~0;
278 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
280 if (unformat (input, "bvi %U", unformat_vnet_sw_interface,
281 vnm, &bvi_sw_if_index))
283 else if (unformat (input, "uu-flood %U", unformat_vnet_sw_interface,
284 vnm, &uu_fwd_sw_if_index))
286 else if (unformat (input, "add"))
288 else if (unformat (input, "del"))
290 else if (unformat (input, "bd %d", &bd_id))
297 return clib_error_return (0, "EPG-ID must be specified");
301 if (~0 == bvi_sw_if_index)
302 return clib_error_return (0, "interface must be specified");
304 gbp_bridge_domain_add_and_lock (bd_id, GBP_BD_FLAG_NONE,
305 bvi_sw_if_index, uu_fwd_sw_if_index);
308 gbp_bridge_domain_delete (bd_id);
314 * Configure a GBP bridge-domain
317 * @cliexstart{set gbp bridge-domain [del] bd <ID> bvi <interface> uu-flood <interface>}
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,
328 gbp_bridge_domain_show_one (gbp_bridge_domain_t *gb, void *ctx)
333 vlib_cli_output (vm, " %U", format_gbp_bridge_domain_ptr, gb);
338 static clib_error_t *
339 gbp_bridge_domain_show (vlib_main_t * vm,
340 unformat_input_t * input, vlib_cli_command_t * cmd)
342 vlib_cli_output (vm, "Bridge-Domains:");
343 gbp_bridge_domain_walk (gbp_bridge_domain_show_one, vm);
350 * Show Group Based Policy Bridge_Domains and derived information
353 * @cliexstart{show gbp bridge_domain}
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,
364 static clib_error_t *
365 gbp_bridge_domain_init (vlib_main_t * vm)
367 gb_logger = vlib_log_register_class ("gbp", "bd");
372 VLIB_INIT_FUNCTION (gbp_bridge_domain_init);
375 * fd.io coding-style-patch-verification: ON
378 * eval: (c-set-style "gnu")