4 * Copyright 2015 6WIND S.A.
5 * Copyright 2015 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <netinet/in.h>
42 #include <sys/ioctl.h>
43 #include <arpa/inet.h>
46 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
48 #pragma GCC diagnostic ignored "-pedantic"
50 #include <infiniband/verbs.h>
52 #pragma GCC diagnostic error "-pedantic"
55 /* DPDK headers don't like -pedantic. */
57 #pragma GCC diagnostic ignored "-pedantic"
59 #include <rte_ether.h>
60 #include <rte_ethdev.h>
61 #include <rte_common.h>
63 #pragma GCC diagnostic error "-pedantic"
67 #include "mlx5_utils.h"
68 #include "mlx5_rxtx.h"
69 #include "mlx5_defs.h"
72 * Get MAC address by querying netdevice.
75 * struct priv for the requested device.
77 * MAC address output buffer.
80 * 0 on success, -1 on failure and errno is set.
83 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
87 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
89 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
94 * Delete MAC flow steering rule.
97 * Pointer to RX queue structure.
102 rxq_del_mac_flow(struct rxq *rxq, unsigned int mac_index)
105 const uint8_t (*mac)[ETHER_ADDR_LEN] =
106 (const uint8_t (*)[ETHER_ADDR_LEN])
107 rxq->priv->mac[mac_index].addr_bytes;
110 assert(mac_index < RTE_DIM(rxq->mac_flow));
111 if (rxq->mac_flow[mac_index] == NULL)
113 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u",
115 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
117 claim_zero(ibv_destroy_flow(rxq->mac_flow[mac_index]));
118 rxq->mac_flow[mac_index] = NULL;
122 * Unregister a MAC address from a RX queue.
125 * Pointer to RX queue structure.
130 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
132 assert(mac_index < RTE_DIM(rxq->mac_flow));
133 rxq_del_mac_flow(rxq, mac_index);
137 * Unregister all MAC addresses from a RX queue.
140 * Pointer to RX queue structure.
143 rxq_mac_addrs_del(struct rxq *rxq)
147 for (i = 0; (i != RTE_DIM(rxq->mac_flow)); ++i)
148 rxq_mac_addr_del(rxq, i);
152 * Unregister a MAC address.
154 * In RSS mode, the MAC address is unregistered from the parent queue,
155 * otherwise it is unregistered from each queue directly.
158 * Pointer to private structure.
163 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
167 assert(mac_index < RTE_DIM(priv->mac));
168 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
171 rxq_mac_addr_del(&priv->rxq_parent, mac_index);
174 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
175 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
177 BITFIELD_RESET(priv->mac_configured, mac_index);
181 * DPDK callback to remove a MAC address.
184 * Pointer to Ethernet device structure.
189 mlx5_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
191 struct priv *priv = dev->data->dev_private;
194 DEBUG("%p: removing MAC address from index %" PRIu32,
196 /* Last array entry is reserved for broadcast. */
197 if (index >= (RTE_DIM(priv->mac) - 1))
199 priv_mac_addr_del(priv, index);
205 * Add MAC flow steering rule.
208 * Pointer to RX queue structure.
210 * MAC address index to register.
213 * 0 on success, errno value on failure.
216 rxq_add_mac_flow(struct rxq *rxq, unsigned int mac_index)
218 struct ibv_flow *flow;
219 struct priv *priv = rxq->priv;
220 const uint8_t (*mac)[ETHER_ADDR_LEN] =
221 (const uint8_t (*)[ETHER_ADDR_LEN])
222 priv->mac[mac_index].addr_bytes;
223 struct __attribute__((packed)) {
224 struct ibv_flow_attr attr;
225 struct ibv_flow_spec_eth spec;
227 struct ibv_flow_attr *attr = &data.attr;
228 struct ibv_flow_spec_eth *spec = &data.spec;
230 assert(mac_index < RTE_DIM(rxq->mac_flow));
231 if (rxq->mac_flow[mac_index] != NULL)
234 * No padding must be inserted by the compiler between attr and spec.
235 * This layout is expected by libibverbs.
237 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
238 *attr = (struct ibv_flow_attr){
239 .type = IBV_FLOW_ATTR_NORMAL,
244 *spec = (struct ibv_flow_spec_eth){
245 .type = IBV_FLOW_SPEC_ETH,
246 .size = sizeof(*spec),
249 (*mac)[0], (*mac)[1], (*mac)[2],
250 (*mac)[3], (*mac)[4], (*mac)[5]
254 .dst_mac = "\xff\xff\xff\xff\xff\xff",
257 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u",
259 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
261 /* Create related flow. */
263 flow = ibv_create_flow(rxq->qp, attr);
265 /* It's not clear whether errno is always set in this case. */
266 ERROR("%p: flow configuration failed, errno=%d: %s",
268 (errno ? strerror(errno) : "Unknown error"));
273 rxq->mac_flow[mac_index] = flow;
278 * Register a MAC address in a RX queue.
281 * Pointer to RX queue structure.
283 * MAC address index to register.
286 * 0 on success, errno value on failure.
289 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
293 assert(mac_index < RTE_DIM(rxq->mac_flow));
294 ret = rxq_add_mac_flow(rxq, mac_index);
301 * Register all MAC addresses in a RX queue.
304 * Pointer to RX queue structure.
307 * 0 on success, errno value on failure.
310 rxq_mac_addrs_add(struct rxq *rxq)
312 struct priv *priv = rxq->priv;
316 assert(RTE_DIM(priv->mac) == RTE_DIM(rxq->mac_flow));
317 for (i = 0; (i != RTE_DIM(priv->mac)); ++i) {
318 if (!BITFIELD_ISSET(priv->mac_configured, i))
320 ret = rxq_mac_addr_add(rxq, i);
323 /* Failure, rollback. */
325 rxq_mac_addr_del(rxq, --i);
333 * Register a MAC address.
335 * In RSS mode, the MAC address is registered in the parent queue,
336 * otherwise it is registered in each queue directly.
339 * Pointer to private structure.
341 * MAC address index to use.
343 * MAC address to register.
346 * 0 on success, errno value on failure.
349 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
350 const uint8_t (*mac)[ETHER_ADDR_LEN])
355 assert(mac_index < RTE_DIM(priv->mac));
356 /* First, make sure this address isn't already configured. */
357 for (i = 0; (i != RTE_DIM(priv->mac)); ++i) {
358 /* Skip this index, it's going to be reconfigured. */
361 if (!BITFIELD_ISSET(priv->mac_configured, i))
363 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
365 /* Address already configured elsewhere, return with error. */
368 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
369 priv_mac_addr_del(priv, mac_index);
370 priv->mac[mac_index] = (struct ether_addr){
372 (*mac)[0], (*mac)[1], (*mac)[2],
373 (*mac)[3], (*mac)[4], (*mac)[5]
376 /* If device isn't started, this is all we need to do. */
380 ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
385 for (i = 0; (i != priv->rxqs_n); ++i) {
386 if ((*priv->rxqs)[i] == NULL)
388 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
391 /* Failure, rollback. */
393 if ((*priv->rxqs)[--i] != NULL)
394 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
398 BITFIELD_SET(priv->mac_configured, mac_index);
403 * DPDK callback to add a MAC address.
406 * Pointer to Ethernet device structure.
408 * MAC address to register.
412 * VMDq pool index to associate address with (ignored).
415 mlx5_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
416 uint32_t index, uint32_t vmdq)
418 struct priv *priv = dev->data->dev_private;
422 DEBUG("%p: adding MAC address at index %" PRIu32,
424 /* Last array entry is reserved for broadcast. */
425 if (index >= (RTE_DIM(priv->mac) - 1))
427 priv_mac_addr_add(priv, index,
428 (const uint8_t (*)[ETHER_ADDR_LEN])
429 mac_addr->addr_bytes);