FD.io VPP  v19.01.3-6-g70449b9b9
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 
19 #include <vnet/dpo/dvr_dpo.h>
20 #include <vnet/fib/fib_table.h>
21 #include <vnet/l2/l2_input.h>
22 #include <vnet/l2/feat_bitmap.h>
23 #include <vnet/l2/l2_bvi.h>
24 #include <vnet/l2/l2_fib.h>
25 
26 /**
27  * Pool of GBP bridge_domains
28  */
30 
31 /**
32  * DB of bridge_domains
33  */
35 
36 /**
37  * logger
38  */
40 
41 #define GBP_BD_DBG(...) \
42  vlib_log_debug (gb_logger, __VA_ARGS__);
43 
44 index_t
46 {
47  return (gbd - gbp_bridge_domain_pool);
48 }
49 
50 static void
52 {
54 
55  gb = gbp_bridge_domain_get (i);
56  gb->gb_locks++;
57 }
58 
59 u32
61 {
63 
64  gb = gbp_bridge_domain_get (gbdi);
65 
66  return (gb->gb_bd_id);
67 }
68 
69 static index_t
71 {
72  uword *p;
73 
74  p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
75 
76  if (NULL != p)
77  return p[0];
78 
79  return (INDEX_INVALID);
80 }
81 
82 index_t
84 {
85  uword *p;
86 
87  p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
88 
89  if (NULL != p)
90  {
92  return p[0];
93  }
94  return (INDEX_INVALID);
95 }
96 
97 static void
99 {
100  index_t gbi = gb - gbp_bridge_domain_pool;
101 
102  hash_set (gbp_bridge_domain_db.gbd_by_bd_id, gb->gb_bd_id, gbi);
103  vec_validate_init_empty (gbp_bridge_domain_db.gbd_by_bd_index,
105  gbp_bridge_domain_db.gbd_by_bd_index[gb->gb_bd_index] = gbi;
106 }
107 
108 static void
110 {
111  hash_unset (gbp_bridge_domain_db.gbd_by_bd_id, gb->gb_bd_id);
112  gbp_bridge_domain_db.gbd_by_bd_index[gb->gb_bd_index] = INDEX_INVALID;
113 }
114 
115 static u8 *
116 format_gbp_bridge_domain_ptr (u8 * s, va_list * args)
117 {
118  gbp_bridge_domain_t *gb = va_arg (*args, gbp_bridge_domain_t *);
119  vnet_main_t *vnm = vnet_get_main ();
120 
121  if (NULL != gb)
122  s = format (s, "[%d] bd:[%d,%d], bvi:%U uu-flood:%U locks:%d",
123  gb - gbp_bridge_domain_pool,
124  gb->gb_bd_id,
125  gb->gb_bd_index,
128  gb->gb_locks);
129  else
130  s = format (s, "NULL");
131 
132  return (s);
133 }
134 
135 u8 *
136 format_gbp_bridge_domain (u8 * s, va_list * args)
137 {
138  index_t gbi = va_arg (*args, index_t);
139 
140  s =
142  gbp_bridge_domain_get (gbi));
143 
144  return (s);
145 }
146 
147 int
151 {
153  index_t gbi;
154 
155  gbi = gbp_bridge_domain_find (bd_id);
156 
157  if (INDEX_INVALID == gbi)
158  {
159  u32 bd_index;
160 
161  bd_index = bd_find_index (&bd_main, bd_id);
162 
163  if (~0 == bd_index)
164  return (VNET_API_ERROR_BD_NOT_MODIFIABLE);
165 
166  /*
167  * unset learning in the bridge
168  */
169  bd_set_flags (vlib_get_main (), bd_index, L2_LEARN, 0);
170 
171  pool_get (gbp_bridge_domain_pool, gb);
172  memset (gb, 0, sizeof (*gb));
173 
174  gb->gb_bd_id = bd_id;
175  gb->gb_bd_index = bd_index;
178  gb->gb_locks = 1;
179  gb->gb_flags = flags;
180 
181  /*
182  * Set the BVI and uu-flood interfaces into the BD
183  */
186  bd_index, L2_BD_PORT_TYPE_BVI, 0, 0);
187  if (~0 != gb->gb_uu_fwd_sw_if_index)
190  bd_index, L2_BD_PORT_TYPE_UU_FWD, 0, 0);
191 
192  /*
193  * Add the BVI's MAC to the L2FIB
194  */
198  (L2FIB_ENTRY_RESULT_FLAG_STATIC |
199  L2FIB_ENTRY_RESULT_FLAG_BVI));
200 
202  }
203  else
204  {
205  gb = gbp_bridge_domain_get (gbi);
206  gb->gb_locks++;
207  }
208 
209  GBP_BD_DBG ("add: %U", format_gbp_bridge_domain_ptr, gb);
210 
211  return (0);
212 }
213 
214 void
216 {
218 
219  gb = gbp_bridge_domain_get (index);
220 
221  gb->gb_locks--;
222 
223  if (0 == gb->gb_locks)
224  {
225  GBP_BD_DBG ("destroy: %U", format_gbp_bridge_domain_ptr, gb);
226 
230 
233  gb->gb_bd_index, L2_BD_PORT_TYPE_BVI, 0, 0);
234  if (~0 != gb->gb_uu_fwd_sw_if_index)
238 
240 
241  pool_put (gbp_bridge_domain_pool, gb);
242  }
243 }
244 
245 int
247 {
248  index_t gbi;
249 
250  GBP_BD_DBG ("del: %d", bd_id);
251  gbi = gbp_bridge_domain_find (bd_id);
252 
253  if (INDEX_INVALID != gbi)
254  {
255  GBP_BD_DBG ("del: %U", format_gbp_bridge_domain, gbi);
257 
258  return (0);
259  }
260 
261  return (VNET_API_ERROR_NO_SUCH_ENTRY);
262 }
263 
264 void
266 {
267  gbp_bridge_domain_t *gbpe;
268 
269  /* *INDENT-OFF* */
270  pool_foreach(gbpe, gbp_bridge_domain_pool,
271  {
272  if (!cb(gbpe, ctx))
273  break;
274  });
275  /* *INDENT-ON* */
276 }
277 
278 static clib_error_t *
280  unformat_input_t * input, vlib_cli_command_t * cmd)
281 {
282  vnet_main_t *vnm = vnet_get_main ();
283  u32 uu_fwd_sw_if_index = ~0;
284  u32 bvi_sw_if_index = ~0;
285  u32 bd_id = ~0;
286  u8 add = 1;
287 
289  {
290  if (unformat (input, "bvi %U", unformat_vnet_sw_interface,
291  vnm, &bvi_sw_if_index))
292  ;
293  else if (unformat (input, "uu-flood %U", unformat_vnet_sw_interface,
294  vnm, &uu_fwd_sw_if_index))
295  ;
296  else if (unformat (input, "add"))
297  add = 1;
298  else if (unformat (input, "del"))
299  add = 0;
300  else if (unformat (input, "bd %d", &bd_id))
301  ;
302  else
303  break;
304  }
305 
306  if (~0 == bd_id)
307  return clib_error_return (0, "EPG-ID must be specified");
308 
309  if (add)
310  {
311  if (~0 == bvi_sw_if_index)
312  return clib_error_return (0, "interface must be specified");
313 
315  bvi_sw_if_index, uu_fwd_sw_if_index);
316  }
317  else
318  gbp_bridge_domain_delete (bd_id);
319 
320  return (NULL);
321 }
322 
323 /*?
324  * Configure a GBP bridge-domain
325  *
326  * @cliexpar
327  * @cliexstart{set gbp bridge-domain [del] bd <ID> bvi <interface> uu-flood <interface>}
328  * @cliexend
329  ?*/
330 /* *INDENT-OFF* */
331 VLIB_CLI_COMMAND (gbp_bridge_domain_cli_node, static) = {
332  .path = "gbp bridge-domain",
333  .short_help = "gbp bridge-domain [del] epg bd <ID> bvi <interface> uu-flood <interface>",
334  .function = gbp_bridge_domain_cli,
335 };
336 
337 static int
339 {
340  vlib_main_t *vm;
341 
342  vm = ctx;
344 
345  return (1);
346 }
347 
348 static clib_error_t *
350  unformat_input_t * input, vlib_cli_command_t * cmd)
351 {
352  vlib_cli_output (vm, "Bridge-Domains:");
354 
355  return (NULL);
356 }
357 
358 
359 /*?
360  * Show Group Based Policy Bridge_Domains and derived information
361  *
362  * @cliexpar
363  * @cliexstart{show gbp bridge_domain}
364  * @cliexend
365  ?*/
366 /* *INDENT-OFF* */
367 VLIB_CLI_COMMAND (gbp_bridge_domain_show_node, static) = {
368  .path = "show gbp bridge-domain",
369  .short_help = "show gbp bridge-domain\n",
370  .function = gbp_bridge_domain_show,
371 };
372 /* *INDENT-ON* */
373 
374 static clib_error_t *
376 {
377  gb_logger = vlib_log_register_class ("gbp", "bd");
378 
379  return (NULL);
380 }
381 
383 
384 /*
385  * fd.io coding-style-patch-verification: ON
386  *
387  * Local Variables:
388  * eval: (c-set-style "gnu")
389  * End:
390  */
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)
u32 vlib_log_class_t
Definition: log.h:21
#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:419
unformat_function_t unformat_vnet_sw_interface
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 uu_fwd_sw_if_index
Definition: gbp.api:33
#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)
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:490
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
#define GBP_BD_DBG(...)
static clib_error_t * gbp_bridge_domain_init(vlib_main_t *vm)
u32 bvi_sw_if_index
Definition: gbp.api:32
long ctx[MAX_CONNS]
Definition: main.c:144
u32 bd_id
Definition: gbp.api:260
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 ...
#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:301
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 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:583
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:488
bd_main_t bd_main
Definition: l2_bd.c:44
void gbp_bridge_domain_unlock(index_t index)
u32 gbp_bridge_domain_get_bd_id(index_t gbdi)
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:762
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