FD.io VPP  v19.04-6-g6f05f72
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 
18 #include <plugins/gbp/gbp_learn.h>
19 
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>
26 
27 /**
28  * Pool of GBP bridge_domains
29  */
31 
32 /**
33  * DB of bridge_domains
34  */
36 
37 /**
38  * logger
39  */
41 
42 #define GBP_BD_DBG(...) \
43  vlib_log_debug (gb_logger, __VA_ARGS__);
44 
45 index_t
47 {
48  return (gbd - gbp_bridge_domain_pool);
49 }
50 
51 static void
53 {
55 
56  gb = gbp_bridge_domain_get (i);
57  gb->gb_locks++;
58 }
59 
60 u32
62 {
64 
65  gb = gbp_bridge_domain_get (gbdi);
66 
67  return (gb->gb_bd_id);
68 }
69 
70 static index_t
72 {
73  uword *p;
74 
75  p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
76 
77  if (NULL != p)
78  return p[0];
79 
80  return (INDEX_INVALID);
81 }
82 
83 index_t
85 {
86  uword *p;
87 
88  p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
89 
90  if (NULL != p)
91  {
93  return p[0];
94  }
95  return (INDEX_INVALID);
96 }
97 
98 static void
100 {
101  index_t gbi = gb - gbp_bridge_domain_pool;
102 
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,
106  gbp_bridge_domain_db.gbd_by_bd_index[gb->gb_bd_index] = gbi;
107 }
108 
109 static void
111 {
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;
114 }
115 
116 u8 *
117 format_gbp_bridge_domain_flags (u8 * s, va_list * args)
118 {
120 
121  if (gf)
122  {
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 ");
131  }
132  else
133  {
134  s = format (s, "none");
135  }
136  return (s);
137 }
138 
139 static u8 *
140 format_gbp_bridge_domain_ptr (u8 * s, va_list * args)
141 {
142  gbp_bridge_domain_t *gb = va_arg (*args, gbp_bridge_domain_t *);
143  vnet_main_t *vnm = vnet_get_main ();
144 
145  if (NULL != gb)
146  s = format (s, "[%d] bd:[%d,%d], bvi:%U uu-flood:%U flags:%U locks:%d",
147  gb - gbp_bridge_domain_pool,
148  gb->gb_bd_id,
149  gb->gb_bd_index,
153  else
154  s = format (s, "NULL");
155 
156  return (s);
157 }
158 
159 u8 *
160 format_gbp_bridge_domain (u8 * s, va_list * args)
161 {
162  index_t gbi = va_arg (*args, index_t);
163 
164  s =
166  gbp_bridge_domain_get (gbi));
167 
168  return (s);
169 }
170 
171 int
177 {
179  index_t gbi;
180 
181  gbi = gbp_bridge_domain_find (bd_id);
182 
183  if (INDEX_INVALID == gbi)
184  {
185  u32 bd_index;
186 
187  bd_index = bd_find_index (&bd_main, bd_id);
188 
189  if (~0 == bd_index)
190  return (VNET_API_ERROR_BD_NOT_MODIFIABLE);
191 
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;
197 
198  pool_get (gbp_bridge_domain_pool, gb);
199  memset (gb, 0, sizeof (*gb));
200 
201  gb->gb_bd_id = bd_id;
202  gb->gb_bd_index = bd_index;
206  gb->gb_locks = 1;
207  gb->gb_flags = flags;
208 
209  /*
210  * Set the BVI and uu-flood interfaces into the BD
211  */
214  bd_index, L2_BD_PORT_TYPE_BVI, 0, 0);
215 
216  if (!(flags & GBP_BD_FLAG_UU_FWD_DROP)
217  && ~0 != gb->gb_uu_fwd_sw_if_index)
218  {
221  bd_index, L2_BD_PORT_TYPE_UU_FWD, 0, 0);
222  }
223  if (!(flags & GBP_BD_FLAG_MCAST_DROP)
224  && ~0 != gb->gb_bm_flood_sw_if_index)
225  {
228  bd_index, L2_BD_PORT_TYPE_NORMAL, 0, 0);
230  }
231 
232  /*
233  * unset learning in the bridge + any flag(s) set above
234  */
235  bd_set_flags (vlib_get_main (), bd_index, bd_flags, 0);
236 
237  /*
238  * Add the BVI's MAC to the L2FIB
239  */
243  (L2FIB_ENTRY_RESULT_FLAG_STATIC |
244  L2FIB_ENTRY_RESULT_FLAG_BVI));
245 
247  }
248  else
249  {
250  gb = gbp_bridge_domain_get (gbi);
251  gb->gb_locks++;
252  }
253 
254  GBP_BD_DBG ("add: %U", format_gbp_bridge_domain_ptr, gb);
255 
256  return (0);
257 }
258 
259 void
261 {
263 
264  gb = gbp_bridge_domain_get (index);
265 
266  gb->gb_locks--;
267 
268  if (0 == gb->gb_locks)
269  {
270  GBP_BD_DBG ("destroy: %U", format_gbp_bridge_domain_ptr, gb);
271 
275 
278  gb->gb_bd_index, L2_BD_PORT_TYPE_BVI, 0, 0);
279  if (~0 != gb->gb_uu_fwd_sw_if_index)
280  {
284  }
285  if (~0 != gb->gb_bm_flood_sw_if_index)
286  {
291  }
292 
294 
295  pool_put (gbp_bridge_domain_pool, gb);
296  }
297 }
298 
299 int
301 {
302  index_t gbi;
303 
304  GBP_BD_DBG ("del: %d", bd_id);
305  gbi = gbp_bridge_domain_find (bd_id);
306 
307  if (INDEX_INVALID != gbi)
308  {
309  GBP_BD_DBG ("del: %U", format_gbp_bridge_domain, gbi);
311 
312  return (0);
313  }
314 
315  return (VNET_API_ERROR_NO_SUCH_ENTRY);
316 }
317 
318 void
320 {
321  gbp_bridge_domain_t *gbpe;
322 
323  /* *INDENT-OFF* */
324  pool_foreach(gbpe, gbp_bridge_domain_pool,
325  {
326  if (!cb(gbpe, ctx))
327  break;
328  });
329  /* *INDENT-ON* */
330 }
331 
332 static clib_error_t *
334  unformat_input_t * input, vlib_cli_command_t * cmd)
335 {
336  vnet_main_t *vnm = vnet_get_main ();
339  u32 uu_fwd_sw_if_index = ~0;
340  u32 bvi_sw_if_index = ~0;
341  u32 bd_id = ~0;
342  u8 add = 1;
343 
344  flags = GBP_BD_FLAG_NONE;
345 
347  {
348  if (unformat (input, "bvi %U", unformat_vnet_sw_interface,
349  vnm, &bvi_sw_if_index))
350  ;
351  else if (unformat (input, "uu-fwd %U", unformat_vnet_sw_interface,
352  vnm, &uu_fwd_sw_if_index))
353  ;
354  else if (unformat (input, "bm-flood %U", unformat_vnet_sw_interface,
355  vnm, &bm_flood_sw_if_index))
356  ;
357  else if (unformat (input, "add"))
358  add = 1;
359  else if (unformat (input, "del"))
360  add = 0;
361  else if (unformat (input, "flags %d", &flags))
362  ;
363  else if (unformat (input, "bd %d", &bd_id))
364  ;
365  else
366  break;
367  }
368 
369  if (~0 == bd_id)
370  return clib_error_return (0, "BD-ID must be specified");
371 
372  if (add)
373  {
374  if (~0 == bvi_sw_if_index)
375  return clib_error_return (0, "interface must be specified");
376 
377  gbp_bridge_domain_add_and_lock (bd_id, flags,
378  bvi_sw_if_index,
379  uu_fwd_sw_if_index,
380  bm_flood_sw_if_index);
381  }
382  else
383  gbp_bridge_domain_delete (bd_id);
384 
385  return (NULL);
386 }
387 
388 /*?
389  * Configure a GBP bridge-domain
390  *
391  * @cliexpar
392  * @cliexstart{set gbp bridge-domain [del] bd <ID> bvi <interface> uu-flood <interface>}
393  * @cliexend
394  ?*/
395 /* *INDENT-OFF* */
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,
401 };
402 
403 static int
405 {
406  vlib_main_t *vm;
407 
408  vm = ctx;
410 
411  return (1);
412 }
413 
414 static clib_error_t *
416  unformat_input_t * input, vlib_cli_command_t * cmd)
417 {
418  vlib_cli_output (vm, "Bridge-Domains:");
420 
421  return (NULL);
422 }
423 
424 
425 /*?
426  * Show Group Based Policy Bridge_Domains and derived information
427  *
428  * @cliexpar
429  * @cliexstart{show gbp bridge_domain}
430  * @cliexend
431  ?*/
432 /* *INDENT-OFF* */
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,
437 };
438 /* *INDENT-ON* */
439 
440 static clib_error_t *
442 {
443  gb_logger = vlib_log_register_class ("gbp", "bd");
444 
445  return (NULL);
446 }
447 
449 
450 /*
451  * fd.io coding-style-patch-verification: ON
452  *
453  * Local Variables:
454  * eval: (c-set-style "gnu")
455  * End:
456  */
vlib_log_class_t vlib_log_register_class(char *class, char *subclass)
Definition: log.c:227
void gbp_bridge_domain_walk(gbp_bridge_domain_cb_t cb, void *ctx)
vlib_log_class_t gb_logger
logger
u32 gb_uu_fwd_sw_if_index
The BD&#39;s MAC spine-proxy interface (optional)
#define hash_set(h, key, value)
Definition: hash.h:255
u32 flags
Definition: vhost_user.h:115
#define hash_unset(h, key)
Definition: hash.h:261
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
static void gbp_bridge_domain_db_add(gbp_bridge_domain_t *gb)
#define NULL
Definition: clib.h:58
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
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:36
#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)
u32 vlib_log_class_t
Definition: vlib.h:50
#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.
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
#define MODE_L2_BRIDGE
Definition: l2_input.h:211
#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:400
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)
static u8 * format_gbp_bridge_domain_ptr(u8 *s, va_list *args)
#define hash_get(h, key)
Definition: hash.h:249
int gbp_bridge_domain_add_and_lock(u32 bd_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_learn_enable(u32 sw_if_index, gbb_learn_mode_t mode)
Definition: gbp_learn.c:25
#define GBP_BD_DBG(...)
static clib_error_t * gbp_bridge_domain_init(vlib_main_t *vm)
u32 bvi_sw_if_index
Definition: gbp.api:35
long ctx[MAX_CONNS]
Definition: main.c:144
u32 bd_id
Definition: gbp.api:269
struct _unformat_input_t unformat_input_t
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:286
DB of bridge_domains.
u32 gb_bd_id
Bridge-domain ID.
static index_t gbp_bridge_domain_find(u32 bd_id)
gbp_bridge_domain_flags_t gb_flags
Flags conttrolling behaviour.
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 ...
u32 gb_bm_flood_sw_if_index
The BD&#39;s interface to sned Broadcast and multicast packets.
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
u32 l2fib_del_entry(const u8 *mac, u32 bd_index, u32 sw_if_index)
Delete an entry from the l2fib.
Definition: l2_fib.c:684
int(* gbp_bridge_domain_cb_t)(gbp_bridge_domain_t *gb, void *ctx)
vlib_main_t * vm
Definition: buffer.c:312
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:251
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:155
u32 gb_bvi_sw_if_index
The BD&#39;s BVI interface (obligatory)
u32 bm_flood_sw_if_index
Definition: gbp.api:37
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:589
u8 * format_gbp_bridge_domain_flags(u8 *s, va_list *args)
int gbp_bridge_domain_delete(u32 bd_id)
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
#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:69
#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
void gbp_bridge_domain_unlock(index_t index)
bd_flags
Flags that can be changed on a bridge domain.
Definition: l2.api:321
u32 gbp_bridge_domain_get_bd_id(index_t gbdi)
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:725
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170
#define MODE_L3
Definition: l2_input.h:210
enum bd_flags_t_ bd_flags_t