From e0792fdff6a9cc141f1cb4c6c1d2ac478cf44ee2 Mon Sep 17 00:00:00 2001 From: Matthew Smith Date: Fri, 12 Jul 2019 11:48:24 -0500 Subject: [PATCH] interface: callback to manage extra MAC addresses Type: feature New callback vnet_hw_interface_add_del_mac_address(). Add or delete secondary MAC addresses on a hardware interface. This will allow packets to be processed which have a destination MAC address other than the primary programmed MAC address without needing to put the device into promiscuous mode. Change-Id: I6beecbcb8932fc1fe45b567f76fa3706feefae2c Signed-off-by: Matthew Smith --- src/vat/api_format.c | 52 ++++++++++++++++ src/vnet/interface.api | 18 +++++- src/vnet/interface.c | 42 +++++++++++++ src/vnet/interface.h | 10 +++ src/vnet/interface_api.c | 29 +++++++++ src/vnet/interface_cli.c | 152 +++++++++++++++++++++++++++++++++++++++++++++ src/vnet/interface_funcs.h | 6 ++ 7 files changed, 308 insertions(+), 1 deletion(-) diff --git a/src/vat/api_format.c b/src/vat/api_format.c index 4c1a85f58e6..9f02507e4f7 100644 --- a/src/vat/api_format.c +++ b/src/vat/api_format.c @@ -5173,6 +5173,7 @@ _(l2_interface_pbb_tag_rewrite_reply) \ _(set_punt_reply) \ _(feature_enable_disable_reply) \ _(sw_interface_tag_add_del_reply) \ +_(sw_interface_add_del_mac_address_reply) \ _(hw_interface_set_mtu_reply) \ _(p2p_ethernet_add_reply) \ _(p2p_ethernet_del_reply) \ @@ -5481,6 +5482,7 @@ _(IP_TABLE_DETAILS, ip_table_details) \ _(IP_ROUTE_DETAILS, ip_route_details) \ _(FEATURE_ENABLE_DISABLE_REPLY, feature_enable_disable_reply) \ _(SW_INTERFACE_TAG_ADD_DEL_REPLY, sw_interface_tag_add_del_reply) \ +_(SW_INTERFACE_ADD_DEL_MAC_ADDRESS_REPLY, sw_interface_add_del_mac_address_reply) \ _(L2_XCONNECT_DETAILS, l2_xconnect_details) \ _(HW_INTERFACE_SET_MTU_REPLY, hw_interface_set_mtu_reply) \ _(IP_NEIGHBOR_DETAILS, ip_neighbor_details) \ @@ -19999,6 +20001,54 @@ api_sw_interface_tag_add_del (vat_main_t * vam) return ret; } +static int +api_sw_interface_add_del_mac_address (vat_main_t * vam) +{ + unformat_input_t *i = vam->input; + vl_api_mac_address_t mac = { 0 }; + vl_api_sw_interface_add_del_mac_address_t *mp; + u32 sw_if_index = ~0; + u8 is_add = 1; + u8 mac_set = 0; + int ret; + + while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) + { + if (unformat (i, "%U", api_unformat_sw_if_index, vam, &sw_if_index)) + ; + else if (unformat (i, "sw_if_index %d", &sw_if_index)) + ; + else if (unformat (i, "%U", unformat_vl_api_mac_address, &mac)) + mac_set++; + else if (unformat (i, "del")) + is_add = 0; + else + break; + } + + if (sw_if_index == ~0) + { + errmsg ("missing interface name or sw_if_index"); + return -99; + } + + if (!mac_set) + { + errmsg ("missing MAC address"); + return -99; + } + + /* Construct the API message */ + M (SW_INTERFACE_ADD_DEL_MAC_ADDRESS, mp); + mp->sw_if_index = ntohl (sw_if_index); + mp->is_add = is_add; + clib_memcpy (&mp->addr, &mac, sizeof (mac)); + + S (mp); + W (ret); + return ret; +} + static void vl_api_l2_xconnect_details_t_handler (vl_api_l2_xconnect_details_t * mp) { @@ -21753,6 +21803,8 @@ _(feature_enable_disable, "arc_name " \ "feature_name | sw_if_index [disable]") \ _(sw_interface_tag_add_del, " | sw_if_index tag " \ "[disable]") \ +_(sw_interface_add_del_mac_address, " | sw_if_index " \ + "mac [del]") \ _(l2_xconnect_dump, "") \ _(hw_interface_set_mtu, " | hw_if_index mtu ") \ _(ip_neighbor_dump, "[ip6] | sw_if_index ") \ diff --git a/src/vnet/interface.api b/src/vnet/interface.api index ff0946538f4..02d7a505a51 100644 --- a/src/vnet/interface.api +++ b/src/vnet/interface.api @@ -14,7 +14,7 @@ * limitations under the License. */ -option version = "3.1.0"; +option version = "3.2.0"; import "vnet/interface_types.api"; import "vnet/ethernet/ethernet_types.api"; @@ -310,6 +310,22 @@ autoreply define sw_interface_tag_add_del string tag[64]; }; +/** \brief Add or delete a secondary MAC address on an interface + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param sw_if_index - the interface whose MAC will be set + @param mac_addr - the new MAC address + @param is_add - 0 to delete, != 0 to add +*/ +autoreply define sw_interface_add_del_mac_address +{ + u32 client_index; + u32 context; + u32 sw_if_index; + vl_api_mac_address_t addr; + u8 is_add; +}; + /** \brief Set an interface's MAC address @param client_index - opaque cookie to identify the sender @param context - sender context, to match reply w/ request diff --git a/src/vnet/interface.c b/src/vnet/interface.c index 0f6b8aeab07..0b4d78afbde 100644 --- a/src/vnet/interface.c +++ b/src/vnet/interface.c @@ -1427,6 +1427,48 @@ vnet_rename_interface (vnet_main_t * vnm, u32 hw_if_index, char *new_name) return error; } +clib_error_t * +vnet_hw_interface_add_del_mac_address (vnet_main_t * vnm, + u32 hw_if_index, + const u8 * mac_address, u8 is_add) +{ + clib_error_t *error = 0; + vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index); + + vnet_device_class_t *dev_class = + vnet_get_device_class (vnm, hi->dev_class_index); + + if (!hi->hw_address) + { + error = + clib_error_return + (0, "Secondary MAC Addresses not supported for interface index %u", + hw_if_index); + goto done; + } + + if (dev_class->mac_addr_add_del_function) + error = dev_class->mac_addr_add_del_function (hi, mac_address, is_add); + + if (!error) + { + vnet_hw_interface_class_t *hw_class; + + hw_class = vnet_get_hw_interface_class (vnm, hi->hw_class_index); + + if (NULL != hw_class->mac_addr_add_del_function) + error = hw_class->mac_addr_add_del_function (hi, mac_address, is_add); + } + + /* If no errors, add to the list of secondary MACs on the ethernet intf */ + if (!error) + ethernet_interface_add_del_address (ðernet_main, hw_if_index, + mac_address, is_add); + +done: + return error; +} + static clib_error_t * vnet_hw_interface_change_mac_address_helper (vnet_main_t * vnm, u32 hw_if_index, diff --git a/src/vnet/interface.h b/src/vnet/interface.h index 7d73c5f6473..e6450f5bf59 100644 --- a/src/vnet/interface.h +++ b/src/vnet/interface.h @@ -74,6 +74,10 @@ typedef clib_error_t *(vnet_interface_set_mac_address_function_t) (struct vnet_hw_interface_t * hi, const u8 * old_address, const u8 * new_address); +/* Interface add/del additional mac address callback */ +typedef clib_error_t *(vnet_interface_add_del_mac_address_function_t) + (struct vnet_hw_interface_t * hi, const u8 * address, u8 is_add); + /* Interface set rx mode callback. */ typedef clib_error_t *(vnet_interface_set_rx_mode_function_t) (struct vnet_main_t * vnm, u32 if_index, u32 queue_id, @@ -266,6 +270,9 @@ typedef struct _vnet_device_class /* Function to set mac address. */ vnet_interface_set_mac_address_function_t *mac_addr_change_function; + + /* Function to add/delete additional MAC addresses */ + vnet_interface_add_del_mac_address_function_t *mac_addr_add_del_function; } vnet_device_class_t; #ifndef CLIB_MARCH_VARIANT @@ -392,6 +399,9 @@ typedef struct _vnet_hw_interface_class /* Function to call when link MAC changes. */ vnet_interface_set_mac_address_function_t *mac_addr_change_function; + /* Function to add/delete additional MAC addresses */ + vnet_interface_add_del_mac_address_function_t *mac_addr_add_del_function; + /* Format function to display interface name. */ format_function_t *format_interface_name; diff --git a/src/vnet/interface_api.c b/src/vnet/interface_api.c index 687f599b616..4d5dab30f38 100644 --- a/src/vnet/interface_api.c +++ b/src/vnet/interface_api.c @@ -68,6 +68,7 @@ _(SW_INTERFACE_GET_TABLE, sw_interface_get_table) \ _(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered) \ _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats) \ _(SW_INTERFACE_TAG_ADD_DEL, sw_interface_tag_add_del) \ +_(SW_INTERFACE_ADD_DEL_MAC_ADDRESS, sw_interface_add_del_mac_address) \ _(SW_INTERFACE_SET_MAC_ADDRESS, sw_interface_set_mac_address) \ _(SW_INTERFACE_GET_MAC_ADDRESS, sw_interface_get_mac_address) \ _(CREATE_VLAN_SUBIF, create_vlan_subif) \ @@ -899,6 +900,34 @@ out: REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY); } +static void vl_api_sw_interface_add_del_mac_address_t_handler + (vl_api_sw_interface_add_del_mac_address_t * mp) +{ + vl_api_sw_interface_add_del_mac_address_reply_t *rmp; + vnet_main_t *vnm = vnet_get_main (); + u32 sw_if_index = ntohl (mp->sw_if_index); + vnet_hw_interface_t *hi; + clib_error_t *error; + int rv = 0; + + VALIDATE_SW_IF_INDEX (mp); + + /* for subifs, the MAC should be changed on the actual hw if */ + hi = vnet_get_sup_hw_interface (vnm, sw_if_index); + error = vnet_hw_interface_add_del_mac_address (vnm, hi->hw_if_index, + mp->addr, mp->is_add); + if (error) + { + rv = VNET_API_ERROR_UNIMPLEMENTED; + clib_error_report (error); + goto out; + } + + BAD_SW_IF_INDEX_LABEL; +out: + REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_MAC_ADDRESS_REPLY); +} + static void vl_api_sw_interface_set_mac_address_t_handler (vl_api_sw_interface_set_mac_address_t * mp) { diff --git a/src/vnet/interface_cli.c b/src/vnet/interface_cli.c index c622a605be8..a66e1576bde 100644 --- a/src/vnet/interface_cli.c +++ b/src/vnet/interface_cli.c @@ -1164,6 +1164,158 @@ VLIB_CLI_COMMAND (set_interface_mtu_cmd, static) = { }; /* *INDENT-ON* */ +static clib_error_t * +show_interface_sec_mac_addr_fn (vlib_main_t * vm, unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + vnet_main_t *vnm = vnet_get_main (); + vnet_interface_main_t *im = &vnm->interface_main; + ethernet_main_t *em = ðernet_main; + u32 sw_if_index = ~0; + vnet_sw_interface_t *si, *sorted_sis = 0; + + if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index)) + { + si = pool_elt_at_index (im->sw_interfaces, sw_if_index); + vec_add1 (sorted_sis, si[0]); + } + + /* if an interface name was not passed, get all interfaces */ + if (vec_len (sorted_sis) == 0) + { + sorted_sis = + vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces)); + _vec_len (sorted_sis) = 0; + /* *INDENT-OFF* */ + pool_foreach (si, im->sw_interfaces, + ({ + int visible = vnet_swif_is_api_visible (si); + if (visible) + vec_add1 (sorted_sis, si[0]);} + )); + /* *INDENT-ON* */ + /* Sort by name. */ + vec_sort_with_function (sorted_sis, sw_interface_name_compare); + } + + vec_foreach (si, sorted_sis) + { + vnet_sw_interface_t *sup_si; + ethernet_interface_t *ei; + + sup_si = vnet_get_sup_sw_interface (vnm, si->sw_if_index); + ei = ethernet_get_interface (em, sup_si->hw_if_index); + + vlib_cli_output (vm, "%U (%s):", + format_vnet_sw_if_index_name, vnm, si->sw_if_index, + (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? + "up" : "dn"); + + if (ei && ei->secondary_addrs) + { + mac_address_t *sec_addr; + + vec_foreach (sec_addr, ei->secondary_addrs) + { + vlib_cli_output (vm, " %U", format_mac_address_t, sec_addr); + } + } + } + + vec_free (sorted_sis); + return 0; +} + +/*? + * This command is used to display interface secondary mac addresses. + * + * @cliexpar + * Example of how to display interface secondary mac addresses: + * @cliexstart{show interface secondary-mac-address} + * @cliexend +?*/ +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (show_interface_sec_mac_addr, static) = { + .path = "show interface secondary-mac-address", + .short_help = "show interface secondary-mac-address []", + .function = show_interface_sec_mac_addr_fn, +}; +/* *INDENT-ON* */ + +static clib_error_t * +interface_add_del_mac_address (vlib_main_t * vm, unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + vnet_main_t *vnm = vnet_get_main (); + vnet_sw_interface_t *si = NULL; + clib_error_t *error = 0; + u32 sw_if_index = ~0; + u8 mac[6] = { 0 }; + u8 is_add, is_del; + + is_add = is_del = 0; + + if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index)) + { + error = clib_error_return (0, "unknown interface `%U'", + format_unformat_error, input); + goto done; + } + if (!unformat_user (input, unformat_ethernet_address, mac)) + { + error = clib_error_return (0, "expected mac address `%U'", + format_unformat_error, input); + goto done; + } + + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (input, "add")) + is_add = 1; + else if (unformat (input, "del")) + is_del = 1; + else + break; + } + + if (is_add == is_del) + { + error = clib_error_return (0, "must choose one of add or del"); + goto done; + } + + si = vnet_get_sw_interface (vnm, sw_if_index); + error = + vnet_hw_interface_add_del_mac_address (vnm, si->hw_if_index, mac, is_add); + +done: + return error; +} + +/*? + * The 'set interface secondary-mac-address ' command allows adding + * or deleting extra MAC addresses on a given interface without changing the + * default MAC address. This could allow packets sent to these MAC addresses + * to be received without setting the interface to promiscuous mode. + * Not all interfaces support this operation. The ones that do are mostly + * hardware NICs, though virtio does also. + * + * @cliexpar + * @parblock + * Example of how to add a secondary MAC Address on an interface: + * @cliexcmd{set interface secondary-mac-address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01 add} + * Example of how to delete a secondary MAC address from an interface: + * @cliexcmd{set interface secondary-mac-address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01 del} + * @endparblock +?*/ +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (interface_add_del_mac_address_cmd, static) = { + .path = "set interface secondary-mac-address", + .short_help = "set interface secondary-mac-address [(add|del)]", + .function = interface_add_del_mac_address, +}; +/* *INDENT-ON* */ + static clib_error_t * set_interface_mac_address (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) diff --git a/src/vnet/interface_funcs.h b/src/vnet/interface_funcs.h index c0ad81c6d87..9241b34d59d 100644 --- a/src/vnet/interface_funcs.h +++ b/src/vnet/interface_funcs.h @@ -390,6 +390,12 @@ void vnet_hw_interface_init_for_class (vnet_main_t * vnm, u32 hw_if_index, clib_error_t *vnet_rename_interface (vnet_main_t * vnm, u32 hw_if_index, char *new_name); +/* Add/delete secondary interface mac address*/ +clib_error_t *vnet_hw_interface_add_del_mac_address (vnet_main_t * vnm, + u32 hw_if_index, + const u8 * mac_address, + u8 is_add); + /* Change interface mac address*/ clib_error_t *vnet_hw_interface_change_mac_address (vnet_main_t * vnm, u32 hw_if_index, -- 2.16.6