Skip to content

Commit

Permalink
ethdev: fix MAC address occupies two entries
Browse files Browse the repository at this point in the history
[ upstream commit 8f02f472a29432650d999969359d6a49ea6aadca ]

The dev->data->mac_addrs[0] will be changed to a new MAC address when
applications modify the default MAC address by .mac_addr_set(). However,
if the new default one has been added as a non-default MAC address by
.mac_addr_add(), the .mac_addr_set() didn't check this address.
As a result, this MAC address occupies two entries in the list. Like:
add(MAC1)
add(MAC2)
add(MAC3)
add(MAC4)
set_default(MAC3)
default=MAC3, the rest of the list=MAC1, MAC2, MAC3, MAC4
Note: MAC3 occupies two entries.

But .mac_addr_set() cannot remove it implicitly in case of MAC address
shrinking in the list.
So this patch adds a check on whether the new default address was
already in the list and if so requires the user to remove it first.

In addition, this patch documents the position of the default MAC
address and address unique in the list.

Fixes: 854d8ad ("ethdev: add default mac address modifier")

Signed-off-by: Huisong Li <lihuisong@huawei.com>
Acked-by: Chengwen Feng <fengchengwen@huawei.com>
Acked-by: Thomas Monjalon <thomas@monjalon.net>
Reviewed-by: Ferruh Yigit <ferruh.yigit@amd.com>
  • Loading branch information
LiHuiSong1 authored and kevintraynor committed Jul 11, 2023
1 parent 80e17cb commit b3741df
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/ethdev/rte_ethdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -4485,6 +4485,7 @@ int
rte_eth_dev_default_mac_addr_set(uint16_t port_id, struct rte_ether_addr *addr)
{
struct rte_eth_dev *dev;
int index;
int ret;

RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
Expand All @@ -4502,6 +4503,15 @@ rte_eth_dev_default_mac_addr_set(uint16_t port_id, struct rte_ether_addr *addr)

RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_set, -ENOTSUP);

/* Keep address unique in dev->data->mac_addrs[]. */
index = eth_dev_get_mac_addr_index(port_id, addr);
if (index > 0) {
RTE_ETHDEV_LOG(ERR,
"New default address for port %u was already in the address list. Please remove it first.\n",
port_id);
return -EEXIST;
}

ret = (*dev->dev_ops->mac_addr_set)(dev, addr);
if (ret < 0)
return ret;
Expand Down
4 changes: 4 additions & 0 deletions lib/ethdev/rte_ethdev.h
Original file line number Diff line number Diff line change
Expand Up @@ -4163,6 +4163,9 @@ int rte_eth_dev_mac_addr_remove(uint16_t port_id,

/**
* Set the default MAC address.
* It replaces the address at index 0 of the MAC address list.
* If the address was already in the MAC address list,
* please remove it first.
*
* @param port_id
* The port identifier of the Ethernet device.
Expand All @@ -4173,6 +4176,7 @@ int rte_eth_dev_mac_addr_remove(uint16_t port_id,
* - (-ENOTSUP) if hardware doesn't support.
* - (-ENODEV) if *port* invalid.
* - (-EINVAL) if MAC address is invalid.
* - (-EEXIST) if MAC address was already in the address list.
*/
int rte_eth_dev_default_mac_addr_set(uint16_t port_id,
struct rte_ether_addr *mac_addr);
Expand Down

0 comments on commit b3741df

Please sign in to comment.