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>
18 #include <plugins/gbp/gbp_sclass.h>
19 #include <plugins/gbp/gbp_learn.h>
21 #include <vnet/dpo/dvr_dpo.h>
22 #include <vnet/fib/fib_table.h>
23 #include <vnet/l2/l2_input.h>
24 #include <vnet/l2/feat_bitmap.h>
25 #include <vnet/l2/l2_bvi.h>
26 #include <vnet/l2/l2_fib.h>
29 * Pool of GBP bridge_domains
31 gbp_bridge_domain_t *gbp_bridge_domain_pool;
34 * DB of bridge_domains
36 gbp_bridge_domain_db_t gbp_bridge_domain_db;
41 vlib_log_class_t gb_logger;
43 #define GBP_BD_DBG(...) \
44 vlib_log_debug (gb_logger, __VA_ARGS__);
47 gbp_bridge_domain_index (const gbp_bridge_domain_t * gbd)
49 return (gbd - gbp_bridge_domain_pool);
53 gbp_bridge_domain_lock (index_t i)
55 gbp_bridge_domain_t *gb;
57 gb = gbp_bridge_domain_get (i);
62 gbp_bridge_domain_get_bd_id (index_t gbdi)
64 gbp_bridge_domain_t *gb;
66 gb = gbp_bridge_domain_get (gbdi);
68 return (gb->gb_bd_id);
72 gbp_bridge_domain_find (u32 bd_id)
76 p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
81 return (INDEX_INVALID);
85 gbp_bridge_domain_find_and_lock (u32 bd_id)
89 p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
93 gbp_bridge_domain_lock (p[0]);
96 return (INDEX_INVALID);
100 gbp_bridge_domain_db_add (gbp_bridge_domain_t * gb)
102 index_t gbi = gb - gbp_bridge_domain_pool;
104 hash_set (gbp_bridge_domain_db.gbd_by_bd_id, gb->gb_bd_id, gbi);
105 vec_validate_init_empty (gbp_bridge_domain_db.gbd_by_bd_index,
106 gb->gb_bd_index, INDEX_INVALID);
107 gbp_bridge_domain_db.gbd_by_bd_index[gb->gb_bd_index] = gbi;
111 gbp_bridge_domain_db_remove (gbp_bridge_domain_t * gb)
113 hash_unset (gbp_bridge_domain_db.gbd_by_bd_id, gb->gb_bd_id);
114 gbp_bridge_domain_db.gbd_by_bd_index[gb->gb_bd_index] = INDEX_INVALID;
118 format_gbp_bridge_domain_flags (u8 * s, va_list * args)
120 gbp_bridge_domain_flags_t gf = va_arg (*args, gbp_bridge_domain_flags_t);
124 if (gf & GBP_BD_FLAG_DO_NOT_LEARN)
125 s = format (s, "do-not-learn");
129 s = format (s, "none");
135 format_gbp_bridge_domain_ptr (u8 * s, va_list * args)
137 gbp_bridge_domain_t *gb = va_arg (*args, gbp_bridge_domain_t *);
138 vnet_main_t *vnm = vnet_get_main ();
141 s = format (s, "[%d] bd:[%d,%d], bvi:%U uu-flood:%U flags:%U locks:%d",
142 gb - gbp_bridge_domain_pool,
145 format_vnet_sw_if_index_name, vnm, gb->gb_bvi_sw_if_index,
146 format_vnet_sw_if_index_name, vnm, gb->gb_uu_fwd_sw_if_index,
147 format_gbp_bridge_domain_flags, gb->gb_flags, gb->gb_locks);
149 s = format (s, "NULL");
155 format_gbp_bridge_domain (u8 * s, va_list * args)
157 index_t gbi = va_arg (*args, index_t);
160 format (s, "%U", format_gbp_bridge_domain_ptr,
161 gbp_bridge_domain_get (gbi));
167 gbp_bridge_domain_add_and_lock (u32 bd_id,
168 gbp_bridge_domain_flags_t flags,
170 u32 uu_fwd_sw_if_index,
171 u32 bm_flood_sw_if_index)
173 gbp_bridge_domain_t *gb;
176 gbi = gbp_bridge_domain_find (bd_id);
178 if (INDEX_INVALID == gbi)
182 bd_index = bd_find_index (&bd_main, bd_id);
185 return (VNET_API_ERROR_BD_NOT_MODIFIABLE);
188 * unset learning in the bridge
190 bd_set_flags (vlib_get_main (), bd_index, L2_LEARN, 0);
192 pool_get (gbp_bridge_domain_pool, gb);
193 memset (gb, 0, sizeof (*gb));
195 gb->gb_bd_id = bd_id;
196 gb->gb_bd_index = bd_index;
197 gb->gb_uu_fwd_sw_if_index = uu_fwd_sw_if_index;
198 gb->gb_bvi_sw_if_index = bvi_sw_if_index;
199 gb->gb_bm_flood_sw_if_index = bm_flood_sw_if_index;
201 gb->gb_flags = flags;
204 * Set the BVI and uu-flood interfaces into the BD
206 set_int_l2_mode (vlib_get_main (), vnet_get_main (),
207 MODE_L2_BRIDGE, gb->gb_bvi_sw_if_index,
208 bd_index, L2_BD_PORT_TYPE_BVI, 0, 0);
209 if (~0 != gb->gb_uu_fwd_sw_if_index)
211 set_int_l2_mode (vlib_get_main (), vnet_get_main (),
212 MODE_L2_BRIDGE, gb->gb_uu_fwd_sw_if_index,
213 bd_index, L2_BD_PORT_TYPE_UU_FWD, 0, 0);
214 gbp_sclass_enable_l2 (gb->gb_uu_fwd_sw_if_index);
216 if (~0 != gb->gb_bm_flood_sw_if_index)
218 set_int_l2_mode (vlib_get_main (), vnet_get_main (),
219 MODE_L2_BRIDGE, gb->gb_bm_flood_sw_if_index,
220 bd_index, L2_BD_PORT_TYPE_NORMAL, 0, 0);
221 gbp_sclass_enable_l2 (gb->gb_bm_flood_sw_if_index);
222 gbp_learn_enable (gb->gb_bm_flood_sw_if_index, GBP_LEARN_MODE_L2);
226 * Add the BVI's MAC to the L2FIB
228 l2fib_add_entry (vnet_sw_interface_get_hw_address
229 (vnet_get_main (), gb->gb_bvi_sw_if_index),
230 gb->gb_bd_index, gb->gb_bvi_sw_if_index,
231 (L2FIB_ENTRY_RESULT_FLAG_STATIC |
232 L2FIB_ENTRY_RESULT_FLAG_BVI));
234 gbp_bridge_domain_db_add (gb);
238 gb = gbp_bridge_domain_get (gbi);
242 GBP_BD_DBG ("add: %U", format_gbp_bridge_domain_ptr, gb);
248 gbp_bridge_domain_unlock (index_t index)
250 gbp_bridge_domain_t *gb;
252 gb = gbp_bridge_domain_get (index);
256 if (0 == gb->gb_locks)
258 GBP_BD_DBG ("destroy: %U", format_gbp_bridge_domain_ptr, gb);
260 l2fib_del_entry (vnet_sw_interface_get_hw_address
261 (vnet_get_main (), gb->gb_bvi_sw_if_index),
262 gb->gb_bd_index, gb->gb_bvi_sw_if_index);
264 set_int_l2_mode (vlib_get_main (), vnet_get_main (),
265 MODE_L3, gb->gb_bvi_sw_if_index,
266 gb->gb_bd_index, L2_BD_PORT_TYPE_BVI, 0, 0);
267 if (~0 != gb->gb_uu_fwd_sw_if_index)
269 set_int_l2_mode (vlib_get_main (), vnet_get_main (),
270 MODE_L3, gb->gb_uu_fwd_sw_if_index,
271 gb->gb_bd_index, L2_BD_PORT_TYPE_UU_FWD, 0, 0);
272 gbp_sclass_disable_l2 (gb->gb_uu_fwd_sw_if_index);
274 if (~0 != gb->gb_bm_flood_sw_if_index)
276 set_int_l2_mode (vlib_get_main (), vnet_get_main (),
277 MODE_L3, gb->gb_bm_flood_sw_if_index,
278 gb->gb_bd_index, L2_BD_PORT_TYPE_NORMAL, 0, 0);
279 gbp_sclass_disable_l2 (gb->gb_bm_flood_sw_if_index);
280 gbp_learn_enable (gb->gb_bm_flood_sw_if_index, GBP_LEARN_MODE_L2);
283 gbp_bridge_domain_db_remove (gb);
285 pool_put (gbp_bridge_domain_pool, gb);
290 gbp_bridge_domain_delete (u32 bd_id)
294 GBP_BD_DBG ("del: %d", bd_id);
295 gbi = gbp_bridge_domain_find (bd_id);
297 if (INDEX_INVALID != gbi)
299 GBP_BD_DBG ("del: %U", format_gbp_bridge_domain, gbi);
300 gbp_bridge_domain_unlock (gbi);
305 return (VNET_API_ERROR_NO_SUCH_ENTRY);
309 gbp_bridge_domain_walk (gbp_bridge_domain_cb_t cb, void *ctx)
311 gbp_bridge_domain_t *gbpe;
314 pool_foreach(gbpe, gbp_bridge_domain_pool,
322 static clib_error_t *
323 gbp_bridge_domain_cli (vlib_main_t * vm,
324 unformat_input_t * input, vlib_cli_command_t * cmd)
326 vnet_main_t *vnm = vnet_get_main ();
327 u32 bm_flood_sw_if_index = ~0;
328 u32 uu_fwd_sw_if_index = ~0;
329 u32 bvi_sw_if_index = ~0;
333 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
335 if (unformat (input, "bvi %U", unformat_vnet_sw_interface,
336 vnm, &bvi_sw_if_index))
338 else if (unformat (input, "uu-fwd %U", unformat_vnet_sw_interface,
339 vnm, &uu_fwd_sw_if_index))
341 else if (unformat (input, "bm-flood %U", unformat_vnet_sw_interface,
342 vnm, &bm_flood_sw_if_index))
344 else if (unformat (input, "add"))
346 else if (unformat (input, "del"))
348 else if (unformat (input, "bd %d", &bd_id))
355 return clib_error_return (0, "EPG-ID must be specified");
359 if (~0 == bvi_sw_if_index)
360 return clib_error_return (0, "interface must be specified");
362 gbp_bridge_domain_add_and_lock (bd_id, GBP_BD_FLAG_NONE,
365 bm_flood_sw_if_index);
368 gbp_bridge_domain_delete (bd_id);
374 * Configure a GBP bridge-domain
377 * @cliexstart{set gbp bridge-domain [del] bd <ID> bvi <interface> uu-flood <interface>}
381 VLIB_CLI_COMMAND (gbp_bridge_domain_cli_node, static) = {
382 .path = "gbp bridge-domain",
383 .short_help = "gbp bridge-domain [del] epg bd <ID> bvi <interface> uu-flood <interface>",
384 .function = gbp_bridge_domain_cli,
388 gbp_bridge_domain_show_one (gbp_bridge_domain_t *gb, void *ctx)
393 vlib_cli_output (vm, " %U", format_gbp_bridge_domain_ptr, gb);
398 static clib_error_t *
399 gbp_bridge_domain_show (vlib_main_t * vm,
400 unformat_input_t * input, vlib_cli_command_t * cmd)
402 vlib_cli_output (vm, "Bridge-Domains:");
403 gbp_bridge_domain_walk (gbp_bridge_domain_show_one, vm);
410 * Show Group Based Policy Bridge_Domains and derived information
413 * @cliexstart{show gbp bridge_domain}
417 VLIB_CLI_COMMAND (gbp_bridge_domain_show_node, static) = {
418 .path = "show gbp bridge-domain",
419 .short_help = "show gbp bridge-domain\n",
420 .function = gbp_bridge_domain_show,
424 static clib_error_t *
425 gbp_bridge_domain_init (vlib_main_t * vm)
427 gb_logger = vlib_log_register_class ("gbp", "bd");
432 VLIB_INIT_FUNCTION (gbp_bridge_domain_init);
435 * fd.io coding-style-patch-verification: ON
438 * eval: (c-set-style "gnu")