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>
41 #include <sys/ioctl.h>
42 #include <arpa/inet.h>
45 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
47 #pragma GCC diagnostic ignored "-Wpedantic"
49 #include <infiniband/verbs.h>
51 #pragma GCC diagnostic error "-Wpedantic"
54 #include <rte_ether.h>
55 #include <rte_ethdev.h>
56 #include <rte_common.h>
59 #include "mlx5_utils.h"
60 #include "mlx5_rxtx.h"
61 #include "mlx5_defs.h"
64 * Get MAC address by querying netdevice.
67 * struct priv for the requested device.
69 * MAC address output buffer.
72 * 0 on success, -1 on failure and errno is set.
75 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
79 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
81 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
86 * Delete MAC flow steering rule.
89 * Pointer to hash RX queue structure.
96 hash_rxq_del_mac_flow(struct hash_rxq *hash_rxq, unsigned int mac_index,
97 unsigned int vlan_index)
100 const uint8_t (*mac)[ETHER_ADDR_LEN] =
101 (const uint8_t (*)[ETHER_ADDR_LEN])
102 hash_rxq->priv->mac[mac_index].addr_bytes;
105 assert(mac_index < RTE_DIM(hash_rxq->mac_flow));
106 assert(vlan_index < RTE_DIM(hash_rxq->mac_flow[mac_index]));
107 if (hash_rxq->mac_flow[mac_index][vlan_index] == NULL)
109 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
112 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
115 claim_zero(ibv_exp_destroy_flow(hash_rxq->mac_flow
116 [mac_index][vlan_index]));
117 hash_rxq->mac_flow[mac_index][vlan_index] = NULL;
121 * Unregister a MAC address from a hash RX queue.
124 * Pointer to hash RX queue structure.
129 hash_rxq_mac_addr_del(struct hash_rxq *hash_rxq, unsigned int mac_index)
133 assert(mac_index < RTE_DIM(hash_rxq->mac_flow));
134 for (i = 0; (i != RTE_DIM(hash_rxq->mac_flow[mac_index])); ++i)
135 hash_rxq_del_mac_flow(hash_rxq, mac_index, i);
139 * Unregister all MAC addresses from a hash RX queue.
142 * Pointer to hash RX queue structure.
145 hash_rxq_mac_addrs_del(struct hash_rxq *hash_rxq)
149 for (i = 0; (i != RTE_DIM(hash_rxq->mac_flow)); ++i)
150 hash_rxq_mac_addr_del(hash_rxq, i);
154 * Unregister a MAC address.
156 * This is done for each hash RX queue.
159 * Pointer to private structure.
164 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
168 assert(mac_index < RTE_DIM(priv->mac));
169 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
171 for (i = 0; (i != priv->hash_rxqs_n); ++i)
172 hash_rxq_mac_addr_del(&(*priv->hash_rxqs)[i], mac_index);
173 BITFIELD_RESET(priv->mac_configured, mac_index);
177 * Unregister all MAC addresses from all hash RX queues.
180 * Pointer to private structure.
183 priv_mac_addrs_disable(struct priv *priv)
187 for (i = 0; (i != priv->hash_rxqs_n); ++i)
188 hash_rxq_mac_addrs_del(&(*priv->hash_rxqs)[i]);
192 * DPDK callback to remove a MAC address.
195 * Pointer to Ethernet device structure.
200 mlx5_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
202 struct priv *priv = dev->data->dev_private;
204 if (mlx5_is_secondary())
208 DEBUG("%p: removing MAC address from index %" PRIu32,
210 if (index >= RTE_DIM(priv->mac))
212 priv_mac_addr_del(priv, index);
218 * Add MAC flow steering rule.
221 * Pointer to hash RX queue structure.
223 * MAC address index to register.
228 * 0 on success, errno value on failure.
231 hash_rxq_add_mac_flow(struct hash_rxq *hash_rxq, unsigned int mac_index,
232 unsigned int vlan_index)
234 struct ibv_exp_flow *flow;
235 struct priv *priv = hash_rxq->priv;
236 const uint8_t (*mac)[ETHER_ADDR_LEN] =
237 (const uint8_t (*)[ETHER_ADDR_LEN])
238 priv->mac[mac_index].addr_bytes;
239 FLOW_ATTR_SPEC_ETH(data, priv_flow_attr(priv, NULL, 0, hash_rxq->type));
240 struct ibv_exp_flow_attr *attr = &data->attr;
241 struct ibv_exp_flow_spec_eth *spec = &data->spec;
242 unsigned int vlan_enabled = !!priv->vlan_filter_n;
243 unsigned int vlan_id = priv->vlan_filter[vlan_index];
245 assert(mac_index < RTE_DIM(hash_rxq->mac_flow));
246 assert(vlan_index < RTE_DIM(hash_rxq->mac_flow[mac_index]));
247 if (hash_rxq->mac_flow[mac_index][vlan_index] != NULL)
250 * No padding must be inserted by the compiler between attr and spec.
251 * This layout is expected by libibverbs.
253 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
254 priv_flow_attr(priv, attr, sizeof(data), hash_rxq->type);
255 /* The first specification must be Ethernet. */
256 assert(spec->type == IBV_EXP_FLOW_SPEC_ETH);
257 assert(spec->size == sizeof(*spec));
258 *spec = (struct ibv_exp_flow_spec_eth){
259 .type = IBV_EXP_FLOW_SPEC_ETH,
260 .size = sizeof(*spec),
263 (*mac)[0], (*mac)[1], (*mac)[2],
264 (*mac)[3], (*mac)[4], (*mac)[5]
266 .vlan_tag = (vlan_enabled ? htons(vlan_id) : 0),
269 .dst_mac = "\xff\xff\xff\xff\xff\xff",
270 .vlan_tag = (vlan_enabled ? htons(0xfff) : 0),
273 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
274 " VLAN index %u filtering %s, ID %u",
276 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
279 (vlan_enabled ? "enabled" : "disabled"),
281 /* Create related flow. */
283 flow = ibv_exp_create_flow(hash_rxq->qp, attr);
285 /* It's not clear whether errno is always set in this case. */
286 ERROR("%p: flow configuration failed, errno=%d: %s",
287 (void *)hash_rxq, errno,
288 (errno ? strerror(errno) : "Unknown error"));
293 hash_rxq->mac_flow[mac_index][vlan_index] = flow;
298 * Register a MAC address in a hash RX queue.
301 * Pointer to hash RX queue structure.
303 * MAC address index to register.
306 * 0 on success, errno value on failure.
309 hash_rxq_mac_addr_add(struct hash_rxq *hash_rxq, unsigned int mac_index)
311 struct priv *priv = hash_rxq->priv;
315 assert(mac_index < RTE_DIM(hash_rxq->mac_flow));
316 assert(RTE_DIM(hash_rxq->mac_flow[mac_index]) ==
317 RTE_DIM(priv->vlan_filter));
318 /* Add a MAC address for each VLAN filter, or at least once. */
320 ret = hash_rxq_add_mac_flow(hash_rxq, mac_index, i);
322 /* Failure, rollback. */
324 hash_rxq_del_mac_flow(hash_rxq, mac_index,
328 } while (++i < priv->vlan_filter_n);
333 * Register all MAC addresses in a hash RX queue.
336 * Pointer to hash RX queue structure.
339 * 0 on success, errno value on failure.
342 hash_rxq_mac_addrs_add(struct hash_rxq *hash_rxq)
344 struct priv *priv = hash_rxq->priv;
348 assert(RTE_DIM(priv->mac) == RTE_DIM(hash_rxq->mac_flow));
349 for (i = 0; (i != RTE_DIM(priv->mac)); ++i) {
350 if (!BITFIELD_ISSET(priv->mac_configured, i))
352 ret = hash_rxq_mac_addr_add(hash_rxq, i);
355 /* Failure, rollback. */
357 hash_rxq_mac_addr_del(hash_rxq, --i);
365 * Register a MAC address.
367 * This is done for each hash RX queue.
370 * Pointer to private structure.
372 * MAC address index to use.
374 * MAC address to register.
377 * 0 on success, errno value on failure.
380 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
381 const uint8_t (*mac)[ETHER_ADDR_LEN])
386 assert(mac_index < RTE_DIM(priv->mac));
387 /* First, make sure this address isn't already configured. */
388 for (i = 0; (i != RTE_DIM(priv->mac)); ++i) {
389 /* Skip this index, it's going to be reconfigured. */
392 if (!BITFIELD_ISSET(priv->mac_configured, i))
394 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
396 /* Address already configured elsewhere, return with error. */
399 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
400 priv_mac_addr_del(priv, mac_index);
401 priv->mac[mac_index] = (struct ether_addr){
403 (*mac)[0], (*mac)[1], (*mac)[2],
404 (*mac)[3], (*mac)[4], (*mac)[5]
407 if (!priv_allow_flow_type(priv, HASH_RXQ_FLOW_TYPE_MAC))
409 for (i = 0; (i != priv->hash_rxqs_n); ++i) {
410 ret = hash_rxq_mac_addr_add(&(*priv->hash_rxqs)[i], mac_index);
413 /* Failure, rollback. */
415 hash_rxq_mac_addr_del(&(*priv->hash_rxqs)[--i],
420 BITFIELD_SET(priv->mac_configured, mac_index);
425 * Register all MAC addresses in all hash RX queues.
428 * Pointer to private structure.
431 * 0 on success, errno value on failure.
434 priv_mac_addrs_enable(struct priv *priv)
441 if (!priv_allow_flow_type(priv, HASH_RXQ_FLOW_TYPE_MAC))
443 for (i = 0; (i != priv->hash_rxqs_n); ++i) {
444 ret = hash_rxq_mac_addrs_add(&(*priv->hash_rxqs)[i]);
447 /* Failure, rollback. */
449 hash_rxq_mac_addrs_del(&(*priv->hash_rxqs)[--i]);
457 * DPDK callback to add a MAC address.
460 * Pointer to Ethernet device structure.
462 * MAC address to register.
466 * VMDq pool index to associate address with (ignored).
469 mlx5_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
470 uint32_t index, uint32_t vmdq)
472 struct priv *priv = dev->data->dev_private;
475 if (mlx5_is_secondary())
480 DEBUG("%p: adding MAC address at index %" PRIu32,
482 if (index >= RTE_DIM(priv->mac)) {
486 re = priv_mac_addr_add(priv, index,
487 (const uint8_t (*)[ETHER_ADDR_LEN])
488 mac_addr->addr_bytes);
495 * DPDK callback to set primary MAC address.
498 * Pointer to Ethernet device structure.
500 * MAC address to register.
503 mlx5_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
505 DEBUG("%p: setting primary MAC address", (void *)dev);
506 mlx5_mac_addr_remove(dev, 0);
507 mlx5_mac_addr_add(dev, mac_addr, 0, 0);