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_learn.h>
20 #include <vnet/dpo/dvr_dpo.h>
21 #include <vnet/fib/fib_table.h>
22 #include <vnet/l2/l2_input.h>
23 #include <vnet/l2/feat_bitmap.h>
24 #include <vnet/l2/l2_bvi.h>
25 #include <vnet/l2/l2_fib.h>
28 * Pool of GBP bridge_domains
30 gbp_bridge_domain_t *gbp_bridge_domain_pool;
33 * DB of bridge_domains
35 gbp_bridge_domain_db_t gbp_bridge_domain_db;
40 vlib_log_class_t gb_logger;
42 #define GBP_BD_DBG(...) \
43 vlib_log_debug (gb_logger, __VA_ARGS__);
46 gbp_bridge_domain_index (const gbp_bridge_domain_t * gbd)
48 return (gbd - gbp_bridge_domain_pool);
52 gbp_bridge_domain_lock (index_t i)
54 gbp_bridge_domain_t *gb;
56 gb = gbp_bridge_domain_get (i);
61 gbp_bridge_domain_get_bd_id (index_t gbdi)
63 gbp_bridge_domain_t *gb;
65 gb = gbp_bridge_domain_get (gbdi);
67 return (gb->gb_bd_id);
71 gbp_bridge_domain_find (u32 bd_id)
75 p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
80 return (INDEX_INVALID);
84 gbp_bridge_domain_find_and_lock (u32 bd_id)
88 p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
92 gbp_bridge_domain_lock (p[0]);
95 return (INDEX_INVALID);
99 gbp_bridge_domain_db_add (gbp_bridge_domain_t * gb)
101 index_t gbi = gb - gbp_bridge_domain_pool;
103 hash_set (gbp_bridge_domain_db.gbd_by_bd_id, gb->gb_bd_id, gbi);
104 vec_validate_init_empty (gbp_bridge_domain_db.gbd_by_bd_index,
105 gb->gb_bd_index, INDEX_INVALID);
106 gbp_bridge_domain_db.gbd_by_bd_index[gb->gb_bd_index] = gbi;
110 gbp_bridge_domain_db_remove (gbp_bridge_domain_t * gb)
112 hash_unset (gbp_bridge_domain_db.gbd_by_bd_id, gb->gb_bd_id);
113 gbp_bridge_domain_db.gbd_by_bd_index[gb->gb_bd_index] = INDEX_INVALID;
117 format_gbp_bridge_domain_flags (u8 * s, va_list * args)
119 gbp_bridge_domain_flags_t gf = va_arg (*args, gbp_bridge_domain_flags_t);
123 if (gf & GBP_BD_FLAG_DO_NOT_LEARN)
124 s = format (s, "do-not-learn ");
125 if (gf & GBP_BD_FLAG_UU_FWD_DROP)
126 s = format (s, "uu-fwd-drop ");
127 if (gf & GBP_BD_FLAG_MCAST_DROP)
128 s = format (s, "mcast-drop ");
129 if (gf & GBP_BD_FLAG_UCAST_ARP)
130 s = format (s, "ucast-arp ");
134 s = format (s, "none");
140 format_gbp_bridge_domain_ptr (u8 * s, va_list * args)
142 gbp_bridge_domain_t *gb = va_arg (*args, gbp_bridge_domain_t *);
143 vnet_main_t *vnm = vnet_get_main ();
146 s = format (s, "[%d] bd:[%d,%d], bvi:%U uu-flood:%U flags:%U locks:%d",
147 gb - gbp_bridge_domain_pool,
150 format_vnet_sw_if_index_name, vnm, gb->gb_bvi_sw_if_index,
151 format_vnet_sw_if_index_name, vnm, gb->gb_uu_fwd_sw_if_index,
152 format_gbp_bridge_domain_flags, gb->gb_flags, gb->gb_locks);
154 s = format (s, "NULL");
160 format_gbp_bridge_domain (u8 * s, va_list * args)
162 index_t gbi = va_arg (*args, index_t);
165 format (s, "%U", format_gbp_bridge_domain_ptr,
166 gbp_bridge_domain_get (gbi));
172 gbp_bridge_domain_add_and_lock (u32 bd_id,
173 gbp_bridge_domain_flags_t flags,
175 u32 uu_fwd_sw_if_index,
176 u32 bm_flood_sw_if_index)
178 gbp_bridge_domain_t *gb;
181 gbi = gbp_bridge_domain_find (bd_id);
183 if (INDEX_INVALID == gbi)
187 bd_index = bd_find_index (&bd_main, bd_id);
190 return (VNET_API_ERROR_BD_NOT_MODIFIABLE);
192 bd_flags_t bd_flags = L2_LEARN;
193 if (flags & GBP_BD_FLAG_UU_FWD_DROP)
194 bd_flags |= L2_UU_FLOOD;
195 if (flags & GBP_BD_FLAG_MCAST_DROP)
196 bd_flags |= L2_FLOOD;
198 pool_get (gbp_bridge_domain_pool, gb);
199 memset (gb, 0, sizeof (*gb));
201 gb->gb_bd_id = bd_id;
202 gb->gb_bd_index = bd_index;
203 gb->gb_uu_fwd_sw_if_index = uu_fwd_sw_if_index;
204 gb->gb_bvi_sw_if_index = bvi_sw_if_index;
205 gb->gb_bm_flood_sw_if_index = bm_flood_sw_if_index;
207 gb->gb_flags = flags;
210 * Set the BVI and uu-flood interfaces into the BD
212 set_int_l2_mode (vlib_get_main (), vnet_get_main (),
213 MODE_L2_BRIDGE, gb->gb_bvi_sw_if_index,
214 bd_index, L2_BD_PORT_TYPE_BVI, 0, 0);
216 if (!(flags & GBP_BD_FLAG_UU_FWD_DROP)
217 && ~0 != gb->gb_uu_fwd_sw_if_index)
219 set_int_l2_mode (vlib_get_main (), vnet_get_main (),
220 MODE_L2_BRIDGE, gb->gb_uu_fwd_sw_if_index,
221 bd_index, L2_BD_PORT_TYPE_UU_FWD, 0, 0);
223 if (!(flags & GBP_BD_FLAG_MCAST_DROP)
224 && ~0 != gb->gb_bm_flood_sw_if_index)
226 set_int_l2_mode (vlib_get_main (), vnet_get_main (),
227 MODE_L2_BRIDGE, gb->gb_bm_flood_sw_if_index,
228 bd_index, L2_BD_PORT_TYPE_NORMAL, 0, 0);
229 gbp_learn_enable (gb->gb_bm_flood_sw_if_index, GBP_LEARN_MODE_L2);
233 * unset learning in the bridge + any flag(s) set above
235 bd_set_flags (vlib_get_main (), bd_index, bd_flags, 0);
238 * Add the BVI's MAC to the L2FIB
240 l2fib_add_entry (vnet_sw_interface_get_hw_address
241 (vnet_get_main (), gb->gb_bvi_sw_if_index),
242 gb->gb_bd_index, gb->gb_bvi_sw_if_index,
243 (L2FIB_ENTRY_RESULT_FLAG_STATIC |
244 L2FIB_ENTRY_RESULT_FLAG_BVI));
246 gbp_bridge_domain_db_add (gb);
250 gb = gbp_bridge_domain_get (gbi);
254 GBP_BD_DBG ("add: %U", format_gbp_bridge_domain_ptr, gb);
260 gbp_bridge_domain_unlock (index_t index)
262 gbp_bridge_domain_t *gb;
264 gb = gbp_bridge_domain_get (index);
268 if (0 == gb->gb_locks)
270 GBP_BD_DBG ("destroy: %U", format_gbp_bridge_domain_ptr, gb);
272 l2fib_del_entry (vnet_sw_interface_get_hw_address
273 (vnet_get_main (), gb->gb_bvi_sw_if_index),
274 gb->gb_bd_index, gb->gb_bvi_sw_if_index);
276 set_int_l2_mode (vlib_get_main (), vnet_get_main (),
277 MODE_L3, gb->gb_bvi_sw_if_index,
278 gb->gb_bd_index, L2_BD_PORT_TYPE_BVI, 0, 0);
279 if (~0 != gb->gb_uu_fwd_sw_if_index)
281 set_int_l2_mode (vlib_get_main (), vnet_get_main (),
282 MODE_L3, gb->gb_uu_fwd_sw_if_index,
283 gb->gb_bd_index, L2_BD_PORT_TYPE_UU_FWD, 0, 0);
285 if (~0 != gb->gb_bm_flood_sw_if_index)
287 set_int_l2_mode (vlib_get_main (), vnet_get_main (),
288 MODE_L3, gb->gb_bm_flood_sw_if_index,
289 gb->gb_bd_index, L2_BD_PORT_TYPE_NORMAL, 0, 0);
290 gbp_learn_enable (gb->gb_bm_flood_sw_if_index, GBP_LEARN_MODE_L2);
293 gbp_bridge_domain_db_remove (gb);
295 pool_put (gbp_bridge_domain_pool, gb);
300 gbp_bridge_domain_delete (u32 bd_id)
304 GBP_BD_DBG ("del: %d", bd_id);
305 gbi = gbp_bridge_domain_find (bd_id);
307 if (INDEX_INVALID != gbi)
309 GBP_BD_DBG ("del: %U", format_gbp_bridge_domain, gbi);
310 gbp_bridge_domain_unlock (gbi);
315 return (VNET_API_ERROR_NO_SUCH_ENTRY);
319 gbp_bridge_domain_walk (gbp_bridge_domain_cb_t cb, void *ctx)
321 gbp_bridge_domain_t *gbpe;
324 pool_foreach(gbpe, gbp_bridge_domain_pool,
332 static clib_error_t *
333 gbp_bridge_domain_cli (vlib_main_t * vm,
334 unformat_input_t * input, vlib_cli_command_t * cmd)
336 vnet_main_t *vnm = vnet_get_main ();
337 gbp_bridge_domain_flags_t flags;
338 u32 bm_flood_sw_if_index = ~0;
339 u32 uu_fwd_sw_if_index = ~0;
340 u32 bvi_sw_if_index = ~0;
344 flags = GBP_BD_FLAG_NONE;
346 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
348 if (unformat (input, "bvi %U", unformat_vnet_sw_interface,
349 vnm, &bvi_sw_if_index))
351 else if (unformat (input, "uu-fwd %U", unformat_vnet_sw_interface,
352 vnm, &uu_fwd_sw_if_index))
354 else if (unformat (input, "bm-flood %U", unformat_vnet_sw_interface,
355 vnm, &bm_flood_sw_if_index))
357 else if (unformat (input, "add"))
359 else if (unformat (input, "del"))
361 else if (unformat (input, "flags %d", &flags))
363 else if (unformat (input, "bd %d", &bd_id))
370 return clib_error_return (0, "BD-ID must be specified");
374 if (~0 == bvi_sw_if_index)
375 return clib_error_return (0, "interface must be specified");
377 gbp_bridge_domain_add_and_lock (bd_id, flags,
380 bm_flood_sw_if_index);
383 gbp_bridge_domain_delete (bd_id);
389 * Configure a GBP bridge-domain
392 * @cliexstart{set gbp bridge-domain [del] bd <ID> bvi <interface> uu-flood <interface>}
396 VLIB_CLI_COMMAND (gbp_bridge_domain_cli_node, static) = {
397 .path = "gbp bridge-domain",
398 .short_help = "gbp bridge-domain [del] bd <ID> bvi <interface>"
399 " uu-flood <interface> [flags <flags>]",
400 .function = gbp_bridge_domain_cli,
404 gbp_bridge_domain_show_one (gbp_bridge_domain_t *gb, void *ctx)
409 vlib_cli_output (vm, " %U", format_gbp_bridge_domain_ptr, gb);
414 static clib_error_t *
415 gbp_bridge_domain_show (vlib_main_t * vm,
416 unformat_input_t * input, vlib_cli_command_t * cmd)
418 vlib_cli_output (vm, "Bridge-Domains:");
419 gbp_bridge_domain_walk (gbp_bridge_domain_show_one, vm);
426 * Show Group Based Policy Bridge_Domains and derived information
429 * @cliexstart{show gbp bridge_domain}
433 VLIB_CLI_COMMAND (gbp_bridge_domain_show_node, static) = {
434 .path = "show gbp bridge-domain",
435 .short_help = "show gbp bridge-domain\n",
436 .function = gbp_bridge_domain_show,
440 static clib_error_t *
441 gbp_bridge_domain_init (vlib_main_t * vm)
443 gb_logger = vlib_log_register_class ("gbp", "bd");
448 VLIB_INIT_FUNCTION (gbp_bridge_domain_init);
451 * fd.io coding-style-patch-verification: ON
454 * eval: (c-set-style "gnu")