net/mlx5: reduce Netlink commands dependencies
[dpdk.git] / drivers / net / mlx5 / mlx5_mac.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2015 Mellanox Technologies, Ltd
4  */
5
6 #include <stddef.h>
7 #include <assert.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <inttypes.h>
11 #include <errno.h>
12 #include <netinet/in.h>
13 #include <sys/ioctl.h>
14 #include <arpa/inet.h>
15
16 /* Verbs header. */
17 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
18 #ifdef PEDANTIC
19 #pragma GCC diagnostic ignored "-Wpedantic"
20 #endif
21 #include <infiniband/verbs.h>
22 #ifdef PEDANTIC
23 #pragma GCC diagnostic error "-Wpedantic"
24 #endif
25
26 #include <rte_ether.h>
27 #include <rte_ethdev_driver.h>
28 #include <rte_common.h>
29
30 #include "mlx5_defs.h"
31 #include "mlx5.h"
32 #include "mlx5_utils.h"
33 #include "mlx5_rxtx.h"
34
35 /**
36  * Get MAC address by querying netdevice.
37  *
38  * @param[in] dev
39  *   Pointer to Ethernet device.
40  * @param[out] mac
41  *   MAC address output buffer.
42  *
43  * @return
44  *   0 on success, a negative errno value otherwise and rte_errno is set.
45  */
46 int
47 mlx5_get_mac(struct rte_eth_dev *dev, uint8_t (*mac)[RTE_ETHER_ADDR_LEN])
48 {
49         struct ifreq request;
50         int ret;
51
52         ret = mlx5_ifreq(dev, SIOCGIFHWADDR, &request);
53         if (ret)
54                 return ret;
55         memcpy(mac, request.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
56         return 0;
57 }
58
59 /**
60  * Remove a MAC address from the internal array.
61  *
62  * @param dev
63  *   Pointer to Ethernet device structure.
64  * @param index
65  *   MAC address index.
66  */
67 static void
68 mlx5_internal_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
69 {
70         struct mlx5_priv *priv = dev->data->dev_private;
71         const int vf = priv->config.vf;
72
73         assert(index < MLX5_MAX_MAC_ADDRESSES);
74         if (rte_is_zero_ether_addr(&dev->data->mac_addrs[index]))
75                 return;
76         if (vf)
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));
81 }
82
83 /**
84  * Adds a MAC address to the internal array.
85  *
86  * @param dev
87  *   Pointer to Ethernet device structure.
88  * @param mac_addr
89  *   MAC address to register.
90  * @param index
91  *   MAC address index.
92  *
93  * @return
94  *   0 on success, a negative errno value otherwise and rte_errno is set.
95  */
96 static int
97 mlx5_internal_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac,
98                            uint32_t index)
99 {
100         struct mlx5_priv *priv = dev->data->dev_private;
101         const int vf = priv->config.vf;
102         unsigned int i;
103
104         assert(index < MLX5_MAX_MAC_ADDRESSES);
105         if (rte_is_zero_ether_addr(mac)) {
106                 rte_errno = EINVAL;
107                 return -rte_errno;
108         }
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. */
112                 if (i == index)
113                         continue;
114                 if (memcmp(&dev->data->mac_addrs[i], mac, sizeof(*mac)))
115                         continue;
116                 /* Address already configured elsewhere, return with error. */
117                 rte_errno = EADDRINUSE;
118                 return -rte_errno;
119         }
120         if (vf) {
121                 int ret = mlx5_nl_mac_addr_add(priv->nl_socket_route,
122                                                mlx5_ifindex(dev), priv->mac_own,
123                                                mac, index);
124
125                 if (ret)
126                         return ret;
127         }
128         dev->data->mac_addrs[index] = *mac;
129         return 0;
130 }
131
132 /**
133  * DPDK callback to remove a MAC address.
134  *
135  * @param dev
136  *   Pointer to Ethernet device structure.
137  * @param index
138  *   MAC address index.
139  */
140 void
141 mlx5_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
142 {
143         int ret;
144
145         if (index >= MLX5_MAX_UC_MAC_ADDRESSES)
146                 return;
147         mlx5_internal_mac_addr_remove(dev, index);
148         if (!dev->data->promiscuous) {
149                 ret = mlx5_traffic_restart(dev);
150                 if (ret)
151                         DRV_LOG(ERR, "port %u cannot restart traffic: %s",
152                                 dev->data->port_id, strerror(rte_errno));
153         }
154 }
155
156 /**
157  * DPDK callback to add a MAC address.
158  *
159  * @param dev
160  *   Pointer to Ethernet device structure.
161  * @param mac_addr
162  *   MAC address to register.
163  * @param index
164  *   MAC address index.
165  * @param vmdq
166  *   VMDq pool index to associate address with (ignored).
167  *
168  * @return
169  *   0 on success, a negative errno value otherwise and rte_errno is set.
170  */
171 int
172 mlx5_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac,
173                   uint32_t index, uint32_t vmdq __rte_unused)
174 {
175         int ret;
176
177         if (index >= MLX5_MAX_UC_MAC_ADDRESSES) {
178                 rte_errno = EINVAL;
179                 return -rte_errno;
180         }
181         ret = mlx5_internal_mac_addr_add(dev, mac, index);
182         if (ret < 0)
183                 return ret;
184         if (!dev->data->promiscuous)
185                 return mlx5_traffic_restart(dev);
186         return 0;
187 }
188
189 /**
190  * DPDK callback to set primary MAC address.
191  *
192  * @param dev
193  *   Pointer to Ethernet device structure.
194  * @param mac_addr
195  *   MAC address to register.
196  *
197  * @return
198  *   0 on success, a negative errno value otherwise and rte_errno is set.
199  */
200 int
201 mlx5_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
202 {
203         uint16_t port_id;
204         struct mlx5_priv *priv = dev->data->dev_private;
205
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",
209                         dev->data->port_id);
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);
218                         }
219                 }
220                 rte_errno = -ENOTSUP;
221                 return rte_errno;
222         }
223
224         DRV_LOG(DEBUG, "port %u setting primary MAC address",
225                 dev->data->port_id);
226         return mlx5_mac_addr_add(dev, mac_addr, 0, 0);
227 }
228
229 /**
230  * DPDK callback to set multicast addresses list.
231  *
232  * @see rte_eth_dev_set_mc_addr_list()
233  */
234 int
235 mlx5_set_mc_addr_list(struct rte_eth_dev *dev,
236                       struct rte_ether_addr *mc_addr_set, uint32_t nb_mc_addr)
237 {
238         uint32_t i;
239         int ret;
240
241         if (nb_mc_addr >= MLX5_MAX_MC_MAC_ADDRESSES) {
242                 rte_errno = ENOSPC;
243                 return -rte_errno;
244         }
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++);
250                 if (ret)
251                         return ret;
252         }
253         if (!dev->data->promiscuous)
254                 return mlx5_traffic_restart(dev);
255         return 0;
256 }