blob: 7760f3d858461631aeac261adcfea312341c8386 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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
Neale Ranns192b13f2019-03-15 02:16:20 -070024/* Allocated BVI instances */
25static uword *l2_bvi_instances;
Ed Warnickecb9cada2015-12-08 15:45:58 -070026
Dave Barach97d8dc22016-08-15 15:31:15 -040027/* Call the L2 nodes that need the ethertype mapping */
Ed Warnickecb9cada2015-12-08 15:45:58 -070028void
29l2bvi_register_input_type (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -040030 ethernet_type_t type, u32 node_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -070031{
32 l2fwd_register_input_type (vm, type, node_index);
33 l2flood_register_input_type (vm, type, node_index);
34}
35
Neale Ranns192b13f2019-03-15 02:16:20 -070036static u8 *
37format_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
43static clib_error_t *
44bvi_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
45{
46 u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
47 VNET_HW_INTERFACE_FLAG_LINK_UP : 0;
48 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
49 return 0;
50}
51
52static clib_error_t *
53bvi_mac_change (vnet_hw_interface_t * hi,
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* */
62VNET_DEVICE_CLASS (bvi_device_class) = {
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
75static u32
76bvi_instance_alloc (u32 want)
77{
78 /*
79 * Check for dynamically allocaetd instance number.
80 */
81 if (~0 == want)
82 {
83 u32 bit;
84
85 bit = clib_bitmap_first_clear (l2_bvi_instances);
86 if (bit >= BVI_MAX_INSTANCE)
87 {
88 return ~0;
89 }
90 l2_bvi_instances = clib_bitmap_set (l2_bvi_instances, bit, 1);
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 */
113 l2_bvi_instances = clib_bitmap_set (l2_bvi_instances, want, 1);
114
115 return want;
116}
117
118static int
119bvi_instance_free (u32 instance)
120{
121 if (instance >= BVI_MAX_INSTANCE)
122 {
123 return -1;
124 }
125
126 if (clib_bitmap_get (l2_bvi_instances, instance) == 0)
127 {
128 return -1;
129 }
130
131 l2_bvi_instances = clib_bitmap_set (l2_bvi_instances, instance, 0);
132 return 0;
133}
134
135int
136l2_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 ();
140 vlib_main_t *vm = vlib_get_main ();
141 u32 instance, hw_if_index, slot;
142 vnet_hw_interface_t *hw_if;
143 clib_error_t *error;
144 mac_address_t mac;
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 {
Neale Rannsd2a41942019-03-29 11:37:59 +0000169 u8 bytes[6] = {
Neale Ranns192b13f2019-03-15 02:16:20 -0700170 [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
181 error = ethernet_register_interface (vnm,
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;
189 clib_error_report (error);
190 return rv;
191 }
192
193 hw_if = vnet_get_hw_interface (vnm, hw_if_index);
194
195 slot = vlib_node_add_named_next_with_slot (vm, hw_if->tx_node_index,
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
203 si->flood_class = VNET_FLOOD_CLASS_BVI;
204 }
205
206 return 0;
207}
208
209int
210l2_bvi_delete (u32 sw_if_index)
211{
212 vnet_main_t *vnm = vnet_get_main ();
213
214 if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
215 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
216
217 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
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
224 ethernet_delete_interface (vnm, hw->hw_if_index);
225
226 return 0;
227}
228
229static clib_error_t *
230l2_bvi_create_cli (vlib_main_t * vm,
231 unformat_input_t * input, vlib_cli_command_t * cmd)
232{
233 unformat_input_t _line_input, *line_input = &_line_input;
234 u32 instance, sw_if_index;
235 clib_error_t *error;
236 mac_address_t mac;
237 int rv;
238
239 error = NULL;
240 instance = sw_if_index = ~0;
241 mac_address_set_zero (&mac);
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
265 rv = l2_bvi_create (instance, &mac, &sw_if_index);
266
267 if (rv)
268 return clib_error_return (0, "BVI create failed");
269
270 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
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* */
286VLIB_CLI_COMMAND (l2_bvi_create_command, static) = {
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
293static clib_error_t *
294l2_bvi_delete_cli (vlib_main_t * vm,
295 unformat_input_t * input, vlib_cli_command_t * cmd)
296{
297 vnet_main_t *vnm;
298 u32 sw_if_index;
299 int rv;
300
301 vnm = vnet_get_main ();
302 sw_if_index = ~0;
303
304 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
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 {
315 rv = l2_bvi_delete (sw_if_index);
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:
Dave Wallace3b62e292019-04-18 18:03:22 -0400332 * @cliexcmd{bvi delete <interace>}
Neale Ranns192b13f2019-03-15 02:16:20 -0700333 * Example of how to create a bvi interface:
334 * @cliexcmd{bvi delete bvi0}
335?*/
336/* *INDENT-OFF* */
337VLIB_CLI_COMMAND (l2_bvi_delete_command, static) = {
338 .path = "bvi delete",
339 .short_help = "bvi delete <interface>",
340 .function = l2_bvi_delete_cli,
341};
342/* *INDENT-ON* */
343
344
Dave Barach97d8dc22016-08-15 15:31:15 -0400345/*
346 * fd.io coding-style-patch-verification: ON
347 *
348 * Local Variables:
349 * eval: (c-set-style "gnu")
350 * End:
351 */