2 * Copyright (c) 2015 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * ethernet_interface.c: ethernet interfaces
18 * Copyright (c) 2008 Eliot Dresselhaus
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 #include <vnet/vnet.h>
41 #include <vnet/ip/ip.h>
42 #include <vnet/pg/pg.h>
43 #include <vnet/ethernet/ethernet.h>
45 static uword ethernet_set_rewrite (vnet_main_t * vnm,
50 uword max_rewrite_bytes)
52 vnet_sw_interface_t * sub_sw = vnet_get_sw_interface (vnm, sw_if_index);
53 vnet_sw_interface_t * sup_sw = vnet_get_sup_sw_interface (vnm, sw_if_index);
54 vnet_hw_interface_t * hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
55 ethernet_main_t * em = ðernet_main;
56 ethernet_interface_t * ei;
57 ethernet_header_t * h = rewrite;
59 uword n_bytes = sizeof (h[0]);
61 if (sub_sw != sup_sw) {
62 if (sub_sw->sub.eth.flags.one_tag) {
63 n_bytes += sizeof (ethernet_vlan_header_t);
64 } else if (sub_sw->sub.eth.flags.two_tags) {
65 n_bytes += 2 * (sizeof (ethernet_vlan_header_t));
67 // Check for encaps that are not supported for L3 interfaces
68 if (!(sub_sw->sub.eth.flags.exact_match) ||
69 (sub_sw->sub.eth.flags.default_sub) ||
70 (sub_sw->sub.eth.flags.outer_vlan_id_any) ||
71 (sub_sw->sub.eth.flags.inner_vlan_id_any)) {
76 if (n_bytes > max_rewrite_bytes)
80 #define _(a,b) case VNET_L3_PACKET_TYPE_##a: type = ETHERNET_TYPE_##b; break
83 _ (MPLS_UNICAST, MPLS_UNICAST);
84 _ (MPLS_MULTICAST, MPLS_MULTICAST);
91 ei = pool_elt_at_index (em->interfaces, hw->hw_instance);
92 memcpy (h->src_address, ei->address, sizeof (h->src_address));
94 memcpy (h->dst_address, dst_address, sizeof (h->dst_address));
96 memset (h->dst_address, ~0, sizeof (h->dst_address)); /* broadcast */
98 if (sub_sw->sub.eth.flags.one_tag) {
99 ethernet_vlan_header_t * outer = (void *) (h + 1);
101 h->type = sub_sw->sub.eth.flags.dot1ad ?
102 clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD) :
103 clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
104 outer->priority_cfi_and_id = clib_host_to_net_u16 (sub_sw->sub.eth.outer_vlan_id);
105 outer->type = clib_host_to_net_u16 (type);
107 } else if (sub_sw->sub.eth.flags.two_tags) {
108 ethernet_vlan_header_t * outer = (void *) (h + 1);
109 ethernet_vlan_header_t * inner = (void *) (outer + 1);
111 h->type = sub_sw->sub.eth.flags.dot1ad ?
112 clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD) :
113 clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
114 outer->priority_cfi_and_id = clib_host_to_net_u16 (sub_sw->sub.eth.outer_vlan_id);
115 outer->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
116 inner->priority_cfi_and_id = clib_host_to_net_u16 (sub_sw->sub.eth.inner_vlan_id);
117 inner->type = clib_host_to_net_u16 (type);
120 h->type = clib_host_to_net_u16 (type);
126 VNET_HW_INTERFACE_CLASS (ethernet_hw_interface_class) = {
128 .format_address = format_ethernet_address,
129 .format_header = format_ethernet_header_with_length,
130 .unformat_hw_address = unformat_ethernet_address,
131 .unformat_header = unformat_ethernet_header,
132 .set_rewrite = ethernet_set_rewrite,
135 uword unformat_ethernet_interface (unformat_input_t * input, va_list * args)
137 vnet_main_t * vnm = va_arg (*args, vnet_main_t *);
138 u32 * result = va_arg (*args, u32 *);
140 ethernet_main_t * em = ðernet_main;
141 ethernet_interface_t * eif;
143 if (! unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
146 eif = ethernet_get_interface (em, hw_if_index);
149 *result = hw_if_index;
156 ethernet_register_interface (vnet_main_t * vnm,
160 u32 * hw_if_index_return,
161 ethernet_flag_change_function_t flag_change)
163 ethernet_main_t * em = ðernet_main;
164 ethernet_interface_t * ei;
165 vnet_hw_interface_t * hi;
166 clib_error_t * error = 0;
169 pool_get (em->interfaces, ei);
170 ei->flag_change = flag_change;
172 hw_if_index = vnet_register_interface
174 dev_class_index, dev_instance,
175 ethernet_hw_interface_class.index,
176 ei - em->interfaces);
177 *hw_if_index_return = hw_if_index;
179 hi = vnet_get_hw_interface (vnm, hw_if_index);
181 ethernet_setup_node (vnm->vlib_main, hi->output_node_index);
183 hi->min_packet_bytes = ETHERNET_MIN_PACKET_BYTES;
184 hi->max_packet_bytes = ETHERNET_MAX_PACKET_BYTES;
185 hi->per_packet_overhead_bytes =
186 /* preamble */ 8 + /* inter frame gap */ 12;
188 /* Standard default ethernet MTU. */
189 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
191 memcpy (ei->address, address, sizeof (ei->address));
192 vec_free (hi->hw_address);
193 vec_add (hi->hw_address, address, sizeof (ei->address));
197 pool_put (em->interfaces, ei);
204 ethernet_delete_interface (vnet_main_t * vnm, u32 hw_if_index)
206 ethernet_main_t * em = ðernet_main;
207 ethernet_interface_t * ei;
208 vnet_hw_interface_t * hi;
209 main_intf_t * main_intf;
210 vlan_table_t * vlan_table;
213 hi = vnet_get_hw_interface (vnm, hw_if_index);
214 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
216 /* Delete vlan mapping table for dot1q and dot1ad. */
217 main_intf = vec_elt_at_index (em->main_intfs, hi->hw_if_index);
218 if (main_intf->dot1q_vlans) {
219 vlan_table = vec_elt_at_index (em->vlan_pool, main_intf->dot1q_vlans);
220 for (idx=0; idx<ETHERNET_N_VLAN; idx++ ) {
221 if (vlan_table->vlans[idx].qinqs) {
222 pool_put_index(em->qinq_pool, vlan_table->vlans[idx].qinqs);
225 pool_put_index(em->vlan_pool, main_intf->dot1q_vlans);
227 if (main_intf->dot1ad_vlans) {
228 vlan_table = vec_elt_at_index (em->vlan_pool, main_intf->dot1ad_vlans);
229 for (idx=0; idx<ETHERNET_N_VLAN; idx++ ) {
230 if (vlan_table->vlans[idx].qinqs) {
231 pool_put_index(em->qinq_pool, vlan_table->vlans[idx].qinqs);
234 pool_put_index(em->vlan_pool, main_intf->dot1ad_vlans);
237 vnet_delete_hw_interface (vnm, hw_if_index);
238 pool_put (em->interfaces, ei);
242 ethernet_set_flags (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
244 ethernet_main_t * em = ðernet_main;
245 vnet_hw_interface_t * hi;
246 ethernet_interface_t * ei;
248 hi = vnet_get_hw_interface (vnm, hw_if_index);
250 ASSERT (hi->hw_class_index == ethernet_hw_interface_class.index);
252 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
254 return ei->flag_change (vnm, hi, flags);
258 #define VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT VNET_INTERFACE_TX_N_NEXT
260 /* Echo packets back to ethernet input. */
262 simulated_ethernet_interface_tx (vlib_main_t * vm,
263 vlib_node_runtime_t * node,
264 vlib_frame_t * frame)
266 u32 n_left_from, n_left_to_next, n_copy, * from, * to_next;
267 u32 next_index = VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT;
271 n_left_from = frame->n_vectors;
272 from = vlib_frame_args (frame);
274 while (n_left_from > 0)
276 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
278 n_copy = clib_min (n_left_from, n_left_to_next);
280 memcpy (to_next, from, n_copy * sizeof (from[0]));
281 n_left_to_next -= n_copy;
282 n_left_from -= n_copy;
283 for (i = 0; i < n_copy; i++)
285 b = vlib_get_buffer (vm, from[i]);
286 /* Set up RX and TX indices as if received from a real driver */
287 vnet_buffer (b)->sw_if_index[VLIB_RX] =
288 vnet_buffer (b)->sw_if_index[VLIB_TX];
289 vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32) ~0;
292 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
298 static u8 * format_simulated_ethernet_name (u8 * s, va_list * args)
300 u32 dev_instance = va_arg (*args, u32);
301 return format (s, "loop%d", dev_instance);
304 static clib_error_t *
305 simulated_ethernet_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
307 u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
308 VNET_HW_INTERFACE_FLAG_LINK_UP : 0;
309 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
313 VNET_DEVICE_CLASS (ethernet_simulated_device_class) = {
315 .format_device_name = format_simulated_ethernet_name,
316 .tx_function = simulated_ethernet_interface_tx,
317 .admin_up_down_function = simulated_ethernet_admin_up_down,
320 int vnet_create_loopback_interface (u32 * sw_if_indexp, u8 *mac_address)
322 vnet_main_t * vnm = vnet_get_main();
323 vlib_main_t * vm = vlib_get_main();
324 clib_error_t * error;
328 vnet_hw_interface_t * hw_if;
332 ASSERT(sw_if_indexp);
334 *sw_if_indexp = (u32)~0;
336 memset (address, 0, sizeof (address));
339 * Default MAC address (dead:0000:0000 + instance) is allocated
340 * if zero mac_address is configured. Otherwise, user-configurable MAC
341 * address is programmed on the loopback interface.
343 if (memcmp (address, mac_address, sizeof (address)))
344 memcpy (address, mac_address, sizeof (address));
349 address[5] = instance;
352 error = ethernet_register_interface
354 ethernet_simulated_device_class.index,
358 /* flag change */ 0);
362 rv = VNET_API_ERROR_INVALID_REGISTRATION;
363 clib_error_report(error);
367 hw_if = vnet_get_hw_interface (vnm, hw_if_index);
368 slot = vlib_node_add_named_next_with_slot
369 (vm, hw_if->tx_node_index,
371 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
372 ASSERT (slot == VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
375 vnet_sw_interface_t * si = vnet_get_hw_sw_interface (vnm, hw_if_index);
376 *sw_if_indexp = si->sw_if_index;
382 static clib_error_t *
383 create_simulated_ethernet_interfaces (vlib_main_t * vm,
384 unformat_input_t * input,
385 vlib_cli_command_t * cmd)
391 memset (mac_address, 0, sizeof (mac_address));
393 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
395 if (unformat (input, "mac %U", unformat_ethernet_address, mac_address))
401 rv = vnet_create_loopback_interface (&sw_if_index, mac_address);
404 return clib_error_return (0, "vnet_create_loopback_interface failed");
409 VLIB_CLI_COMMAND (create_simulated_ethernet_interface_command, static) = {
410 .path = "loopback create-interface",
411 .short_help = "Create Loopback ethernet interface [mac <mac-addr>]",
412 .function = create_simulated_ethernet_interfaces,
415 VLIB_CLI_COMMAND (create_loopback_interface_command, static) = {
416 .path = "create loopback interface",
417 .short_help = "create loopback interface [mac <mac-addr>]",
418 .function = create_simulated_ethernet_interfaces,
421 ethernet_interface_t *
422 ethernet_get_interface (ethernet_main_t * em, u32 hw_if_index)
424 vnet_hw_interface_t * i = vnet_get_hw_interface (vnet_get_main(), hw_if_index);
425 return (i->hw_class_index == ethernet_hw_interface_class.index
426 ? pool_elt_at_index (em->interfaces, i->hw_instance)
430 int vnet_delete_loopback_interface (u32 sw_if_index)
432 vnet_main_t * vnm = vnet_get_main();
433 vnet_sw_interface_t * si;
435 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
437 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
439 si = vnet_get_sw_interface (vnm, sw_if_index);
440 ethernet_delete_interface (vnm, si->hw_if_index);
445 static clib_error_t *
446 delete_simulated_ethernet_interfaces (vlib_main_t * vm,
447 unformat_input_t * input,
448 vlib_cli_command_t * cmd)
451 u32 sw_if_index = ~0;
452 vnet_main_t * vnm = vnet_get_main();
454 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
456 if (unformat (input, "intfc %U",
457 unformat_vnet_sw_interface, vnm, &sw_if_index))
463 if (sw_if_index == ~0)
464 return clib_error_return (0, "interface not specified");
466 rv = vnet_delete_loopback_interface (sw_if_index);
469 return clib_error_return (0, "vnet_delete_loopback_interface failed");
474 VLIB_CLI_COMMAND (delete_simulated_ethernet_interface_command, static) = {
475 .path = "loopback delete-interface",
476 .short_help = "Delete Loopback ethernet interface intfc <interface>",
477 .function = delete_simulated_ethernet_interfaces,