1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
12 #include <netinet/in.h>
13 #include <sys/ioctl.h>
14 #include <arpa/inet.h>
17 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
19 #pragma GCC diagnostic ignored "-Wpedantic"
21 #include <infiniband/verbs.h>
23 #pragma GCC diagnostic error "-Wpedantic"
26 #include <rte_ether.h>
27 #include <rte_ethdev_driver.h>
28 #include <rte_common.h>
31 #include "mlx5_utils.h"
32 #include "mlx5_rxtx.h"
33 #include "mlx5_defs.h"
36 * Get MAC address by querying netdevice.
39 * Pointer to Ethernet device.
41 * MAC address output buffer.
44 * 0 on success, a negative errno value otherwise and rte_errno is set.
47 mlx5_get_mac(struct rte_eth_dev *dev, uint8_t (*mac)[RTE_ETHER_ADDR_LEN])
52 ret = mlx5_ifreq(dev, SIOCGIFHWADDR, &request);
55 memcpy(mac, request.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
60 * Remove a MAC address from the internal array.
63 * Pointer to Ethernet device structure.
68 mlx5_internal_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
70 struct mlx5_priv *priv = dev->data->dev_private;
71 const int vf = priv->config.vf;
73 assert(index < MLX5_MAX_MAC_ADDRESSES);
74 if (rte_is_zero_ether_addr(&dev->data->mac_addrs[index]))
77 mlx5_nl_mac_addr_remove(dev, &dev->data->mac_addrs[index],
79 memset(&dev->data->mac_addrs[index], 0, sizeof(struct rte_ether_addr));
83 * Adds a MAC address to the internal array.
86 * Pointer to Ethernet device structure.
88 * MAC address to register.
93 * 0 on success, a negative errno value otherwise and rte_errno is set.
96 mlx5_internal_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac,
99 struct mlx5_priv *priv = dev->data->dev_private;
100 const int vf = priv->config.vf;
103 assert(index < MLX5_MAX_MAC_ADDRESSES);
104 if (rte_is_zero_ether_addr(mac)) {
108 /* First, make sure this address isn't already configured. */
109 for (i = 0; (i != MLX5_MAX_MAC_ADDRESSES); ++i) {
110 /* Skip this index, it's going to be reconfigured. */
113 if (memcmp(&dev->data->mac_addrs[i], mac, sizeof(*mac)))
115 /* Address already configured elsewhere, return with error. */
116 rte_errno = EADDRINUSE;
120 int ret = mlx5_nl_mac_addr_add(dev, mac, index);
125 dev->data->mac_addrs[index] = *mac;
130 * DPDK callback to remove a MAC address.
133 * Pointer to Ethernet device structure.
138 mlx5_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
142 if (index >= MLX5_MAX_UC_MAC_ADDRESSES)
144 mlx5_internal_mac_addr_remove(dev, index);
145 if (!dev->data->promiscuous) {
146 ret = mlx5_traffic_restart(dev);
148 DRV_LOG(ERR, "port %u cannot restart traffic: %s",
149 dev->data->port_id, strerror(rte_errno));
154 * DPDK callback to add a MAC address.
157 * Pointer to Ethernet device structure.
159 * MAC address to register.
163 * VMDq pool index to associate address with (ignored).
166 * 0 on success, a negative errno value otherwise and rte_errno is set.
169 mlx5_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac,
170 uint32_t index, uint32_t vmdq __rte_unused)
174 if (index >= MLX5_MAX_UC_MAC_ADDRESSES) {
178 ret = mlx5_internal_mac_addr_add(dev, mac, index);
181 if (!dev->data->promiscuous)
182 return mlx5_traffic_restart(dev);
187 * DPDK callback to set primary MAC address.
190 * Pointer to Ethernet device structure.
192 * MAC address to register.
195 * 0 on success, a negative errno value otherwise and rte_errno is set.
198 mlx5_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
200 DRV_LOG(DEBUG, "port %u setting primary MAC address",
202 return mlx5_mac_addr_add(dev, mac_addr, 0, 0);
206 * DPDK callback to set multicast addresses list.
208 * @see rte_eth_dev_set_mc_addr_list()
211 mlx5_set_mc_addr_list(struct rte_eth_dev *dev,
212 struct rte_ether_addr *mc_addr_set, uint32_t nb_mc_addr)
217 if (nb_mc_addr >= MLX5_MAX_MC_MAC_ADDRESSES) {
221 for (i = MLX5_MAX_UC_MAC_ADDRESSES; i != MLX5_MAX_MAC_ADDRESSES; ++i)
222 mlx5_internal_mac_addr_remove(dev, i);
223 i = MLX5_MAX_UC_MAC_ADDRESSES;
224 while (nb_mc_addr--) {
225 ret = mlx5_internal_mac_addr_add(dev, mc_addr_set++, i++);
229 if (!dev->data->promiscuous)
230 return mlx5_traffic_restart(dev);