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>
30 #include "mlx5_defs.h"
32 #include "mlx5_utils.h"
33 #include "mlx5_rxtx.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(priv->nl_socket_route,
78 mlx5_ifindex(dev), priv->mac_own,
79 &dev->data->mac_addrs[index], index);
80 memset(&dev->data->mac_addrs[index], 0, sizeof(struct rte_ether_addr));
84 * Adds a MAC address to the internal array.
87 * Pointer to Ethernet device structure.
89 * MAC address to register.
94 * 0 on success, a negative errno value otherwise and rte_errno is set.
97 mlx5_internal_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac,
100 struct mlx5_priv *priv = dev->data->dev_private;
101 const int vf = priv->config.vf;
104 assert(index < MLX5_MAX_MAC_ADDRESSES);
105 if (rte_is_zero_ether_addr(mac)) {
109 /* First, make sure this address isn't already configured. */
110 for (i = 0; (i != MLX5_MAX_MAC_ADDRESSES); ++i) {
111 /* Skip this index, it's going to be reconfigured. */
114 if (memcmp(&dev->data->mac_addrs[i], mac, sizeof(*mac)))
116 /* Address already configured elsewhere, return with error. */
117 rte_errno = EADDRINUSE;
121 int ret = mlx5_nl_mac_addr_add(priv->nl_socket_route,
122 mlx5_ifindex(dev), priv->mac_own,
128 dev->data->mac_addrs[index] = *mac;
133 * DPDK callback to remove a MAC address.
136 * Pointer to Ethernet device structure.
141 mlx5_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
145 if (index >= MLX5_MAX_UC_MAC_ADDRESSES)
147 mlx5_internal_mac_addr_remove(dev, index);
148 if (!dev->data->promiscuous) {
149 ret = mlx5_traffic_restart(dev);
151 DRV_LOG(ERR, "port %u cannot restart traffic: %s",
152 dev->data->port_id, strerror(rte_errno));
157 * DPDK callback to add a MAC address.
160 * Pointer to Ethernet device structure.
162 * MAC address to register.
166 * VMDq pool index to associate address with (ignored).
169 * 0 on success, a negative errno value otherwise and rte_errno is set.
172 mlx5_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac,
173 uint32_t index, uint32_t vmdq __rte_unused)
177 if (index >= MLX5_MAX_UC_MAC_ADDRESSES) {
181 ret = mlx5_internal_mac_addr_add(dev, mac, index);
184 if (!dev->data->promiscuous)
185 return mlx5_traffic_restart(dev);
190 * DPDK callback to set primary MAC address.
193 * Pointer to Ethernet device structure.
195 * MAC address to register.
198 * 0 on success, a negative errno value otherwise and rte_errno is set.
201 mlx5_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
204 struct mlx5_priv *priv = dev->data->dev_private;
206 /* Configuring the VF instead of its representor. */
207 if (priv->representor) {
208 DRV_LOG(DEBUG, "VF represented by port %u setting primary MAC address",
210 RTE_ETH_FOREACH_DEV_SIBLING(port_id, dev->data->port_id) {
211 priv = rte_eth_devices[port_id].data->dev_private;
212 if (priv->master == 1) {
213 priv = dev->data->dev_private;
214 return mlx5_nl_vf_mac_addr_modify
215 (priv->nl_socket_route,
216 mlx5_ifindex(&rte_eth_devices[port_id]),
217 mac_addr, priv->representor_id);
220 rte_errno = -ENOTSUP;
224 DRV_LOG(DEBUG, "port %u setting primary MAC address",
226 return mlx5_mac_addr_add(dev, mac_addr, 0, 0);
230 * DPDK callback to set multicast addresses list.
232 * @see rte_eth_dev_set_mc_addr_list()
235 mlx5_set_mc_addr_list(struct rte_eth_dev *dev,
236 struct rte_ether_addr *mc_addr_set, uint32_t nb_mc_addr)
241 if (nb_mc_addr >= MLX5_MAX_MC_MAC_ADDRESSES) {
245 for (i = MLX5_MAX_UC_MAC_ADDRESSES; i != MLX5_MAX_MAC_ADDRESSES; ++i)
246 mlx5_internal_mac_addr_remove(dev, i);
247 i = MLX5_MAX_UC_MAC_ADDRESSES;
248 while (nb_mc_addr--) {
249 ret = mlx5_internal_mac_addr_add(dev, mc_addr_set++, i++);
253 if (!dev->data->promiscuous)
254 return mlx5_traffic_restart(dev);