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.
39 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
41 #pragma GCC diagnostic ignored "-Wpedantic"
43 #include <infiniband/verbs.h>
45 #pragma GCC diagnostic error "-Wpedantic"
48 #include <rte_ethdev.h>
51 #include "mlx5_rxtx.h"
52 #include "mlx5_utils.h"
54 /* Initialization data for special flows. */
55 static const struct special_flow_init special_flow_init[] = {
56 [HASH_RXQ_FLOW_TYPE_ALLMULTI] = {
57 .dst_mac_val = "\x01\x00\x00\x00\x00\x00",
58 .dst_mac_mask = "\x01\x00\x00\x00\x00\x00",
68 [HASH_RXQ_FLOW_TYPE_BROADCAST] = {
69 .dst_mac_val = "\xff\xff\xff\xff\xff\xff",
70 .dst_mac_mask = "\xff\xff\xff\xff\xff\xff",
80 [HASH_RXQ_FLOW_TYPE_IPV6MULTI] = {
81 .dst_mac_val = "\x33\x33\x00\x00\x00\x00",
82 .dst_mac_mask = "\xff\xff\x00\x00\x00\x00",
93 * Enable a special flow in a hash RX queue for a given VLAN index.
96 * Pointer to hash RX queue structure.
103 * 0 on success, errno value on failure.
106 hash_rxq_special_flow_enable_vlan(struct hash_rxq *hash_rxq,
107 enum hash_rxq_flow_type flow_type,
108 unsigned int vlan_index)
110 struct priv *priv = hash_rxq->priv;
111 struct ibv_flow *flow;
112 FLOW_ATTR_SPEC_ETH(data, priv_flow_attr(priv, NULL, 0, hash_rxq->type));
113 struct ibv_flow_attr *attr = &data->attr;
114 struct ibv_flow_spec_eth *spec = &data->spec;
117 unsigned int vlan_enabled = (priv->vlan_filter_n &&
118 special_flow_init[flow_type].per_vlan);
119 unsigned int vlan_id = priv->vlan_filter[vlan_index];
121 /* Check if flow is relevant for this hash_rxq. */
122 if (!(special_flow_init[flow_type].hash_types & (1 << hash_rxq->type)))
124 /* Check if flow already exists. */
125 if (hash_rxq->special_flow[flow_type][vlan_index] != NULL)
129 * No padding must be inserted by the compiler between attr and spec.
130 * This layout is expected by libibverbs.
132 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
133 priv_flow_attr(priv, attr, sizeof(data), hash_rxq->type);
134 /* The first specification must be Ethernet. */
135 assert(spec->type == IBV_FLOW_SPEC_ETH);
136 assert(spec->size == sizeof(*spec));
138 mac = special_flow_init[flow_type].dst_mac_val;
139 mask = special_flow_init[flow_type].dst_mac_mask;
140 *spec = (struct ibv_flow_spec_eth){
141 .type = IBV_FLOW_SPEC_ETH,
142 .size = sizeof(*spec),
145 mac[0], mac[1], mac[2],
146 mac[3], mac[4], mac[5],
148 .vlan_tag = (vlan_enabled ?
149 rte_cpu_to_be_16(vlan_id) :
154 mask[0], mask[1], mask[2],
155 mask[3], mask[4], mask[5],
157 .vlan_tag = (vlan_enabled ?
158 rte_cpu_to_be_16(0xfff) :
164 flow = ibv_create_flow(hash_rxq->qp, attr);
166 /* It's not clear whether errno is always set in this case. */
167 ERROR("%p: flow configuration failed, errno=%d: %s",
168 (void *)hash_rxq, errno,
169 (errno ? strerror(errno) : "Unknown error"));
174 hash_rxq->special_flow[flow_type][vlan_index] = flow;
175 DEBUG("%p: special flow %s (index %d) VLAN %u (index %u) enabled",
176 (void *)hash_rxq, hash_rxq_flow_type_str(flow_type), flow_type,
177 vlan_id, vlan_index);
182 * Disable a special flow in a hash RX queue for a given VLAN index.
185 * Pointer to hash RX queue structure.
192 hash_rxq_special_flow_disable_vlan(struct hash_rxq *hash_rxq,
193 enum hash_rxq_flow_type flow_type,
194 unsigned int vlan_index)
196 struct ibv_flow *flow =
197 hash_rxq->special_flow[flow_type][vlan_index];
201 claim_zero(ibv_destroy_flow(flow));
202 hash_rxq->special_flow[flow_type][vlan_index] = NULL;
203 DEBUG("%p: special flow %s (index %d) VLAN %u (index %u) disabled",
204 (void *)hash_rxq, hash_rxq_flow_type_str(flow_type), flow_type,
205 hash_rxq->priv->vlan_filter[vlan_index], vlan_index);
209 * Enable a special flow in a hash RX queue.
212 * Pointer to hash RX queue structure.
219 * 0 on success, errno value on failure.
222 hash_rxq_special_flow_enable(struct hash_rxq *hash_rxq,
223 enum hash_rxq_flow_type flow_type)
225 struct priv *priv = hash_rxq->priv;
229 assert((unsigned int)flow_type < RTE_DIM(hash_rxq->special_flow));
230 assert(RTE_DIM(hash_rxq->special_flow[flow_type]) ==
231 RTE_DIM(priv->vlan_filter));
232 /* Add a special flow for each VLAN filter when relevant. */
234 ret = hash_rxq_special_flow_enable_vlan(hash_rxq, flow_type, i);
236 /* Failure, rollback. */
238 hash_rxq_special_flow_disable_vlan(hash_rxq,
243 } while (special_flow_init[flow_type].per_vlan &&
244 ++i < priv->vlan_filter_n);
249 * Disable a special flow in a hash RX queue.
252 * Pointer to hash RX queue structure.
257 hash_rxq_special_flow_disable(struct hash_rxq *hash_rxq,
258 enum hash_rxq_flow_type flow_type)
262 assert((unsigned int)flow_type < RTE_DIM(hash_rxq->special_flow));
263 for (i = 0; (i != RTE_DIM(hash_rxq->special_flow[flow_type])); ++i)
264 hash_rxq_special_flow_disable_vlan(hash_rxq, flow_type, i);
268 * Enable a special flow in all hash RX queues.
276 * 0 on success, errno value on failure.
279 priv_special_flow_enable(struct priv *priv, enum hash_rxq_flow_type flow_type)
283 if (!priv_allow_flow_type(priv, flow_type))
285 for (i = 0; (i != priv->hash_rxqs_n); ++i) {
286 struct hash_rxq *hash_rxq = &(*priv->hash_rxqs)[i];
289 ret = hash_rxq_special_flow_enable(hash_rxq, flow_type);
292 /* Failure, rollback. */
294 hash_rxq = &(*priv->hash_rxqs)[--i];
295 hash_rxq_special_flow_disable(hash_rxq, flow_type);
303 * Disable a special flow in all hash RX queues.
311 priv_special_flow_disable(struct priv *priv, enum hash_rxq_flow_type flow_type)
315 for (i = 0; (i != priv->hash_rxqs_n); ++i) {
316 struct hash_rxq *hash_rxq = &(*priv->hash_rxqs)[i];
318 hash_rxq_special_flow_disable(hash_rxq, flow_type);
323 * Enable all special flows in all hash RX queues.
329 priv_special_flow_enable_all(struct priv *priv)
331 enum hash_rxq_flow_type flow_type;
335 for (flow_type = HASH_RXQ_FLOW_TYPE_ALLMULTI;
336 flow_type != HASH_RXQ_FLOW_TYPE_MAC;
340 ret = priv_special_flow_enable(priv, flow_type);
343 /* Failure, rollback. */
345 priv_special_flow_disable(priv, --flow_type);
352 * Disable all special flows in all hash RX queues.
358 priv_special_flow_disable_all(struct priv *priv)
360 enum hash_rxq_flow_type flow_type;
362 for (flow_type = HASH_RXQ_FLOW_TYPE_ALLMULTI;
363 flow_type != HASH_RXQ_FLOW_TYPE_MAC;
365 priv_special_flow_disable(priv, flow_type);
369 * DPDK callback to enable promiscuous mode.
372 * Pointer to Ethernet device structure.
375 mlx5_promiscuous_enable(struct rte_eth_dev *dev)
377 struct rte_flow_item_eth eth = {
378 .dst.addr_bytes = "\x00\x00\x00\x00\x00\x00",
379 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
383 if (mlx5_is_secondary())
385 dev->data->promiscuous = 1;
386 claim_zero(mlx5_ctrl_flow(dev, ð, ð, 1));
390 * DPDK callback to disable promiscuous mode.
393 * Pointer to Ethernet device structure.
396 mlx5_promiscuous_disable(struct rte_eth_dev *dev)
398 struct rte_flow_item_eth eth = {
399 .dst.addr_bytes = "\x00\x00\x00\x00\x00\x00",
400 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
404 if (mlx5_is_secondary())
406 dev->data->promiscuous = 0;
407 claim_zero(mlx5_ctrl_flow(dev, ð, ð, 0));
411 * DPDK callback to enable allmulti mode.
414 * Pointer to Ethernet device structure.
417 mlx5_allmulticast_enable(struct rte_eth_dev *dev)
419 struct priv *priv = dev->data->dev_private;
422 if (mlx5_is_secondary())
426 priv->allmulti_req = 1;
427 ret = priv_rehash_flows(priv);
429 ERROR("error while enabling allmulticast mode: %s",
435 * DPDK callback to disable allmulti mode.
438 * Pointer to Ethernet device structure.
441 mlx5_allmulticast_disable(struct rte_eth_dev *dev)
443 struct priv *priv = dev->data->dev_private;
446 if (mlx5_is_secondary())
450 priv->allmulti_req = 0;
451 ret = priv_rehash_flows(priv);
453 ERROR("error while disabling allmulticast mode: %s",