1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
11 #include <netinet/in.h>
12 #include <sys/ioctl.h>
13 #include <arpa/inet.h>
16 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
18 #pragma GCC diagnostic ignored "-Wpedantic"
20 #include <infiniband/verbs.h>
22 #pragma GCC diagnostic error "-Wpedantic"
25 #include <rte_ether.h>
26 #include <rte_ethdev_driver.h>
27 #include <rte_common.h>
29 #include "mlx5_defs.h"
31 #include "mlx5_utils.h"
32 #include "mlx5_rxtx.h"
35 * Get MAC address by querying netdevice.
38 * Pointer to Ethernet device.
40 * MAC address output buffer.
43 * 0 on success, a negative errno value otherwise and rte_errno is set.
46 mlx5_get_mac(struct rte_eth_dev *dev, uint8_t (*mac)[RTE_ETHER_ADDR_LEN])
51 ret = mlx5_ifreq(dev, SIOCGIFHWADDR, &request);
54 memcpy(mac, request.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
59 * Remove a MAC address from the internal array.
62 * Pointer to Ethernet device structure.
67 mlx5_internal_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
69 struct mlx5_priv *priv = dev->data->dev_private;
70 const int vf = priv->config.vf;
72 MLX5_ASSERT(index < MLX5_MAX_MAC_ADDRESSES);
73 if (rte_is_zero_ether_addr(&dev->data->mac_addrs[index]))
76 mlx5_nl_mac_addr_remove(priv->nl_socket_route,
77 mlx5_ifindex(dev), priv->mac_own,
78 &dev->data->mac_addrs[index], 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 MLX5_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(priv->nl_socket_route,
121 mlx5_ifindex(dev), priv->mac_own,
127 dev->data->mac_addrs[index] = *mac;
132 * DPDK callback to remove a MAC address.
135 * Pointer to Ethernet device structure.
140 mlx5_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
144 if (index >= MLX5_MAX_UC_MAC_ADDRESSES)
146 mlx5_internal_mac_addr_remove(dev, index);
147 if (!dev->data->promiscuous) {
148 ret = mlx5_traffic_restart(dev);
150 DRV_LOG(ERR, "port %u cannot restart traffic: %s",
151 dev->data->port_id, strerror(rte_errno));
156 * DPDK callback to add a MAC address.
159 * Pointer to Ethernet device structure.
161 * MAC address to register.
165 * VMDq pool index to associate address with (ignored).
168 * 0 on success, a negative errno value otherwise and rte_errno is set.
171 mlx5_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac,
172 uint32_t index, uint32_t vmdq __rte_unused)
176 if (index >= MLX5_MAX_UC_MAC_ADDRESSES) {
180 ret = mlx5_internal_mac_addr_add(dev, mac, index);
183 if (!dev->data->promiscuous)
184 return mlx5_traffic_restart(dev);
189 * DPDK callback to set primary MAC address.
192 * Pointer to Ethernet device structure.
194 * MAC address to register.
197 * 0 on success, a negative errno value otherwise and rte_errno is set.
200 mlx5_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
203 struct mlx5_priv *priv = dev->data->dev_private;
205 /* Configuring the VF instead of its representor. */
206 if (priv->representor) {
207 DRV_LOG(DEBUG, "VF represented by port %u setting primary MAC address",
209 RTE_ETH_FOREACH_DEV_SIBLING(port_id, dev->data->port_id) {
210 priv = rte_eth_devices[port_id].data->dev_private;
211 if (priv->master == 1) {
212 priv = dev->data->dev_private;
213 return mlx5_nl_vf_mac_addr_modify
214 (priv->nl_socket_route,
215 mlx5_ifindex(&rte_eth_devices[port_id]),
216 mac_addr, priv->representor_id);
219 rte_errno = -ENOTSUP;
223 DRV_LOG(DEBUG, "port %u setting primary MAC address",
225 return mlx5_mac_addr_add(dev, mac_addr, 0, 0);
229 * DPDK callback to set multicast addresses list.
231 * @see rte_eth_dev_set_mc_addr_list()
234 mlx5_set_mc_addr_list(struct rte_eth_dev *dev,
235 struct rte_ether_addr *mc_addr_set, uint32_t nb_mc_addr)
240 if (nb_mc_addr >= MLX5_MAX_MC_MAC_ADDRESSES) {
244 for (i = MLX5_MAX_UC_MAC_ADDRESSES; i != MLX5_MAX_MAC_ADDRESSES; ++i)
245 mlx5_internal_mac_addr_remove(dev, i);
246 i = MLX5_MAX_UC_MAC_ADDRESSES;
247 while (nb_mc_addr--) {
248 ret = mlx5_internal_mac_addr_add(dev, mc_addr_set++, i++);
252 if (!dev->data->promiscuous)
253 return mlx5_traffic_restart(dev);