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 /* DPDK headers don't like -pedantic. */
56 #pragma GCC diagnostic ignored "-Wpedantic"
58 #include <rte_ether.h>
59 #include <rte_ethdev.h>
60 #include <rte_common.h>
62 #pragma GCC diagnostic error "-Wpedantic"
66 #include "mlx5_utils.h"
67 #include "mlx5_rxtx.h"
68 #include "mlx5_defs.h"
71 * Get MAC address by querying netdevice.
74 * struct priv for the requested device.
76 * MAC address output buffer.
79 * 0 on success, -1 on failure and errno is set.
82 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
86 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
88 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
93 * Delete MAC flow steering rule.
96 * Pointer to hash RX queue structure.
103 hash_rxq_del_mac_flow(struct hash_rxq *hash_rxq, unsigned int mac_index,
104 unsigned int vlan_index)
107 const uint8_t (*mac)[ETHER_ADDR_LEN] =
108 (const uint8_t (*)[ETHER_ADDR_LEN])
109 hash_rxq->priv->mac[mac_index].addr_bytes;
112 assert(mac_index < RTE_DIM(hash_rxq->mac_flow));
113 assert(vlan_index < RTE_DIM(hash_rxq->mac_flow[mac_index]));
114 if (hash_rxq->mac_flow[mac_index][vlan_index] == NULL)
116 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
119 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
122 claim_zero(ibv_exp_destroy_flow(hash_rxq->mac_flow
123 [mac_index][vlan_index]));
124 hash_rxq->mac_flow[mac_index][vlan_index] = NULL;
128 * Unregister a MAC address from a hash RX queue.
131 * Pointer to hash RX queue structure.
136 hash_rxq_mac_addr_del(struct hash_rxq *hash_rxq, unsigned int mac_index)
140 assert(mac_index < RTE_DIM(hash_rxq->mac_flow));
141 for (i = 0; (i != RTE_DIM(hash_rxq->mac_flow[mac_index])); ++i)
142 hash_rxq_del_mac_flow(hash_rxq, mac_index, i);
146 * Unregister all MAC addresses from a hash RX queue.
149 * Pointer to hash RX queue structure.
152 hash_rxq_mac_addrs_del(struct hash_rxq *hash_rxq)
156 for (i = 0; (i != RTE_DIM(hash_rxq->mac_flow)); ++i)
157 hash_rxq_mac_addr_del(hash_rxq, i);
161 * Unregister a MAC address.
163 * This is done for each hash RX queue.
166 * Pointer to private structure.
171 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
175 assert(mac_index < RTE_DIM(priv->mac));
176 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
178 for (i = 0; (i != priv->hash_rxqs_n); ++i)
179 hash_rxq_mac_addr_del(&(*priv->hash_rxqs)[i], mac_index);
180 BITFIELD_RESET(priv->mac_configured, mac_index);
184 * Unregister all MAC addresses from all hash RX queues.
187 * Pointer to private structure.
190 priv_mac_addrs_disable(struct priv *priv)
194 for (i = 0; (i != priv->hash_rxqs_n); ++i)
195 hash_rxq_mac_addrs_del(&(*priv->hash_rxqs)[i]);
199 * DPDK callback to remove a MAC address.
202 * Pointer to Ethernet device structure.
207 mlx5_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
209 struct priv *priv = dev->data->dev_private;
211 if (mlx5_is_secondary())
215 DEBUG("%p: removing MAC address from index %" PRIu32,
217 if (index >= RTE_DIM(priv->mac))
219 priv_mac_addr_del(priv, index);
225 * Add MAC flow steering rule.
228 * Pointer to hash RX queue structure.
230 * MAC address index to register.
235 * 0 on success, errno value on failure.
238 hash_rxq_add_mac_flow(struct hash_rxq *hash_rxq, unsigned int mac_index,
239 unsigned int vlan_index)
241 struct ibv_exp_flow *flow;
242 struct priv *priv = hash_rxq->priv;
243 const uint8_t (*mac)[ETHER_ADDR_LEN] =
244 (const uint8_t (*)[ETHER_ADDR_LEN])
245 priv->mac[mac_index].addr_bytes;
246 FLOW_ATTR_SPEC_ETH(data, priv_flow_attr(priv, NULL, 0, hash_rxq->type));
247 struct ibv_exp_flow_attr *attr = &data->attr;
248 struct ibv_exp_flow_spec_eth *spec = &data->spec;
249 unsigned int vlan_enabled = !!priv->vlan_filter_n;
250 unsigned int vlan_id = priv->vlan_filter[vlan_index];
252 assert(mac_index < RTE_DIM(hash_rxq->mac_flow));
253 assert(vlan_index < RTE_DIM(hash_rxq->mac_flow[mac_index]));
254 if (hash_rxq->mac_flow[mac_index][vlan_index] != NULL)
257 * No padding must be inserted by the compiler between attr and spec.
258 * This layout is expected by libibverbs.
260 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
261 priv_flow_attr(priv, attr, sizeof(data), hash_rxq->type);
262 /* The first specification must be Ethernet. */
263 assert(spec->type == IBV_EXP_FLOW_SPEC_ETH);
264 assert(spec->size == sizeof(*spec));
265 *spec = (struct ibv_exp_flow_spec_eth){
266 .type = IBV_EXP_FLOW_SPEC_ETH,
267 .size = sizeof(*spec),
270 (*mac)[0], (*mac)[1], (*mac)[2],
271 (*mac)[3], (*mac)[4], (*mac)[5]
273 .vlan_tag = (vlan_enabled ? htons(vlan_id) : 0),
276 .dst_mac = "\xff\xff\xff\xff\xff\xff",
277 .vlan_tag = (vlan_enabled ? htons(0xfff) : 0),
280 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
281 " VLAN index %u filtering %s, ID %u",
283 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
286 (vlan_enabled ? "enabled" : "disabled"),
288 /* Create related flow. */
290 flow = ibv_exp_create_flow(hash_rxq->qp, attr);
292 /* It's not clear whether errno is always set in this case. */
293 ERROR("%p: flow configuration failed, errno=%d: %s",
294 (void *)hash_rxq, errno,
295 (errno ? strerror(errno) : "Unknown error"));
300 hash_rxq->mac_flow[mac_index][vlan_index] = flow;
305 * Register a MAC address in a hash RX queue.
308 * Pointer to hash RX queue structure.
310 * MAC address index to register.
313 * 0 on success, errno value on failure.
316 hash_rxq_mac_addr_add(struct hash_rxq *hash_rxq, unsigned int mac_index)
318 struct priv *priv = hash_rxq->priv;
322 assert(mac_index < RTE_DIM(hash_rxq->mac_flow));
323 assert(RTE_DIM(hash_rxq->mac_flow[mac_index]) ==
324 RTE_DIM(priv->vlan_filter));
325 /* Add a MAC address for each VLAN filter, or at least once. */
327 ret = hash_rxq_add_mac_flow(hash_rxq, mac_index, i);
329 /* Failure, rollback. */
331 hash_rxq_del_mac_flow(hash_rxq, mac_index,
335 } while (++i < priv->vlan_filter_n);
340 * Register all MAC addresses in a hash RX queue.
343 * Pointer to hash RX queue structure.
346 * 0 on success, errno value on failure.
349 hash_rxq_mac_addrs_add(struct hash_rxq *hash_rxq)
351 struct priv *priv = hash_rxq->priv;
355 assert(RTE_DIM(priv->mac) == RTE_DIM(hash_rxq->mac_flow));
356 for (i = 0; (i != RTE_DIM(priv->mac)); ++i) {
357 if (!BITFIELD_ISSET(priv->mac_configured, i))
359 ret = hash_rxq_mac_addr_add(hash_rxq, i);
362 /* Failure, rollback. */
364 hash_rxq_mac_addr_del(hash_rxq, --i);
372 * Register a MAC address.
374 * This is done for each hash RX queue.
377 * Pointer to private structure.
379 * MAC address index to use.
381 * MAC address to register.
384 * 0 on success, errno value on failure.
387 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
388 const uint8_t (*mac)[ETHER_ADDR_LEN])
393 assert(mac_index < RTE_DIM(priv->mac));
394 /* First, make sure this address isn't already configured. */
395 for (i = 0; (i != RTE_DIM(priv->mac)); ++i) {
396 /* Skip this index, it's going to be reconfigured. */
399 if (!BITFIELD_ISSET(priv->mac_configured, i))
401 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
403 /* Address already configured elsewhere, return with error. */
406 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
407 priv_mac_addr_del(priv, mac_index);
408 priv->mac[mac_index] = (struct ether_addr){
410 (*mac)[0], (*mac)[1], (*mac)[2],
411 (*mac)[3], (*mac)[4], (*mac)[5]
414 if (!priv_allow_flow_type(priv, HASH_RXQ_FLOW_TYPE_MAC))
416 for (i = 0; (i != priv->hash_rxqs_n); ++i) {
417 ret = hash_rxq_mac_addr_add(&(*priv->hash_rxqs)[i], mac_index);
420 /* Failure, rollback. */
422 hash_rxq_mac_addr_del(&(*priv->hash_rxqs)[--i],
427 BITFIELD_SET(priv->mac_configured, mac_index);
432 * Register all MAC addresses in all hash RX queues.
435 * Pointer to private structure.
438 * 0 on success, errno value on failure.
441 priv_mac_addrs_enable(struct priv *priv)
446 if (!priv_allow_flow_type(priv, HASH_RXQ_FLOW_TYPE_MAC))
448 for (i = 0; (i != priv->hash_rxqs_n); ++i) {
449 ret = hash_rxq_mac_addrs_add(&(*priv->hash_rxqs)[i]);
452 /* Failure, rollback. */
454 hash_rxq_mac_addrs_del(&(*priv->hash_rxqs)[--i]);
462 * DPDK callback to add a MAC address.
465 * Pointer to Ethernet device structure.
467 * MAC address to register.
471 * VMDq pool index to associate address with (ignored).
474 mlx5_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
475 uint32_t index, uint32_t vmdq)
477 struct priv *priv = dev->data->dev_private;
479 if (mlx5_is_secondary())
484 DEBUG("%p: adding MAC address at index %" PRIu32,
486 if (index >= RTE_DIM(priv->mac))
488 priv_mac_addr_add(priv, index,
489 (const uint8_t (*)[ETHER_ADDR_LEN])
490 mac_addr->addr_bytes);
496 * DPDK callback to set primary MAC address.
499 * Pointer to Ethernet device structure.
501 * MAC address to register.
504 mlx5_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
506 DEBUG("%p: setting primary MAC address", (void *)dev);
507 mlx5_mac_addr_remove(dev, 0);
508 mlx5_mac_addr_add(dev, mac_addr, 0, 0);