ethernet: check destination mac for L3 in ethernet-input node
[vpp.git] / src / vnet / l2 / l2_bvi.c
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 */
25 static uword *l2_bvi_instances;
26
27 /* Call the L2 nodes that need the ethertype mapping */
28 void
29 l2bvi_register_input_type (vlib_main_t * vm,
30                            ethernet_type_t type, u32 node_index)
31 {
32   l2fwd_register_input_type (vm, type, node_index);
33   l2flood_register_input_type (vm, type, node_index);
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 {
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
52 static clib_error_t *
53 bvi_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 VNET_DEVICE_CLASS (bvi_device_class) = {
62   .name = "BVI",
63   .format_device_name = format_bvi_name,
64   .admin_up_down_function = bvi_admin_up_down,
65   .mac_addr_change_function = bvi_mac_change,
66 };
67
68 /*
69  * Maintain a bitmap of allocated bvi instance numbers.
70  */
71 #define BVI_MAX_INSTANCE                (16 * 1024)
72
73 static u32
74 bvi_instance_alloc (u32 want)
75 {
76   /*
77    * Check for dynamically allocated instance number.
78    */
79   if (~0 == want)
80     {
81       u32 bit;
82
83       bit = clib_bitmap_first_clear (l2_bvi_instances);
84       if (bit >= BVI_MAX_INSTANCE)
85         {
86           return ~0;
87         }
88       l2_bvi_instances = clib_bitmap_set (l2_bvi_instances, bit, 1);
89       return bit;
90     }
91
92   /*
93    * In range?
94    */
95   if (want >= BVI_MAX_INSTANCE)
96     {
97       return ~0;
98     }
99
100   /*
101    * Already in use?
102    */
103   if (clib_bitmap_get (l2_bvi_instances, want))
104     {
105       return ~0;
106     }
107
108   /*
109    * Grant allocation request.
110    */
111   l2_bvi_instances = clib_bitmap_set (l2_bvi_instances, want, 1);
112
113   return want;
114 }
115
116 static int
117 bvi_instance_free (u32 instance)
118 {
119   if (instance >= BVI_MAX_INSTANCE)
120     {
121       return -1;
122     }
123
124   if (clib_bitmap_get (l2_bvi_instances, instance) == 0)
125     {
126       return -1;
127     }
128
129   l2_bvi_instances = clib_bitmap_set (l2_bvi_instances, instance, 0);
130   return 0;
131 }
132
133 int
134 l2_bvi_create (u32 user_instance,
135                const mac_address_t * mac_in, u32 * sw_if_indexp)
136 {
137   vnet_main_t *vnm = vnet_get_main ();
138   vlib_main_t *vm = vlib_get_main ();
139   vnet_eth_interface_registration_t eir = {};
140   u32 instance, hw_if_index, slot;
141   vnet_hw_interface_t *hw_if;
142   mac_address_t mac;
143
144   ASSERT (sw_if_indexp);
145
146   *sw_if_indexp = (u32) ~ 0;
147
148   /*
149    * Allocate a bvi instance.  Either select on dynamically
150    * or try to use the desired user_instance number.
151    */
152   instance = bvi_instance_alloc (user_instance);
153   if (instance == ~0)
154     {
155       return VNET_API_ERROR_INVALID_REGISTRATION;
156     }
157
158   /*
159    * Default MAC address (b0b0:0000:0000 + instance) is allocated
160    * if zero mac_address is configured. Otherwise, user-configurable MAC
161    * address is programmed on the bvi interface.
162    */
163   if (mac_address_is_zero (mac_in))
164     {
165       u8 bytes[6] = {
166         [0] = 0xb0,
167         [1] = 0xb0,
168         [5] = instance,
169       };
170       mac_address_from_bytes (&mac, bytes);
171     }
172   else
173     {
174       mac_address_copy (&mac, mac_in);
175     }
176
177   eir.dev_class_index = bvi_device_class.index;
178   eir.dev_instance = instance;
179   eir.address = mac.bytes;
180   hw_if_index = vnet_eth_register_interface (vnm, &eir);
181
182   hw_if = vnet_get_hw_interface (vnm, hw_if_index);
183
184   slot = vlib_node_add_named_next_with_slot (vm, hw_if->tx_node_index,
185                                              "l2-input", 0);
186   ASSERT (slot == 0);
187
188   {
189     vnet_sw_interface_t *si = vnet_get_hw_sw_interface (vnm, hw_if_index);
190     *sw_if_indexp = si->sw_if_index;
191
192     si->flood_class = VNET_FLOOD_CLASS_BVI;
193   }
194
195   return 0;
196 }
197
198 int
199 l2_bvi_delete (u32 sw_if_index)
200 {
201   vnet_main_t *vnm = vnet_get_main ();
202
203   if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
204     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
205
206   vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
207   if (hw == 0 || hw->dev_class_index != bvi_device_class.index)
208     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
209
210   if (bvi_instance_free (hw->dev_instance) < 0)
211     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
212
213   ethernet_delete_interface (vnm, hw->hw_if_index);
214
215   return 0;
216 }
217
218 static clib_error_t *
219 l2_bvi_create_cli (vlib_main_t * vm,
220                    unformat_input_t * input, vlib_cli_command_t * cmd)
221 {
222   unformat_input_t _line_input, *line_input = &_line_input;
223   u32 instance, sw_if_index;
224   clib_error_t *error;
225   mac_address_t mac;
226   int rv;
227
228   error = NULL;
229   instance = sw_if_index = ~0;
230   mac_address_set_zero (&mac);
231
232   if (unformat_user (input, unformat_line_input, line_input))
233     {
234       while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
235         {
236           if (unformat (line_input, "mac %U", unformat_mac_address_t, &mac))
237             ;
238           else if (unformat (line_input, "instance %d", &instance))
239             ;
240           else
241             {
242               error = clib_error_return (0, "unknown input: %U",
243                                          format_unformat_error, line_input);
244               break;
245             }
246         }
247
248       unformat_free (line_input);
249
250       if (error)
251         return error;
252     }
253
254   rv = l2_bvi_create (instance, &mac, &sw_if_index);
255
256   if (rv)
257     return clib_error_return (0, "BVI create failed");
258
259   vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
260                    sw_if_index);
261   return 0;
262 }
263
264 /*?
265  * Create a BVI interface. Optionally, a MAC Address can be
266  * provided. If not provided, 0b:0b::00:00:00:<instance> will be used.
267  *
268  * @cliexpar
269  * The following two command syntaxes are equivalent:
270  * @cliexcmd{bvi create [mac <mac-addr>] [instance <instance>]}
271  * Example of how to create a bvi interface:
272  * @cliexcmd{bvi create}
273 ?*/
274 VLIB_CLI_COMMAND (l2_bvi_create_command, static) = {
275   .path = "bvi create",
276   .short_help = "bvi create [mac <mac-addr>] [instance <instance>]",
277   .function = l2_bvi_create_cli,
278 };
279
280 static clib_error_t *
281 l2_bvi_delete_cli (vlib_main_t * vm,
282                    unformat_input_t * input, vlib_cli_command_t * cmd)
283 {
284   vnet_main_t *vnm;
285   u32 sw_if_index;
286   int rv;
287
288   vnm = vnet_get_main ();
289   sw_if_index = ~0;
290
291   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
292     {
293       if (unformat
294           (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
295         ;
296       else
297         break;
298     }
299
300   if (~0 != sw_if_index)
301     {
302       rv = l2_bvi_delete (sw_if_index);
303
304       if (rv)
305         return clib_error_return (0, "BVI delete failed");
306     }
307   else
308     return clib_error_return (0, "no such interface: %U",
309                               format_unformat_error, input);
310
311   return 0;
312 }
313
314 /*?
315  * Delete a BVI interface.
316  *
317  * @cliexpar
318  * The following two command syntaxes are equivalent:
319  * @cliexcmd{bvi delete <interface>}
320  * Example of how to create a bvi interface:
321  * @cliexcmd{bvi delete bvi0}
322 ?*/
323 VLIB_CLI_COMMAND (l2_bvi_delete_command, static) = {
324   .path = "bvi delete",
325   .short_help = "bvi delete <interface>",
326   .function = l2_bvi_delete_cli,
327 };
328
329
330 /*
331  * fd.io coding-style-patch-verification: ON
332  *
333  * Local Variables:
334  * eval: (c-set-style "gnu")
335  * End:
336  */