FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
gbp_bridge_domain.c
Go to the documentation of this file.
1 /*
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:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 
19 #include <plugins/gbp/gbp_learn.h>
20 #include <plugins/gbp/gbp_itf.h>
21 
22 #include <vnet/dpo/dvr_dpo.h>
23 #include <vnet/fib/fib_table.h>
24 #include <vnet/l2/l2_input.h>
25 #include <vnet/l2/feat_bitmap.h>
26 #include <vnet/l2/l2_bvi.h>
27 #include <vnet/l2/l2_fib.h>
28 
29 /**
30  * Pool of GBP bridge_domains
31  */
33 
34 /**
35  * DB of bridge_domains
36  */
38 
39 /**
40  * Map of BD index to contract scope
41  */
43 
44 /**
45  * logger
46  */
48 
49 #define GBP_BD_DBG(...) \
50  vlib_log_debug (gb_logger, __VA_ARGS__);
51 
52 index_t
54 {
55  return (gbd - gbp_bridge_domain_pool);
56 }
57 
58 static void
60 {
62 
63  gb = gbp_bridge_domain_get (i);
64  gb->gb_locks++;
65 }
66 
67 u32
69 {
71 
72  gb = gbp_bridge_domain_get (gbdi);
73 
74  return (gb->gb_bd_id);
75 }
76 
77 static index_t
79 {
80  uword *p;
81 
82  p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
83 
84  if (NULL != p)
85  return p[0];
86 
87  return (INDEX_INVALID);
88 }
89 
90 index_t
92 {
93  uword *p;
94 
95  p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
96 
97  if (NULL != p)
98  {
100  return p[0];
101  }
102  return (INDEX_INVALID);
103 }
104 
105 static void
107 {
108  index_t gbi = gb - gbp_bridge_domain_pool;
109 
110  hash_set (gbp_bridge_domain_db.gbd_by_bd_id, gb->gb_bd_id, gbi);
111  vec_validate_init_empty (gbp_bridge_domain_db.gbd_by_bd_index,
113  gbp_bridge_domain_db.gbd_by_bd_index[gb->gb_bd_index] = gbi;
114 }
115 
116 static void
118 {
119  hash_unset (gbp_bridge_domain_db.gbd_by_bd_id, gb->gb_bd_id);
120  gbp_bridge_domain_db.gbd_by_bd_index[gb->gb_bd_index] = INDEX_INVALID;
121 }
122 
123 u8 *
124 format_gbp_bridge_domain_flags (u8 * s, va_list * args)
125 {
127 
128  if (gf)
129  {
130  if (gf & GBP_BD_FLAG_DO_NOT_LEARN)
131  s = format (s, "do-not-learn ");
132  if (gf & GBP_BD_FLAG_UU_FWD_DROP)
133  s = format (s, "uu-fwd-drop ");
134  if (gf & GBP_BD_FLAG_MCAST_DROP)
135  s = format (s, "mcast-drop ");
136  if (gf & GBP_BD_FLAG_UCAST_ARP)
137  s = format (s, "ucast-arp ");
138  }
139  else
140  {
141  s = format (s, "none");
142  }
143  return (s);
144 }
145 
146 static u8 *
147 format_gbp_bridge_domain_ptr (u8 * s, va_list * args)
148 {
149  gbp_bridge_domain_t *gb = va_arg (*args, gbp_bridge_domain_t *);
150  vnet_main_t *vnm = vnet_get_main ();
151 
152  if (NULL != gb)
153  s =
154  format (s,
155  "[%d] bd:[%d,%d], bvi:%U uu-flood:%U bm-flood:%U flags:%U locks:%d",
156  gb - gbp_bridge_domain_pool, gb->gb_bd_id, gb->gb_bd_index,
161  else
162  s = format (s, "NULL");
163 
164  return (s);
165 }
166 
167 u8 *
168 format_gbp_bridge_domain (u8 * s, va_list * args)
169 {
170  index_t gbi = va_arg (*args, index_t);
171 
172  s =
174  gbp_bridge_domain_get (gbi));
175 
176  return (s);
177 }
178 
179 int
181  u32 rd_id,
186 {
188  index_t gbi;
189 
190  gbi = gbp_bridge_domain_find (bd_id);
191 
192  if (INDEX_INVALID == gbi)
193  {
194  gbp_route_domain_t *gr;
195  u32 bd_index;
196 
197  bd_index = bd_find_index (&bd_main, bd_id);
198 
199  if (~0 == bd_index)
200  return (VNET_API_ERROR_BD_NOT_MODIFIABLE);
201 
203  if (flags & GBP_BD_FLAG_UU_FWD_DROP)
204  bd_flags |= L2_UU_FLOOD;
205  if (flags & GBP_BD_FLAG_MCAST_DROP)
206  bd_flags |= L2_FLOOD;
207 
208  pool_get (gbp_bridge_domain_pool, gb);
209  memset (gb, 0, sizeof (*gb));
210 
211  gbi = gb - gbp_bridge_domain_pool;
212  gb->gb_bd_id = bd_id;
213  gb->gb_bd_index = bd_index;
217  gb->gb_locks = 1;
218  gb->gb_flags = flags;
220 
221  /*
222  * set the scope from the BD's RD's scope
223  */
224  gr = gbp_route_domain_get (gb->gb_rdi);
227 
228  /*
229  * Set the BVI and uu-flood interfaces into the BD
230  */
233 
234  if ((!(flags & GBP_BD_FLAG_UU_FWD_DROP) ||
235  (flags & GBP_BD_FLAG_UCAST_ARP)) &&
236  ~0 != gb->gb_uu_fwd_sw_if_index)
239 
240  if (!(flags & GBP_BD_FLAG_MCAST_DROP) && ~0 != bm_flood_sw_if_index)
241  {
242  gb->gb_bm_flood_itf =
243  gbp_itf_l2_add_and_lock (bm_flood_sw_if_index, gbi);
245  L2INPUT_FEAT_GBP_LEARN);
246  }
247 
248  /*
249  * unset any flag(s) set above
250  */
251  bd_set_flags (vlib_get_main (), bd_index, bd_flags, 0);
252 
253  if (flags & GBP_BD_FLAG_UCAST_ARP)
254  {
255  bd_flags = L2_ARP_UFWD;
256  bd_set_flags (vlib_get_main (), bd_index, bd_flags, 1);
257  }
258 
259  /*
260  * Add the BVI's MAC to the L2FIB
261  */
265  (L2FIB_ENTRY_RESULT_FLAG_STATIC |
266  L2FIB_ENTRY_RESULT_FLAG_BVI));
267 
269  }
270  else
271  {
272  gb = gbp_bridge_domain_get (gbi);
273  gb->gb_locks++;
274  }
275 
276  GBP_BD_DBG ("add: %U", format_gbp_bridge_domain_ptr, gb);
277 
278  return (0);
279 }
280 
281 void
284 {
286 
287  gb = gbp_bridge_domain_get (gbdi);
288 
290  sw_if_index, gb->gb_bd_index, type, 0, 0);
291  /*
292  * adding an interface to the bridge enables learning on the
293  * interface. Disable learning on the interface by default for gbp
294  * interfaces
295  */
296  l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_LEARN, 0);
297 }
298 
299 void
302 {
304 
305  gb = gbp_bridge_domain_get (gbdi);
306 
307  set_int_l2_mode (vlib_get_main (), vnet_get_main (), MODE_L3, sw_if_index,
308  gb->gb_bd_index, type, 0, 0);
309 }
310 
311 void
313 {
315 
316  gb = gbp_bridge_domain_get (gbdi);
317 
318  gb->gb_locks--;
319 
320  if (0 == gb->gb_locks)
321  {
322  GBP_BD_DBG ("destroy: %U", format_gbp_bridge_domain_ptr, gb);
323 
327 
330  if (~0 != gb->gb_uu_fwd_sw_if_index)
334 
337 
338  pool_put (gbp_bridge_domain_pool, gb);
339  }
340 }
341 
342 int
344 {
345  index_t gbi;
346 
347  GBP_BD_DBG ("del: %d", bd_id);
348  gbi = gbp_bridge_domain_find (bd_id);
349 
350  if (INDEX_INVALID != gbi)
351  {
352  GBP_BD_DBG ("del: %U", format_gbp_bridge_domain, gbi);
354 
355  return (0);
356  }
357 
358  return (VNET_API_ERROR_NO_SUCH_ENTRY);
359 }
360 
361 void
363 {
364  gbp_bridge_domain_t *gbpe;
365 
366  /* *INDENT-OFF* */
367  pool_foreach(gbpe, gbp_bridge_domain_pool,
368  {
369  if (!cb(gbpe, ctx))
370  break;
371  });
372  /* *INDENT-ON* */
373 }
374 
375 static clib_error_t *
377  unformat_input_t * input, vlib_cli_command_t * cmd)
378 {
379  vnet_main_t *vnm = vnet_get_main ();
382  u32 uu_fwd_sw_if_index = ~0;
383  u32 bd_id = ~0, rd_id = ~0;
384  u32 bvi_sw_if_index = ~0;
385  u8 add = 1;
386 
387  flags = GBP_BD_FLAG_NONE;
388 
390  {
391  if (unformat (input, "bvi %U", unformat_vnet_sw_interface,
392  vnm, &bvi_sw_if_index))
393  ;
394  else if (unformat (input, "uu-fwd %U", unformat_vnet_sw_interface,
395  vnm, &uu_fwd_sw_if_index))
396  ;
397  else if (unformat (input, "bm-flood %U", unformat_vnet_sw_interface,
398  vnm, &bm_flood_sw_if_index))
399  ;
400  else if (unformat (input, "add"))
401  add = 1;
402  else if (unformat (input, "del"))
403  add = 0;
404  else if (unformat (input, "flags %d", &flags))
405  ;
406  else if (unformat (input, "bd %d", &bd_id))
407  ;
408  else if (unformat (input, "rd %d", &rd_id))
409  ;
410  else
411  break;
412  }
413 
414  if (~0 == bd_id)
415  return clib_error_return (0, "BD-ID must be specified");
416  if (~0 == rd_id)
417  return clib_error_return (0, "RD-ID must be specified");
418 
419  if (add)
420  {
421  if (~0 == bvi_sw_if_index)
422  return clib_error_return (0, "interface must be specified");
423 
425  flags,
426  bvi_sw_if_index,
427  uu_fwd_sw_if_index,
428  bm_flood_sw_if_index);
429  }
430  else
431  gbp_bridge_domain_delete (bd_id);
432 
433  return (NULL);
434 }
435 
436 /*?
437  * Configure a GBP bridge-domain
438  *
439  * @cliexpar
440  * @cliexstart{gbp bridge-domain [del] bd <ID> bvi <interface> [uu-fwd <interface>] [bm-flood <interface>] [flags <flags>]}
441  * @cliexend
442  ?*/
443 /* *INDENT-OFF* */
444 VLIB_CLI_COMMAND (gbp_bridge_domain_cli_node, static) = {
445  .path = "gbp bridge-domain",
446  .short_help = "gbp bridge-domain [del] bd <ID> bvi <interface> [uu-fwd <interface>] [bm-flood <interface>] [flags <flags>]",
447  .function = gbp_bridge_domain_cli,
448 };
449 
450 static int
452 {
453  vlib_main_t *vm;
454 
455  vm = ctx;
457 
458  return (1);
459 }
460 
461 static clib_error_t *
463  unformat_input_t * input, vlib_cli_command_t * cmd)
464 {
465  vlib_cli_output (vm, "Bridge-Domains:");
467 
468  return (NULL);
469 }
470 
471 
472 /*?
473  * Show Group Based Policy Bridge_Domains and derived information
474  *
475  * @cliexpar
476  * @cliexstart{show gbp bridge_domain}
477  * @cliexend
478  ?*/
479 /* *INDENT-OFF* */
480 VLIB_CLI_COMMAND (gbp_bridge_domain_show_node, static) = {
481  .path = "show gbp bridge-domain",
482  .short_help = "show gbp bridge-domain\n",
483  .function = gbp_bridge_domain_show,
484 };
485 /* *INDENT-ON* */
486 
487 static clib_error_t *
489 {
490  gb_logger = vlib_log_register_class ("gbp", "bd");
491 
492  return (NULL);
493 }
494 
496 
497 /*
498  * fd.io coding-style-patch-verification: ON
499  *
500  * Local Variables:
501  * eval: (c-set-style "gnu")
502  * End:
503  */
vlib_log_class_t vlib_log_register_class(char *class, char *subclass)
Definition: log.c:176
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:439
void gbp_bridge_domain_walk(gbp_bridge_domain_cb_t cb, void *ctx)
void gbp_bridge_domain_itf_add(index_t gbdi, u32 sw_if_index, l2_bd_port_type_t type)
void gbp_bridge_domain_itf_del(index_t gbdi, u32 sw_if_index, l2_bd_port_type_t type)
vlib_log_class_t gb_logger
logger
u32 gb_uu_fwd_sw_if_index
The BD&#39;s MAC spine-proxy interface (optional)
void gbp_route_domain_unlock(index_t index)
#define hash_set(h, key, value)
Definition: hash.h:255
u32 flags
Definition: vhost_user.h:141
#define hash_unset(h, key)
Definition: hash.h:261
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
static void gbp_bridge_domain_db_add(gbp_bridge_domain_t *gb)
int gbp_bridge_domain_add_and_lock(u32 bd_id, u32 rd_id, gbp_bridge_domain_flags_t flags, u32 bvi_sw_if_index, u32 uu_fwd_sw_if_index, u32 bm_flood_sw_if_index)
void gbp_itf_unlock(gbp_itf_hdl_t *gh)
Definition: gbp_itf.c:286
A bridge Domain Representation.
u32 index_t
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.h:41
int i
u32 rd_id
Definition: gbp.api:34
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
unformat_function_t unformat_vnet_sw_interface
u32 uu_fwd_sw_if_index
Definition: gbp.api:37
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:236
static gbp_bridge_domain_t * gbp_bridge_domain_get(index_t i)
format_function_t format_vnet_sw_if_index_name
unsigned char u8
Definition: types.h:56
index_t gbp_bridge_domain_index(const gbp_bridge_domain_t *gbd)
void gbp_bridge_domain_unlock(index_t gbdi)
u32 vlib_log_class_t
Definition: vlib.h:51
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
enum gbp_bridge_domain_flags_t_ gbp_bridge_domain_flags_t
Bridge Domain Flags.
vl_api_interface_index_t sw_if_index
Definition: gre.api:50
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
#define MODE_L2_BRIDGE
Definition: l2_input.h:221
u16 gbp_scope_t
Definition: gbp_types.h:24
#define clib_error_return(e, args...)
Definition: error.h:99
void l2fib_add_entry(const u8 *mac, u32 bd_index, u32 sw_if_index, l2fib_entry_result_flags_t flags)
Add an entry to the l2fib.
Definition: l2_fib.c:401
unsigned int u32
Definition: types.h:88
static void gbp_bridge_domain_lock(index_t i)
static const u8 * vnet_sw_interface_get_hw_address(vnet_main_t *vnm, u32 sw_if_index)
vl_api_fib_path_type_t type
Definition: fib_types.api:123
static u8 * format_gbp_bridge_domain_ptr(u8 *s, va_list *args)
#define hash_get(h, key)
Definition: hash.h:249
#define GBP_BD_DBG(...)
static clib_error_t * gbp_bridge_domain_init(vlib_main_t *vm)
u32 bvi_sw_if_index
Definition: gbp.api:36
long ctx[MAX_CONNS]
Definition: main.c:144
u32 bd_id
Definition: gbp.api:274
struct _unformat_input_t unformat_input_t
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:286
enum l2_bd_port_type_t_ l2_bd_port_type_t
DB of bridge_domains.
u32 gb_bd_id
Bridge-domain ID.
u32 gb_rdi
Index of the Route-domain this BD is associated with.
static index_t gbp_bridge_domain_find(u32 bd_id)
Definition: l2_bd.h:150
gbp_bridge_domain_flags_t gb_flags
Flags conttrolling behaviour.
void gbp_itf_hdl_reset(gbp_itf_hdl_t *gh)
Definition: gbp_itf.c:108
static clib_error_t * gbp_bridge_domain_show(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
static int gbp_bridge_domain_show_one(gbp_bridge_domain_t *gb, void *ctx)
u32 gb_locks
locks/references to the BD so it does not get deleted (from the API) whilst it is still being used ...
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
u32 l2fib_del_entry(const u8 *mac, u32 bd_index, u32 sw_if_index)
Delete an entry from the l2fib.
Definition: l2_fib.c:685
int(* gbp_bridge_domain_cb_t)(gbp_bridge_domain_t *gb, void *ctx)
vlib_main_t * vm
Definition: buffer.c:323
gbp_itf_hdl_t gbp_itf_l2_add_and_lock(u32 sw_if_index, index_t gbi)
Definition: gbp_itf.c:196
u8 * format_gbp_bridge_domain(u8 *s, va_list *args)
static void gbp_bridge_domain_db_remove(gbp_bridge_domain_t *gb)
u32 bd_set_flags(vlib_main_t *vm, u32 bd_index, bd_flags_t flags, u32 enable)
Set the learn/forward/flood flags for the bridge domain.
Definition: l2_bd.c:252
gbp_bridge_domain_db_t gbp_bridge_domain_db
DB of bridge_domains.
gbp_bridge_domain_t * gbp_bridge_domain_pool
Pool of GBP bridge_domains.
index_t gbp_bridge_domain_find_and_lock(u32 bd_id)
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:161
gbp_route_domain_t * gbp_route_domain_get(index_t i)
u32 gb_bvi_sw_if_index
The BD&#39;s BVI interface (obligatory)
u32 bm_flood_sw_if_index
Definition: gbp.api:38
u8 * format_gbp_itf_hdl(u8 *s, va_list *args)
Definition: gbp_itf.c:520
u32 set_int_l2_mode(vlib_main_t *vm, vnet_main_t *vnet_main, u32 mode, u32 sw_if_index, u32 bd_index, l2_bd_port_type_t port_type, u32 shg, u32 xc_sw_if_index)
Set the subinterface to run in l2 or l3 mode.
Definition: l2_input.c:591
u8 * format_gbp_bridge_domain_flags(u8 *s, va_list *args)
u32 l2input_intf_bitmap_enable(u32 sw_if_index, l2input_feat_masks_t feature_bitmap, u32 enable)
Enable (or disable) the feature in the bitmap for the given interface.
Definition: l2_input.c:538
int gbp_bridge_domain_delete(u32 bd_id)
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
gbp_scope_t * gbp_scope_by_bd_index
Map of BD index to contract scope.
void gbp_itf_l2_set_input_feature(gbp_itf_hdl_t gh, l2input_feat_masks_t feats)
Definition: gbp_itf.c:382
gbp_itf_hdl_t gb_bm_flood_itf
The BD&#39;s interface to sned Broadcast and multicast packets.
A route Domain Representation.
#define INDEX_INVALID
Invalid index - used when no index is known blazoned capitals INVALID speak volumes where ~0 does not...
Definition: dpo.h:47
u64 uword
Definition: types.h:112
static clib_error_t * gbp_bridge_domain_cli(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
u32 bd_find_index(bd_main_t *bdm, u32 bd_id)
Get a bridge domain.
Definition: l2_bd.c:70
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
Definition: vec.h:486
bd_main_t bd_main
Definition: l2_bd.c:44
bd_flags
Flags that can be changed on a bridge domain.
Definition: l2.api:325
u32 gbp_bridge_domain_get_bd_id(index_t gbdi)
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:772
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
index_t gbp_route_domain_find_and_lock(u32 rd_id)
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171
#define MODE_L3
Definition: l2_input.h:220
enum bd_flags_t_ bd_flags_t