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.
104 rxq_del_mac_flow(struct rxq *rxq, unsigned int mac_index,
105 unsigned int vlan_index)
108 const uint8_t (*mac)[ETHER_ADDR_LEN] =
109 (const uint8_t (*)[ETHER_ADDR_LEN])
110 rxq->priv->mac[mac_index].addr_bytes;
113 assert(mac_index < RTE_DIM(rxq->mac_flow));
114 assert(vlan_index < RTE_DIM(rxq->mac_flow[mac_index]));
115 if (rxq->mac_flow[mac_index][vlan_index] == NULL)
117 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
120 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
123 claim_zero(ibv_destroy_flow(rxq->mac_flow[mac_index][vlan_index]));
124 rxq->mac_flow[mac_index][vlan_index] = NULL;
128 * Unregister a MAC address from a RX queue.
131 * Pointer to RX queue structure.
136 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
140 assert(mac_index < RTE_DIM(rxq->mac_flow));
141 for (i = 0; (i != RTE_DIM(rxq->mac_flow[mac_index])); ++i)
142 rxq_del_mac_flow(rxq, mac_index, i);
146 * Unregister all MAC addresses from a RX queue.
149 * Pointer to RX queue structure.
152 rxq_mac_addrs_del(struct rxq *rxq)
156 for (i = 0; (i != RTE_DIM(rxq->mac_flow)); ++i)
157 rxq_mac_addr_del(rxq, i);
161 * Unregister a MAC address.
163 * In RSS mode, the MAC address is unregistered from the parent queue,
164 * otherwise it is unregistered from each queue directly.
167 * Pointer to private structure.
172 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
176 assert(mac_index < RTE_DIM(priv->mac));
177 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
180 rxq_mac_addr_del(&priv->rxq_parent, mac_index);
183 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
184 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
186 BITFIELD_RESET(priv->mac_configured, mac_index);
190 * DPDK callback to remove a MAC address.
193 * Pointer to Ethernet device structure.
198 mlx5_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
200 struct priv *priv = dev->data->dev_private;
203 DEBUG("%p: removing MAC address from index %" PRIu32,
205 /* Last array entry is reserved for broadcast. */
206 if (index >= (RTE_DIM(priv->mac) - 1))
208 priv_mac_addr_del(priv, index);
214 * Add MAC flow steering rule.
217 * Pointer to RX queue structure.
219 * MAC address index to register.
224 * 0 on success, errno value on failure.
227 rxq_add_mac_flow(struct rxq *rxq, unsigned int mac_index,
228 unsigned int vlan_index)
230 struct ibv_flow *flow;
231 struct priv *priv = rxq->priv;
232 const uint8_t (*mac)[ETHER_ADDR_LEN] =
233 (const uint8_t (*)[ETHER_ADDR_LEN])
234 priv->mac[mac_index].addr_bytes;
235 struct __attribute__((packed)) {
236 struct ibv_flow_attr attr;
237 struct ibv_flow_spec_eth spec;
239 struct ibv_flow_attr *attr = &data.attr;
240 struct ibv_flow_spec_eth *spec = &data.spec;
241 unsigned int vlan_enabled = !!priv->vlan_filter_n;
242 unsigned int vlan_id = priv->vlan_filter[vlan_index];
244 assert(mac_index < RTE_DIM(rxq->mac_flow));
245 assert(vlan_index < RTE_DIM(rxq->mac_flow[mac_index]));
246 if (rxq->mac_flow[mac_index][vlan_index] != NULL)
249 * No padding must be inserted by the compiler between attr and spec.
250 * This layout is expected by libibverbs.
252 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
253 *attr = (struct ibv_flow_attr){
254 .type = IBV_FLOW_ATTR_NORMAL,
259 *spec = (struct ibv_flow_spec_eth){
260 .type = IBV_FLOW_SPEC_ETH,
261 .size = sizeof(*spec),
264 (*mac)[0], (*mac)[1], (*mac)[2],
265 (*mac)[3], (*mac)[4], (*mac)[5]
267 .vlan_tag = (vlan_enabled ? htons(vlan_id) : 0),
270 .dst_mac = "\xff\xff\xff\xff\xff\xff",
271 .vlan_tag = (vlan_enabled ? htons(0xfff) : 0),
274 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
275 " VLAN index %u filtering %s, ID %u",
277 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
280 (vlan_enabled ? "enabled" : "disabled"),
282 /* Create related flow. */
284 flow = ibv_create_flow(rxq->qp, attr);
286 /* It's not clear whether errno is always set in this case. */
287 ERROR("%p: flow configuration failed, errno=%d: %s",
289 (errno ? strerror(errno) : "Unknown error"));
294 rxq->mac_flow[mac_index][vlan_index] = flow;
299 * Register a MAC address in a RX queue.
302 * Pointer to RX queue structure.
304 * MAC address index to register.
307 * 0 on success, errno value on failure.
310 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
312 struct priv *priv = rxq->priv;
316 assert(mac_index < RTE_DIM(rxq->mac_flow));
317 assert(RTE_DIM(rxq->mac_flow[mac_index]) ==
318 RTE_DIM(priv->vlan_filter));
319 /* Add a MAC address for each VLAN filter, or at least once. */
321 ret = rxq_add_mac_flow(rxq, mac_index, i);
323 /* Failure, rollback. */
325 rxq_del_mac_flow(rxq, mac_index, --i);
328 } while (++i < priv->vlan_filter_n);
333 * Register all MAC addresses in a RX queue.
336 * Pointer to RX queue structure.
339 * 0 on success, errno value on failure.
342 rxq_mac_addrs_add(struct rxq *rxq)
344 struct priv *priv = rxq->priv;
348 assert(RTE_DIM(priv->mac) == RTE_DIM(rxq->mac_flow));
349 for (i = 0; (i != RTE_DIM(priv->mac)); ++i) {
350 if (!BITFIELD_ISSET(priv->mac_configured, i))
352 ret = rxq_mac_addr_add(rxq, i);
355 /* Failure, rollback. */
357 rxq_mac_addr_del(rxq, --i);
365 * Register a MAC address.
367 * In RSS mode, the MAC address is registered in the parent queue,
368 * otherwise it is registered in each queue directly.
371 * Pointer to private structure.
373 * MAC address index to use.
375 * MAC address to register.
378 * 0 on success, errno value on failure.
381 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
382 const uint8_t (*mac)[ETHER_ADDR_LEN])
387 assert(mac_index < RTE_DIM(priv->mac));
388 /* First, make sure this address isn't already configured. */
389 for (i = 0; (i != RTE_DIM(priv->mac)); ++i) {
390 /* Skip this index, it's going to be reconfigured. */
393 if (!BITFIELD_ISSET(priv->mac_configured, i))
395 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
397 /* Address already configured elsewhere, return with error. */
400 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
401 priv_mac_addr_del(priv, mac_index);
402 priv->mac[mac_index] = (struct ether_addr){
404 (*mac)[0], (*mac)[1], (*mac)[2],
405 (*mac)[3], (*mac)[4], (*mac)[5]
408 /* If device isn't started, this is all we need to do. */
412 ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
417 for (i = 0; (i != priv->rxqs_n); ++i) {
418 if ((*priv->rxqs)[i] == NULL)
420 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
423 /* Failure, rollback. */
425 if ((*priv->rxqs)[--i] != NULL)
426 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
430 BITFIELD_SET(priv->mac_configured, mac_index);
435 * DPDK callback to add a MAC address.
438 * Pointer to Ethernet device structure.
440 * MAC address to register.
444 * VMDq pool index to associate address with (ignored).
447 mlx5_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
448 uint32_t index, uint32_t vmdq)
450 struct priv *priv = dev->data->dev_private;
454 DEBUG("%p: adding MAC address at index %" PRIu32,
456 /* Last array entry is reserved for broadcast. */
457 if (index >= (RTE_DIM(priv->mac) - 1))
459 priv_mac_addr_add(priv, index,
460 (const uint8_t (*)[ETHER_ADDR_LEN])
461 mac_addr->addr_bytes);