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_lock (index_t i)
47 gbp_bridge_domain_t *gb;
49 gb = gbp_bridge_domain_get (i);
54 gbp_bridge_domain_find (u32 bd_id)
58 p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
63 return (INDEX_INVALID);
67 gbp_bridge_domain_find_and_lock (u32 bd_id)
71 p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
75 gbp_bridge_domain_lock (p[0]);
78 return (INDEX_INVALID);
82 gbp_bridge_domain_db_add (gbp_bridge_domain_t * gb)
84 index_t gbi = gb - gbp_bridge_domain_pool;
86 hash_set (gbp_bridge_domain_db.gbd_by_bd_id, gb->gb_bd_id, gbi);
87 vec_validate_init_empty (gbp_bridge_domain_db.gbd_by_bd_index,
88 gb->gb_bd_index, INDEX_INVALID);
89 gbp_bridge_domain_db.gbd_by_bd_index[gb->gb_bd_index] = gbi;
93 gbp_bridge_domain_db_remove (gbp_bridge_domain_t * gb)
95 hash_unset (gbp_bridge_domain_db.gbd_by_bd_id, gb->gb_bd_id);
96 gbp_bridge_domain_db.gbd_by_bd_index[gb->gb_bd_index] = INDEX_INVALID;
100 gbp_bridge_domain_add_and_lock (u32 bd_id,
101 gbp_bridge_domain_flags_t flags,
102 u32 bvi_sw_if_index, u32 uu_fwd_sw_if_index)
104 gbp_bridge_domain_t *gb;
107 gbi = gbp_bridge_domain_find (bd_id);
109 if (INDEX_INVALID == gbi)
113 bd_index = bd_find_index (&bd_main, bd_id);
116 return (VNET_API_ERROR_BD_NOT_MODIFIABLE);
119 * unset learning in the bridge
121 bd_set_flags (vlib_get_main (), bd_index, L2_LEARN, 0);
123 pool_get (gbp_bridge_domain_pool, gb);
124 memset (gb, 0, sizeof (*gb));
126 gb->gb_bd_id = bd_id;
127 gb->gb_bd_index = bd_index;
128 gb->gb_uu_fwd_sw_if_index = uu_fwd_sw_if_index;
129 gb->gb_bvi_sw_if_index = bvi_sw_if_index;
131 gb->gb_flags = flags;
134 * Set the BVI and uu-flood interfaces into the BD
136 set_int_l2_mode (vlib_get_main (), vnet_get_main (),
137 MODE_L2_BRIDGE, gb->gb_bvi_sw_if_index,
138 bd_index, L2_BD_PORT_TYPE_BVI, 0, 0);
139 if (~0 != gb->gb_uu_fwd_sw_if_index)
140 set_int_l2_mode (vlib_get_main (), vnet_get_main (),
141 MODE_L2_BRIDGE, gb->gb_uu_fwd_sw_if_index,
142 bd_index, L2_BD_PORT_TYPE_UU_FWD, 0, 0);
145 * Add the BVI's MAC to the L2FIB
147 l2fib_add_entry (vnet_sw_interface_get_hw_address
148 (vnet_get_main (), gb->gb_bvi_sw_if_index),
149 gb->gb_bd_index, gb->gb_bvi_sw_if_index,
150 (L2FIB_ENTRY_RESULT_FLAG_STATIC |
151 L2FIB_ENTRY_RESULT_FLAG_BVI));
153 gbp_bridge_domain_db_add (gb);
157 gb = gbp_bridge_domain_get (gbi);
161 GBP_BD_DBG ("add: %U", format_gbp_bridge_domain, gb);
167 gbp_bridge_domain_unlock (index_t index)
169 gbp_bridge_domain_t *gb;
171 gb = gbp_bridge_domain_get (index);
175 if (0 == gb->gb_locks)
177 GBP_BD_DBG ("destroy: %U", format_gbp_bridge_domain, gb);
179 l2fib_del_entry (vnet_sw_interface_get_hw_address
180 (vnet_get_main (), gb->gb_bvi_sw_if_index),
181 gb->gb_bd_index, gb->gb_bvi_sw_if_index);
183 set_int_l2_mode (vlib_get_main (), vnet_get_main (),
184 MODE_L3, gb->gb_bvi_sw_if_index,
185 gb->gb_bd_index, L2_BD_PORT_TYPE_BVI, 0, 0);
186 if (~0 != gb->gb_uu_fwd_sw_if_index)
187 set_int_l2_mode (vlib_get_main (), vnet_get_main (),
188 MODE_L3, gb->gb_uu_fwd_sw_if_index,
189 gb->gb_bd_index, L2_BD_PORT_TYPE_UU_FWD, 0, 0);
191 gbp_bridge_domain_db_remove (gb);
193 pool_put (gbp_bridge_domain_pool, gb);
198 gbp_bridge_domain_delete (u32 bd_id)
202 GBP_BD_DBG ("del: %d", bd_id);
203 gbi = gbp_bridge_domain_find (bd_id);
205 if (INDEX_INVALID != gbi)
207 GBP_BD_DBG ("del: %U", format_gbp_bridge_domain,
208 gbp_bridge_domain_get (gbi));
209 gbp_bridge_domain_unlock (gbi);
214 return (VNET_API_ERROR_NO_SUCH_ENTRY);
218 gbp_bridge_domain_walk (gbp_bridge_domain_cb_t cb, void *ctx)
220 gbp_bridge_domain_t *gbpe;
223 pool_foreach(gbpe, gbp_bridge_domain_pool,
231 static clib_error_t *
232 gbp_bridge_domain_cli (vlib_main_t * vm,
233 unformat_input_t * input, vlib_cli_command_t * cmd)
235 vnet_main_t *vnm = vnet_get_main ();
236 u32 uu_fwd_sw_if_index = ~0;
237 u32 bvi_sw_if_index = ~0;
241 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
243 if (unformat (input, "bvi %U", unformat_vnet_sw_interface,
244 vnm, &bvi_sw_if_index))
246 else if (unformat (input, "uu-flood %U", unformat_vnet_sw_interface,
247 vnm, &uu_fwd_sw_if_index))
249 else if (unformat (input, "add"))
251 else if (unformat (input, "del"))
253 else if (unformat (input, "bd %d", &bd_id))
260 return clib_error_return (0, "EPG-ID must be specified");
264 if (~0 == bvi_sw_if_index)
265 return clib_error_return (0, "interface must be specified");
267 gbp_bridge_domain_add_and_lock (bd_id, GBP_BD_FLAG_NONE,
268 bvi_sw_if_index, uu_fwd_sw_if_index);
271 gbp_bridge_domain_delete (bd_id);
277 * Configure a GBP bridge-domain
280 * @cliexstart{set gbp bridge-domain [del] bd <ID> bvi <interface> uu-flood <interface>}
284 VLIB_CLI_COMMAND (gbp_bridge_domain_cli_node, static) = {
285 .path = "gbp bridge-domain",
286 .short_help = "gbp bridge-domain [del] epg bd <ID> bvi <interface> uu-flood <interface>",
287 .function = gbp_bridge_domain_cli,
291 format_gbp_bridge_domain (u8 * s, va_list * args)
293 gbp_bridge_domain_t *gb = va_arg (*args, gbp_bridge_domain_t*);
294 vnet_main_t *vnm = vnet_get_main ();
297 s = format (s, "[%d] bd:[%d,%d], bvi:%U uu-flood:%U locks:%d",
298 gb - gbp_bridge_domain_pool,
301 format_vnet_sw_if_index_name, vnm, gb->gb_bvi_sw_if_index,
302 format_vnet_sw_if_index_name, vnm, gb->gb_uu_fwd_sw_if_index,
305 s = format (s, "NULL");
311 gbp_bridge_domain_show_one (gbp_bridge_domain_t *gb, void *ctx)
316 vlib_cli_output (vm, " %U",format_gbp_bridge_domain, gb);
321 static clib_error_t *
322 gbp_bridge_domain_show (vlib_main_t * vm,
323 unformat_input_t * input, vlib_cli_command_t * cmd)
325 vlib_cli_output (vm, "Bridge-Domains:");
326 gbp_bridge_domain_walk (gbp_bridge_domain_show_one, vm);
333 * Show Group Based Policy Bridge_Domains and derived information
336 * @cliexstart{show gbp bridge_domain}
340 VLIB_CLI_COMMAND (gbp_bridge_domain_show_node, static) = {
341 .path = "show gbp bridge-domain",
342 .short_help = "show gbp bridge-domain\n",
343 .function = gbp_bridge_domain_show,
347 static clib_error_t *
348 gbp_bridge_domain_init (vlib_main_t * vm)
350 gb_logger = vlib_log_register_class ("gbp", "bd");
355 VLIB_INIT_FUNCTION (gbp_bridge_domain_init);
358 * fd.io coding-style-patch-verification: ON
361 * eval: (c-set-style "gnu")