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_route_domain.h>
18 #include <plugins/gbp/gbp_endpoint.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;
39 * Map of BD index to contract scope
41 gbp_scope_t *gbp_scope_by_bd_index;
46 vlib_log_class_t gb_logger;
48 #define GBP_BD_DBG(...) \
49 vlib_log_debug (gb_logger, __VA_ARGS__);
52 gbp_bridge_domain_index (const gbp_bridge_domain_t * gbd)
54 return (gbd - gbp_bridge_domain_pool);
58 gbp_bridge_domain_lock (index_t i)
60 gbp_bridge_domain_t *gb;
62 gb = gbp_bridge_domain_get (i);
67 gbp_bridge_domain_get_bd_id (index_t gbdi)
69 gbp_bridge_domain_t *gb;
71 gb = gbp_bridge_domain_get (gbdi);
73 return (gb->gb_bd_id);
77 gbp_bridge_domain_find (u32 bd_id)
81 p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
86 return (INDEX_INVALID);
90 gbp_bridge_domain_find_and_lock (u32 bd_id)
94 p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
98 gbp_bridge_domain_lock (p[0]);
101 return (INDEX_INVALID);
105 gbp_bridge_domain_db_add (gbp_bridge_domain_t * gb)
107 index_t gbi = gb - gbp_bridge_domain_pool;
109 hash_set (gbp_bridge_domain_db.gbd_by_bd_id, gb->gb_bd_id, gbi);
110 vec_validate_init_empty (gbp_bridge_domain_db.gbd_by_bd_index,
111 gb->gb_bd_index, INDEX_INVALID);
112 gbp_bridge_domain_db.gbd_by_bd_index[gb->gb_bd_index] = gbi;
116 gbp_bridge_domain_db_remove (gbp_bridge_domain_t * gb)
118 hash_unset (gbp_bridge_domain_db.gbd_by_bd_id, gb->gb_bd_id);
119 gbp_bridge_domain_db.gbd_by_bd_index[gb->gb_bd_index] = INDEX_INVALID;
123 format_gbp_bridge_domain_flags (u8 * s, va_list * args)
125 gbp_bridge_domain_flags_t gf = va_arg (*args, gbp_bridge_domain_flags_t);
129 if (gf & GBP_BD_FLAG_DO_NOT_LEARN)
130 s = format (s, "do-not-learn ");
131 if (gf & GBP_BD_FLAG_UU_FWD_DROP)
132 s = format (s, "uu-fwd-drop ");
133 if (gf & GBP_BD_FLAG_MCAST_DROP)
134 s = format (s, "mcast-drop ");
135 if (gf & GBP_BD_FLAG_UCAST_ARP)
136 s = format (s, "ucast-arp ");
140 s = format (s, "none");
146 format_gbp_bridge_domain_ptr (u8 * s, va_list * args)
148 gbp_bridge_domain_t *gb = va_arg (*args, gbp_bridge_domain_t *);
149 vnet_main_t *vnm = vnet_get_main ();
152 s = format (s, "[%d] bd:[%d,%d], bvi:%U uu-flood:%U flags:%U locks:%d",
153 gb - gbp_bridge_domain_pool,
156 format_vnet_sw_if_index_name, vnm, gb->gb_bvi_sw_if_index,
157 format_vnet_sw_if_index_name, vnm, gb->gb_uu_fwd_sw_if_index,
158 format_gbp_bridge_domain_flags, gb->gb_flags, gb->gb_locks);
160 s = format (s, "NULL");
166 format_gbp_bridge_domain (u8 * s, va_list * args)
168 index_t gbi = va_arg (*args, index_t);
171 format (s, "%U", format_gbp_bridge_domain_ptr,
172 gbp_bridge_domain_get (gbi));
178 gbp_bridge_domain_itf_add (u32 sw_if_index, u32 bd_index,
179 l2_bd_port_type_t type)
181 set_int_l2_mode (vlib_get_main (), vnet_get_main (), MODE_L2_BRIDGE,
182 sw_if_index, bd_index, type, 0, 0);
184 * adding an interface to the bridge enable learning on the
185 * interface. Disable learning on the interface by default for gbp
188 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_LEARN, 0);
192 gbp_bridge_domain_itf_del (u32 sw_if_index, u32 bd_index,
193 l2_bd_port_type_t type)
195 set_int_l2_mode (vlib_get_main (), vnet_get_main (), MODE_L3, sw_if_index,
196 bd_index, type, 0, 0);
200 gbp_bridge_domain_add_and_lock (u32 bd_id,
202 gbp_bridge_domain_flags_t flags,
204 u32 uu_fwd_sw_if_index,
205 u32 bm_flood_sw_if_index)
207 gbp_bridge_domain_t *gb;
210 gbi = gbp_bridge_domain_find (bd_id);
212 if (INDEX_INVALID == gbi)
214 gbp_route_domain_t *gr;
217 bd_index = bd_find_index (&bd_main, bd_id);
220 return (VNET_API_ERROR_BD_NOT_MODIFIABLE);
222 bd_flags_t bd_flags = L2_NONE;
223 if (flags & GBP_BD_FLAG_UU_FWD_DROP)
224 bd_flags |= L2_UU_FLOOD;
225 if (flags & GBP_BD_FLAG_MCAST_DROP)
226 bd_flags |= L2_FLOOD;
228 pool_get (gbp_bridge_domain_pool, gb);
229 memset (gb, 0, sizeof (*gb));
231 gb->gb_bd_id = bd_id;
232 gb->gb_bd_index = bd_index;
233 gb->gb_uu_fwd_sw_if_index = uu_fwd_sw_if_index;
234 gb->gb_bvi_sw_if_index = bvi_sw_if_index;
235 gb->gb_bm_flood_sw_if_index = bm_flood_sw_if_index;
237 gb->gb_flags = flags;
238 gb->gb_rdi = gbp_route_domain_find_and_lock (rd_id);
241 * set the scope from the BD's RD's scope
243 gr = gbp_route_domain_get (gb->gb_rdi);
244 vec_validate (gbp_scope_by_bd_index, gb->gb_bd_index);
245 gbp_scope_by_bd_index[gb->gb_bd_index] = gr->grd_scope;
248 * Set the BVI and uu-flood interfaces into the BD
250 gbp_bridge_domain_itf_add (gb->gb_bvi_sw_if_index, bd_index,
251 L2_BD_PORT_TYPE_BVI);
253 if ((!(flags & GBP_BD_FLAG_UU_FWD_DROP)
254 || (flags & GBP_BD_FLAG_UCAST_ARP))
255 && ~0 != gb->gb_uu_fwd_sw_if_index)
256 gbp_bridge_domain_itf_add (gb->gb_uu_fwd_sw_if_index, bd_index,
257 L2_BD_PORT_TYPE_UU_FWD);
258 if (!(flags & GBP_BD_FLAG_MCAST_DROP)
259 && ~0 != gb->gb_bm_flood_sw_if_index)
261 gbp_bridge_domain_itf_add (gb->gb_bm_flood_sw_if_index, bd_index,
262 L2_BD_PORT_TYPE_NORMAL);
263 gbp_learn_enable (gb->gb_bm_flood_sw_if_index, GBP_LEARN_MODE_L2);
267 * unset any flag(s) set above
269 bd_set_flags (vlib_get_main (), bd_index, bd_flags, 0);
271 if (flags & GBP_BD_FLAG_UCAST_ARP)
273 bd_flags = L2_ARP_UFWD;
274 bd_set_flags (vlib_get_main (), bd_index, bd_flags, 1);
278 * Add the BVI's MAC to the L2FIB
280 l2fib_add_entry (vnet_sw_interface_get_hw_address
281 (vnet_get_main (), gb->gb_bvi_sw_if_index),
282 gb->gb_bd_index, gb->gb_bvi_sw_if_index,
283 (L2FIB_ENTRY_RESULT_FLAG_STATIC |
284 L2FIB_ENTRY_RESULT_FLAG_BVI));
286 gbp_bridge_domain_db_add (gb);
290 gb = gbp_bridge_domain_get (gbi);
294 GBP_BD_DBG ("add: %U", format_gbp_bridge_domain_ptr, gb);
300 gbp_bridge_domain_unlock (index_t index)
302 gbp_bridge_domain_t *gb;
304 gb = gbp_bridge_domain_get (index);
308 if (0 == gb->gb_locks)
310 GBP_BD_DBG ("destroy: %U", format_gbp_bridge_domain_ptr, gb);
312 l2fib_del_entry (vnet_sw_interface_get_hw_address
313 (vnet_get_main (), gb->gb_bvi_sw_if_index),
314 gb->gb_bd_index, gb->gb_bvi_sw_if_index);
316 gbp_bridge_domain_itf_del (gb->gb_bvi_sw_if_index, gb->gb_bd_index,
317 L2_BD_PORT_TYPE_BVI);
318 if (~0 != gb->gb_uu_fwd_sw_if_index)
319 gbp_bridge_domain_itf_del (gb->gb_uu_fwd_sw_if_index, gb->gb_bd_index,
320 L2_BD_PORT_TYPE_UU_FWD);
321 if (~0 != gb->gb_bm_flood_sw_if_index)
323 gbp_bridge_domain_itf_del (gb->gb_bm_flood_sw_if_index,
324 gb->gb_bd_index, L2_BD_PORT_TYPE_NORMAL);
325 gbp_learn_enable (gb->gb_bm_flood_sw_if_index, GBP_LEARN_MODE_L2);
328 gbp_bridge_domain_db_remove (gb);
329 gbp_route_domain_unlock (gb->gb_rdi);
331 pool_put (gbp_bridge_domain_pool, gb);
336 gbp_bridge_domain_delete (u32 bd_id)
340 GBP_BD_DBG ("del: %d", bd_id);
341 gbi = gbp_bridge_domain_find (bd_id);
343 if (INDEX_INVALID != gbi)
345 GBP_BD_DBG ("del: %U", format_gbp_bridge_domain, gbi);
346 gbp_bridge_domain_unlock (gbi);
351 return (VNET_API_ERROR_NO_SUCH_ENTRY);
355 gbp_bridge_domain_walk (gbp_bridge_domain_cb_t cb, void *ctx)
357 gbp_bridge_domain_t *gbpe;
360 pool_foreach(gbpe, gbp_bridge_domain_pool,
368 static clib_error_t *
369 gbp_bridge_domain_cli (vlib_main_t * vm,
370 unformat_input_t * input, vlib_cli_command_t * cmd)
372 vnet_main_t *vnm = vnet_get_main ();
373 gbp_bridge_domain_flags_t flags;
374 u32 bm_flood_sw_if_index = ~0;
375 u32 uu_fwd_sw_if_index = ~0;
376 u32 bd_id = ~0, rd_id = ~0;
377 u32 bvi_sw_if_index = ~0;
380 flags = GBP_BD_FLAG_NONE;
382 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
384 if (unformat (input, "bvi %U", unformat_vnet_sw_interface,
385 vnm, &bvi_sw_if_index))
387 else if (unformat (input, "uu-fwd %U", unformat_vnet_sw_interface,
388 vnm, &uu_fwd_sw_if_index))
390 else if (unformat (input, "bm-flood %U", unformat_vnet_sw_interface,
391 vnm, &bm_flood_sw_if_index))
393 else if (unformat (input, "add"))
395 else if (unformat (input, "del"))
397 else if (unformat (input, "flags %d", &flags))
399 else if (unformat (input, "bd %d", &bd_id))
401 else if (unformat (input, "rd %d", &rd_id))
408 return clib_error_return (0, "BD-ID must be specified");
410 return clib_error_return (0, "RD-ID must be specified");
414 if (~0 == bvi_sw_if_index)
415 return clib_error_return (0, "interface must be specified");
417 gbp_bridge_domain_add_and_lock (bd_id, rd_id,
421 bm_flood_sw_if_index);
424 gbp_bridge_domain_delete (bd_id);
430 * Configure a GBP bridge-domain
433 * @cliexstart{gbp bridge-domain [del] bd <ID> bvi <interface> [uu-fwd <interface>] [bm-flood <interface>] [flags <flags>]}
437 VLIB_CLI_COMMAND (gbp_bridge_domain_cli_node, static) = {
438 .path = "gbp bridge-domain",
439 .short_help = "gbp bridge-domain [del] bd <ID> bvi <interface> [uu-fwd <interface>] [bm-flood <interface>] [flags <flags>]",
440 .function = gbp_bridge_domain_cli,
444 gbp_bridge_domain_show_one (gbp_bridge_domain_t *gb, void *ctx)
449 vlib_cli_output (vm, " %U", format_gbp_bridge_domain_ptr, gb);
454 static clib_error_t *
455 gbp_bridge_domain_show (vlib_main_t * vm,
456 unformat_input_t * input, vlib_cli_command_t * cmd)
458 vlib_cli_output (vm, "Bridge-Domains:");
459 gbp_bridge_domain_walk (gbp_bridge_domain_show_one, vm);
466 * Show Group Based Policy Bridge_Domains and derived information
469 * @cliexstart{show gbp bridge_domain}
473 VLIB_CLI_COMMAND (gbp_bridge_domain_show_node, static) = {
474 .path = "show gbp bridge-domain",
475 .short_help = "show gbp bridge-domain\n",
476 .function = gbp_bridge_domain_show,
480 static clib_error_t *
481 gbp_bridge_domain_init (vlib_main_t * vm)
483 gb_logger = vlib_log_register_class ("gbp", "bd");
488 VLIB_INIT_FUNCTION (gbp_bridge_domain_init);
491 * fd.io coding-style-patch-verification: ON
494 * eval: (c-set-style "gnu")