FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
l2_bvi.c
Go to the documentation of this file.
1 /*
2  * l2_bvi.c : layer 2 Bridged Virtual Interface
3  *
4  * Copyright (c) 2013 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 #include <vnet/l2/l2_fwd.h>
21 #include <vnet/l2/l2_flood.h>
22 #include <vnet/l2/l2_bvi.h>
23 
24 /* Allocated BVI instances */
26 
27 /* Call the L2 nodes that need the ethertype mapping */
28 void
31 {
34 }
35 
36 static u8 *
37 format_bvi_name (u8 * s, va_list * args)
38 {
39  u32 dev_instance = va_arg (*args, u32);
40  return format (s, "bvi%d", dev_instance);
41 }
42 
43 static clib_error_t *
44 bvi_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
45 {
48  vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
49  return 0;
50 }
51 
52 static clib_error_t *
54  const u8 * old_address, const u8 * mac_address)
55 {
56  l2input_interface_mac_change (hi->sw_if_index, old_address, mac_address);
57 
58  return (NULL);
59 }
60 
61 /* *INDENT-OFF* */
63  .name = "BVI",
64  .format_device_name = format_bvi_name,
65  .admin_up_down_function = bvi_admin_up_down,
66  .mac_addr_change_function = bvi_mac_change,
67 };
68 /* *INDENT-ON* */
69 
70 /*
71  * Maintain a bitmap of allocated bvi instance numbers.
72  */
73 #define BVI_MAX_INSTANCE (16 * 1024)
74 
75 static u32
77 {
78  /*
79  * Check for dynamically allocated instance number.
80  */
81  if (~0 == want)
82  {
83  u32 bit;
84 
86  if (bit >= BVI_MAX_INSTANCE)
87  {
88  return ~0;
89  }
91  return bit;
92  }
93 
94  /*
95  * In range?
96  */
97  if (want >= BVI_MAX_INSTANCE)
98  {
99  return ~0;
100  }
101 
102  /*
103  * Already in use?
104  */
105  if (clib_bitmap_get (l2_bvi_instances, want))
106  {
107  return ~0;
108  }
109 
110  /*
111  * Grant allocation request.
112  */
114 
115  return want;
116 }
117 
118 static int
120 {
121  if (instance >= BVI_MAX_INSTANCE)
122  {
123  return -1;
124  }
125 
127  {
128  return -1;
129  }
130 
132  return 0;
133 }
134 
135 int
136 l2_bvi_create (u32 user_instance,
137  const mac_address_t * mac_in, u32 * sw_if_indexp)
138 {
139  vnet_main_t *vnm = vnet_get_main ();
141  u32 instance, hw_if_index, slot;
142  vnet_hw_interface_t *hw_if;
145 
146  int rv = 0;
147 
148  ASSERT (sw_if_indexp);
149 
150  *sw_if_indexp = (u32) ~ 0;
151 
152  /*
153  * Allocate a bvi instance. Either select on dynamically
154  * or try to use the desired user_instance number.
155  */
156  instance = bvi_instance_alloc (user_instance);
157  if (instance == ~0)
158  {
159  return VNET_API_ERROR_INVALID_REGISTRATION;
160  }
161 
162  /*
163  * Default MAC address (b0b0:0000:0000 + instance) is allocated
164  * if zero mac_address is configured. Otherwise, user-configurable MAC
165  * address is programmed on the bvi interface.
166  */
167  if (mac_address_is_zero (mac_in))
168  {
169  u8 bytes[6] = {
170  [0] = 0xb0,
171  [1] = 0xb0,
172  [5] = instance,
173  };
174  mac_address_from_bytes (&mac, bytes);
175  }
176  else
177  {
178  mac_address_copy (&mac, mac_in);
179  }
180 
182  bvi_device_class.index,
183  instance, mac.bytes, &hw_if_index,
184  /* flag change */ 0);
185 
186  if (error)
187  {
188  rv = VNET_API_ERROR_INVALID_REGISTRATION;
190  return rv;
191  }
192 
193  hw_if = vnet_get_hw_interface (vnm, hw_if_index);
194 
196  "l2-input", 0);
197  ASSERT (slot == 0);
198 
199  {
200  vnet_sw_interface_t *si = vnet_get_hw_sw_interface (vnm, hw_if_index);
201  *sw_if_indexp = si->sw_if_index;
202 
204  }
205 
206  return 0;
207 }
208 
209 int
211 {
212  vnet_main_t *vnm = vnet_get_main ();
213 
215  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
216 
218  if (hw == 0 || hw->dev_class_index != bvi_device_class.index)
219  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
220 
221  if (bvi_instance_free (hw->dev_instance) < 0)
222  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
223 
225 
226  return 0;
227 }
228 
229 static clib_error_t *
231  unformat_input_t * input, vlib_cli_command_t * cmd)
232 {
233  unformat_input_t _line_input, *line_input = &_line_input;
237  int rv;
238 
239  error = NULL;
240  instance = sw_if_index = ~0;
242 
243  if (unformat_user (input, unformat_line_input, line_input))
244  {
245  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
246  {
247  if (unformat (line_input, "mac %U", unformat_mac_address_t, &mac))
248  ;
249  else if (unformat (line_input, "instance %d", &instance))
250  ;
251  else
252  {
253  error = clib_error_return (0, "unknown input: %U",
254  format_unformat_error, line_input);
255  break;
256  }
257  }
258 
259  unformat_free (line_input);
260 
261  if (error)
262  return error;
263  }
264 
266 
267  if (rv)
268  return clib_error_return (0, "BVI create failed");
269 
271  sw_if_index);
272  return 0;
273 }
274 
275 /*?
276  * Create a BVI interface. Optionally, a MAC Address can be
277  * provided. If not provided, 0b:0b::00:00:00:<instance> will be used.
278  *
279  * @cliexpar
280  * The following two command syntaxes are equivalent:
281  * @cliexcmd{bvi create [mac <mac-addr>] [instance <instance>]}
282  * Example of how to create a bvi interface:
283  * @cliexcmd{bvi create}
284 ?*/
285 /* *INDENT-OFF* */
287  .path = "bvi create",
288  .short_help = "bvi create [mac <mac-addr>] [instance <instance>]",
289  .function = l2_bvi_create_cli,
290 };
291 /* *INDENT-ON* */
292 
293 static clib_error_t *
295  unformat_input_t * input, vlib_cli_command_t * cmd)
296 {
297  vnet_main_t *vnm;
299  int rv;
300 
301  vnm = vnet_get_main ();
302  sw_if_index = ~0;
303 
305  {
306  if (unformat
307  (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
308  ;
309  else
310  break;
311  }
312 
313  if (~0 != sw_if_index)
314  {
316 
317  if (rv)
318  return clib_error_return (0, "BVI delete failed");
319  }
320  else
321  return clib_error_return (0, "no such interface: %U",
322  format_unformat_error, input);
323 
324  return 0;
325 }
326 
327 /*?
328  * Delete a BVI interface.
329  *
330  * @cliexpar
331  * The following two command syntaxes are equivalent:
332  * @cliexcmd{bvi delete <interface>}
333  * Example of how to create a bvi interface:
334  * @cliexcmd{bvi delete bvi0}
335 ?*/
336 /* *INDENT-OFF* */
338  .path = "bvi delete",
339  .short_help = "bvi delete <interface>",
340  .function = l2_bvi_delete_cli,
341 };
342 /* *INDENT-ON* */
343 
344 
345 /*
346  * fd.io coding-style-patch-verification: ON
347  *
348  * Local Variables:
349  * eval: (c-set-style "gnu")
350  * End:
351  */
vlib.h
l2_bvi_create
int l2_bvi_create(u32 user_instance, const mac_address_t *mac_in, u32 *sw_if_indexp)
Definition: l2_bvi.c:136
mac
vl_api_mac_address_t mac
Definition: l2.api:559
unformat_user
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
l2flood_register_input_type
void l2flood_register_input_type(vlib_main_t *vm, ethernet_type_t type, u32 node_index)
Add the L3 input node for this ethertype to the next nodes structure.
Definition: l2_flood.c:413
vnet_sw_interface_t
Definition: interface.h:868
unformat_line_input
unformat_function_t unformat_line_input
Definition: format.h:275
bvi_device_class
VNET_DEVICE_CLASS_TX_FN() bvi_device_class(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
send packets to l2-input.
Definition: l2_bvi_node.c:23
l2_bvi_create_command
static vlib_cli_command_t l2_bvi_create_command
(constructor) VLIB_CLI_COMMAND (l2_bvi_create_command)
Definition: l2_bvi.c:286
VNET_DEVICE_CLASS
VNET_DEVICE_CLASS(bvi_device_class)
clib_error_return
#define clib_error_return(e, args...)
Definition: error.h:99
vlib_cli_command_t::path
char * path
Definition: cli.h:96
l2input_interface_mac_change
void l2input_interface_mac_change(u32 sw_if_index, const u8 *old_address, const u8 *new_address)
Definition: l2_input.c:201
l2_fwd.h
vnet_hw_interface_t::tx_node_index
u32 tx_node_index
Definition: interface.h:653
l2_bvi_delete_cli
static clib_error_t * l2_bvi_delete_cli(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: l2_bvi.c:294
VNET_SW_INTERFACE_FLAG_ADMIN_UP
@ VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:843
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
node_index
node node_index
Definition: interface_output.c:420
VNET_HW_INTERFACE_FLAG_LINK_UP
@ VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:509
l2_bvi_delete_command
static vlib_cli_command_t l2_bvi_delete_command
(constructor) VLIB_CLI_COMMAND (l2_bvi_delete_command)
Definition: l2_bvi.c:337
hi
vl_api_ip4_address_t hi
Definition: arp.api:37
clib_error_report
#define clib_error_report(e)
Definition: error.h:113
unformat_input_t
struct _unformat_input_t unformat_input_t
vnet_hw_interface_t::dev_instance
u32 dev_instance
Definition: interface.h:660
clib_bitmap_first_clear
static uword clib_bitmap_first_clear(uword *ai)
Return the lowest numbered clear bit in a bitmap.
Definition: bitmap.h:451
error
Definition: cJSON.c:88
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
pool_is_free_index
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:302
vnet_sw_interface_t::sw_if_index
u32 sw_if_index
Definition: interface.h:875
mac_address_copy
static_always_inline void mac_address_copy(mac_address_t *dst, const mac_address_t *src)
Definition: mac_address.h:128
unformat_free
static void unformat_free(unformat_input_t *i)
Definition: format.h:155
clib_bitmap_get
static uword clib_bitmap_get(uword *ai, uword i)
Gets the ith bit value from a bitmap.
Definition: bitmap.h:197
slot
u8 slot
Definition: pci_types.api:22
VNET_FLOOD_CLASS_BVI
@ VNET_FLOOD_CLASS_BVI
Definition: interface.h:816
vnet_get_hw_interface
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface_funcs.h:44
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
unformat_check_input
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:163
bvi_admin_up_down
static clib_error_t * bvi_admin_up_down(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: l2_bvi.c:44
l2_bvi.h
vnet_sw_interface_t::flood_class
vnet_flood_class_t flood_class
Definition: interface.h:897
mac_address_is_zero
static_always_inline int mac_address_is_zero(const mac_address_t *mac)
Definition: mac_address.h:106
uword
u64 uword
Definition: types.h:112
vnet_hw_interface_t::dev_class_index
u32 dev_class_index
Definition: interface.h:659
format_bvi_name
static u8 * format_bvi_name(u8 *s, va_list *args)
Definition: l2_bvi.c:37
ethernet_type_t
ethernet_type_t
Definition: packet.h:45
l2_bvi_delete
int l2_bvi_delete(u32 sw_if_index)
Definition: l2_bvi.c:210
format_unformat_error
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
mac_address_set_zero
static_always_inline void mac_address_set_zero(mac_address_t *mac)
Definition: mac_address.h:146
VLIB_CLI_COMMAND
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:163
clib_bitmap_set
static uword * clib_bitmap_set(uword *ai, uword i, uword value)
Sets the ith bit of a bitmap to new_value Removes trailing zeros from the bitmap.
Definition: bitmap.h:167
vlib_cli_output
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:716
vnet_interface_main_t::sw_interfaces
vnet_sw_interface_t * sw_interfaces
Definition: interface.h:1014
vlib_node_add_named_next_with_slot
uword vlib_node_add_named_next_with_slot(vlib_main_t *vm, uword node, char *name, uword slot)
Definition: node.c:244
bvi_instance_free
static int bvi_instance_free(u32 instance)
Definition: l2_bvi.c:119
l2fwd_register_input_type
void l2fwd_register_input_type(vlib_main_t *vm, ethernet_type_t type, u32 node_index)
Add the L3 input node for this ethertype to the next nodes structure.
Definition: l2_fwd.c:468
vnet_hw_interface_t
Definition: interface.h:638
vnet_main_t
Definition: vnet.h:76
l2bvi_register_input_type
void l2bvi_register_input_type(vlib_main_t *vm, ethernet_type_t type, u32 node_index)
Definition: l2_bvi.c:29
BVI_MAX_INSTANCE
#define BVI_MAX_INSTANCE
Definition: l2_bvi.c:73
format_vnet_sw_if_index_name
format_function_t format_vnet_sw_if_index_name
Definition: interface_funcs.h:455
unformat_vnet_sw_interface
unformat_function_t unformat_vnet_sw_interface
Definition: interface_funcs.h:459
format
description fragment has unexpected format
Definition: map.api:433
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
unformat_mac_address_t
uword unformat_mac_address_t(unformat_input_t *input, va_list *args)
Definition: mac_address.c:37
vnet_get_hw_sw_interface
static vnet_sw_interface_t * vnet_get_hw_sw_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface_funcs.h:72
u32
unsigned int u32
Definition: types.h:88
vnet_get_sup_hw_interface
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
Definition: interface_funcs.h:92
ethernet_delete_interface
void ethernet_delete_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface.c:393
si
vnet_sw_interface_t * si
Definition: interface_output.c:398
instance
u32 instance
Definition: gre.api:51
mac_address_t_
Definition: mac_address.h:21
vlib_main_t
Definition: main.h:102
vlib_get_main
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
vnet_hw_interface_set_flags
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, vnet_hw_interface_flags_t flags)
Definition: interface.c:513
bvi_mac_change
static clib_error_t * bvi_mac_change(vnet_hw_interface_t *hi, const u8 *old_address, const u8 *mac_address)
Definition: l2_bvi.c:53
l2_bvi_create_cli
static clib_error_t * l2_bvi_create_cli(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: l2_bvi.c:230
vnet_hw_interface_t::hw_if_index
u32 hw_if_index
Definition: interface.h:667
rv
int __clib_unused rv
Definition: application.c:491
l2_flood.h
vnet.h
vlib_cli_command_t
Definition: cli.h:92
bvi_instance_alloc
static u32 bvi_instance_alloc(u32 want)
Definition: l2_bvi.c:76
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
l2_bvi_instances
static uword * l2_bvi_instances
Definition: l2_bvi.c:25
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
ethernet_register_interface
clib_error_t * ethernet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, const u8 *address, u32 *hw_if_index_return, ethernet_flag_change_function_t flag_change)
Definition: interface.c:348
vnet_main_t::interface_main
vnet_interface_main_t interface_main
Definition: vnet.h:81
UNFORMAT_END_OF_INPUT
#define UNFORMAT_END_OF_INPUT
Definition: format.h:137
mac_address_from_bytes
static_always_inline void mac_address_from_bytes(mac_address_t *mac, const u8 *bytes)
Definition: mac_address.h:92
mac_address
manual_print typedef u8 mac_address[6]
Definition: ethernet_types.api:19
flags
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105