summaryrefslogtreecommitdiffstats
path: root/src/vnet
diff options
context:
space:
mode:
authorMatthew Smith <mgsmith@netgate.com>2019-07-12 11:48:24 -0500
committerDamjan Marion <dmarion@me.com>2019-10-09 10:30:05 +0000
commite0792fdff6a9cc141f1cb4c6c1d2ac478cf44ee2 (patch)
treeba4d3e098043531cc5957f34e793dd29b0ee851d /src/vnet
parent6a6af6ea1a77b5818e717047b5d01251ef6d024a (diff)
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 <mgsmith@netgate.com>
Diffstat (limited to 'src/vnet')
-rw-r--r--src/vnet/interface.api18
-rw-r--r--src/vnet/interface.c42
-rw-r--r--src/vnet/interface.h10
-rw-r--r--src/vnet/interface_api.c29
-rw-r--r--src/vnet/interface_cli.c152
-rw-r--r--src/vnet/interface_funcs.h6
6 files changed, 256 insertions, 1 deletions
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 (&ethernet_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
@@ -1165,6 +1165,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 = &ethernet_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 [<interface>]",
+ .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 '<em>set interface secondary-mac-address </em>' 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 <interface> <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,