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 hash RX queue structure.
104 hash_rxq_del_mac_flow(struct hash_rxq *hash_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 hash_rxq->priv->mac[mac_index].addr_bytes;
113 assert(mac_index < RTE_DIM(hash_rxq->mac_flow));
114 assert(vlan_index < RTE_DIM(hash_rxq->mac_flow[mac_index]));
115 if (hash_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(hash_rxq->mac_flow
124 [mac_index][vlan_index]));
125 hash_rxq->mac_flow[mac_index][vlan_index] = NULL;
129 * Unregister a MAC address from a hash RX queue.
132 * Pointer to hash RX queue structure.
137 hash_rxq_mac_addr_del(struct hash_rxq *hash_rxq, unsigned int mac_index)
141 assert(mac_index < RTE_DIM(hash_rxq->mac_flow));
142 for (i = 0; (i != RTE_DIM(hash_rxq->mac_flow[mac_index])); ++i)
143 hash_rxq_del_mac_flow(hash_rxq, mac_index, i);
147 * Unregister all MAC addresses from a hash RX queue.
150 * Pointer to hash RX queue structure.
153 hash_rxq_mac_addrs_del(struct hash_rxq *hash_rxq)
157 for (i = 0; (i != RTE_DIM(hash_rxq->mac_flow)); ++i)
158 hash_rxq_mac_addr_del(hash_rxq, i);
162 * Unregister a MAC address.
164 * This is done for each hash RX queue.
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))
179 for (i = 0; (i != priv->hash_rxqs_n); ++i)
180 hash_rxq_mac_addr_del(&(*priv->hash_rxqs)[i], mac_index);
181 BITFIELD_RESET(priv->mac_configured, mac_index);
185 * Unregister all MAC addresses from all hash RX queues.
188 * Pointer to private structure.
191 priv_mac_addrs_disable(struct priv *priv)
195 for (i = 0; (i != priv->hash_rxqs_n); ++i)
196 hash_rxq_mac_addrs_del(&(*priv->hash_rxqs)[i]);
200 * DPDK callback to remove a MAC address.
203 * Pointer to Ethernet device structure.
208 mlx5_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
210 struct priv *priv = dev->data->dev_private;
213 DEBUG("%p: removing MAC address from index %" PRIu32,
215 /* Last array entry is reserved for broadcast. */
216 if (index >= (RTE_DIM(priv->mac) - 1))
218 priv_mac_addr_del(priv, index);
224 * Add MAC flow steering rule.
227 * Pointer to hash RX queue structure.
229 * MAC address index to register.
234 * 0 on success, errno value on failure.
237 hash_rxq_add_mac_flow(struct hash_rxq *hash_rxq, unsigned int mac_index,
238 unsigned int vlan_index)
240 struct ibv_flow *flow;
241 struct priv *priv = hash_rxq->priv;
242 const uint8_t (*mac)[ETHER_ADDR_LEN] =
243 (const uint8_t (*)[ETHER_ADDR_LEN])
244 priv->mac[mac_index].addr_bytes;
245 struct __attribute__((packed)) {
246 struct ibv_flow_attr attr;
247 struct ibv_flow_spec_eth spec;
249 struct ibv_flow_attr *attr = &data.attr;
250 struct ibv_flow_spec_eth *spec = &data.spec;
251 unsigned int vlan_enabled = !!priv->vlan_filter_n;
252 unsigned int vlan_id = priv->vlan_filter[vlan_index];
254 assert(mac_index < RTE_DIM(hash_rxq->mac_flow));
255 assert(vlan_index < RTE_DIM(hash_rxq->mac_flow[mac_index]));
256 if (hash_rxq->mac_flow[mac_index][vlan_index] != NULL)
259 * No padding must be inserted by the compiler between attr and spec.
260 * This layout is expected by libibverbs.
262 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
263 *attr = (struct ibv_flow_attr){
264 .type = IBV_FLOW_ATTR_NORMAL,
269 *spec = (struct ibv_flow_spec_eth){
270 .type = IBV_FLOW_SPEC_ETH,
271 .size = sizeof(*spec),
274 (*mac)[0], (*mac)[1], (*mac)[2],
275 (*mac)[3], (*mac)[4], (*mac)[5]
277 .vlan_tag = (vlan_enabled ? htons(vlan_id) : 0),
280 .dst_mac = "\xff\xff\xff\xff\xff\xff",
281 .vlan_tag = (vlan_enabled ? htons(0xfff) : 0),
284 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
285 " VLAN index %u filtering %s, ID %u",
287 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
290 (vlan_enabled ? "enabled" : "disabled"),
292 /* Create related flow. */
294 flow = ibv_create_flow(hash_rxq->qp, attr);
296 /* It's not clear whether errno is always set in this case. */
297 ERROR("%p: flow configuration failed, errno=%d: %s",
298 (void *)hash_rxq, errno,
299 (errno ? strerror(errno) : "Unknown error"));
304 hash_rxq->mac_flow[mac_index][vlan_index] = flow;
309 * Register a MAC address in a hash RX queue.
312 * Pointer to hash RX queue structure.
314 * MAC address index to register.
317 * 0 on success, errno value on failure.
320 hash_rxq_mac_addr_add(struct hash_rxq *hash_rxq, unsigned int mac_index)
322 struct priv *priv = hash_rxq->priv;
326 assert(mac_index < RTE_DIM(hash_rxq->mac_flow));
327 assert(RTE_DIM(hash_rxq->mac_flow[mac_index]) ==
328 RTE_DIM(priv->vlan_filter));
329 /* Add a MAC address for each VLAN filter, or at least once. */
331 ret = hash_rxq_add_mac_flow(hash_rxq, mac_index, i);
333 /* Failure, rollback. */
335 hash_rxq_del_mac_flow(hash_rxq, mac_index,
339 } while (++i < priv->vlan_filter_n);
344 * Register all MAC addresses in a hash RX queue.
347 * Pointer to hash RX queue structure.
350 * 0 on success, errno value on failure.
353 hash_rxq_mac_addrs_add(struct hash_rxq *hash_rxq)
355 struct priv *priv = hash_rxq->priv;
359 assert(RTE_DIM(priv->mac) == RTE_DIM(hash_rxq->mac_flow));
360 for (i = 0; (i != RTE_DIM(priv->mac)); ++i) {
361 if (!BITFIELD_ISSET(priv->mac_configured, i))
363 ret = hash_rxq_mac_addr_add(hash_rxq, i);
366 /* Failure, rollback. */
368 hash_rxq_mac_addr_del(hash_rxq, --i);
376 * Register a MAC address.
378 * This is done for each hash RX queue.
381 * Pointer to private structure.
383 * MAC address index to use.
385 * MAC address to register.
388 * 0 on success, errno value on failure.
391 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
392 const uint8_t (*mac)[ETHER_ADDR_LEN])
397 assert(mac_index < RTE_DIM(priv->mac));
398 /* First, make sure this address isn't already configured. */
399 for (i = 0; (i != RTE_DIM(priv->mac)); ++i) {
400 /* Skip this index, it's going to be reconfigured. */
403 if (!BITFIELD_ISSET(priv->mac_configured, i))
405 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
407 /* Address already configured elsewhere, return with error. */
410 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
411 priv_mac_addr_del(priv, mac_index);
412 priv->mac[mac_index] = (struct ether_addr){
414 (*mac)[0], (*mac)[1], (*mac)[2],
415 (*mac)[3], (*mac)[4], (*mac)[5]
418 for (i = 0; (i != priv->hash_rxqs_n); ++i) {
419 ret = hash_rxq_mac_addr_add(&(*priv->hash_rxqs)[i], mac_index);
422 /* Failure, rollback. */
424 hash_rxq_mac_addr_del(&(*priv->hash_rxqs)[--i],
428 BITFIELD_SET(priv->mac_configured, mac_index);
433 * Register all MAC addresses in all hash RX queues.
436 * Pointer to private structure.
439 * 0 on success, errno value on failure.
442 priv_mac_addrs_enable(struct priv *priv)
447 for (i = 0; (i != priv->hash_rxqs_n); ++i) {
448 ret = hash_rxq_mac_addrs_add(&(*priv->hash_rxqs)[i]);
451 /* Failure, rollback. */
453 hash_rxq_mac_addrs_del(&(*priv->hash_rxqs)[--i]);
461 * DPDK callback to add a MAC address.
464 * Pointer to Ethernet device structure.
466 * MAC address to register.
470 * VMDq pool index to associate address with (ignored).
473 mlx5_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
474 uint32_t index, uint32_t vmdq)
476 struct priv *priv = dev->data->dev_private;
480 DEBUG("%p: adding MAC address at index %" PRIu32,
482 /* Last array entry is reserved for broadcast. */
483 if (index >= (RTE_DIM(priv->mac) - 1))
485 priv_mac_addr_add(priv, index,
486 (const uint8_t (*)[ETHER_ADDR_LEN])
487 mac_addr->addr_bytes);