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_PROMISC] = {
57 .dst_mac_val = "\x00\x00\x00\x00\x00\x00",
58 .dst_mac_mask = "\x00\x00\x00\x00\x00\x00",
70 [HASH_RXQ_FLOW_TYPE_ALLMULTI] = {
71 .dst_mac_val = "\x01\x00\x00\x00\x00\x00",
72 .dst_mac_mask = "\x01\x00\x00\x00\x00\x00",
82 [HASH_RXQ_FLOW_TYPE_BROADCAST] = {
83 .dst_mac_val = "\xff\xff\xff\xff\xff\xff",
84 .dst_mac_mask = "\xff\xff\xff\xff\xff\xff",
94 [HASH_RXQ_FLOW_TYPE_IPV6MULTI] = {
95 .dst_mac_val = "\x33\x33\x00\x00\x00\x00",
96 .dst_mac_mask = "\xff\xff\x00\x00\x00\x00",
107 * Enable a special flow in a hash RX queue for a given VLAN index.
110 * Pointer to hash RX queue structure.
117 * 0 on success, errno value on failure.
120 hash_rxq_special_flow_enable_vlan(struct hash_rxq *hash_rxq,
121 enum hash_rxq_flow_type flow_type,
122 unsigned int vlan_index)
124 struct priv *priv = hash_rxq->priv;
125 struct ibv_exp_flow *flow;
126 FLOW_ATTR_SPEC_ETH(data, priv_flow_attr(priv, NULL, 0, hash_rxq->type));
127 struct ibv_exp_flow_attr *attr = &data->attr;
128 struct ibv_exp_flow_spec_eth *spec = &data->spec;
131 unsigned int vlan_enabled = (priv->vlan_filter_n &&
132 special_flow_init[flow_type].per_vlan);
133 unsigned int vlan_id = priv->vlan_filter[vlan_index];
135 /* Check if flow is relevant for this hash_rxq. */
136 if (!(special_flow_init[flow_type].hash_types & (1 << hash_rxq->type)))
138 /* Check if flow already exists. */
139 if (hash_rxq->special_flow[flow_type][vlan_index] != NULL)
143 * No padding must be inserted by the compiler between attr and spec.
144 * This layout is expected by libibverbs.
146 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
147 priv_flow_attr(priv, attr, sizeof(data), hash_rxq->type);
148 /* The first specification must be Ethernet. */
149 assert(spec->type == IBV_EXP_FLOW_SPEC_ETH);
150 assert(spec->size == sizeof(*spec));
152 mac = special_flow_init[flow_type].dst_mac_val;
153 mask = special_flow_init[flow_type].dst_mac_mask;
154 *spec = (struct ibv_exp_flow_spec_eth){
155 .type = IBV_EXP_FLOW_SPEC_ETH,
156 .size = sizeof(*spec),
159 mac[0], mac[1], mac[2],
160 mac[3], mac[4], mac[5],
162 .vlan_tag = (vlan_enabled ? htons(vlan_id) : 0),
166 mask[0], mask[1], mask[2],
167 mask[3], mask[4], mask[5],
169 .vlan_tag = (vlan_enabled ? htons(0xfff) : 0),
174 flow = ibv_exp_create_flow(hash_rxq->qp, attr);
176 /* It's not clear whether errno is always set in this case. */
177 ERROR("%p: flow configuration failed, errno=%d: %s",
178 (void *)hash_rxq, errno,
179 (errno ? strerror(errno) : "Unknown error"));
184 hash_rxq->special_flow[flow_type][vlan_index] = flow;
185 DEBUG("%p: special flow %s (index %d) VLAN %u (index %u) enabled",
186 (void *)hash_rxq, hash_rxq_flow_type_str(flow_type), flow_type,
187 vlan_id, vlan_index);
192 * Disable a special flow in a hash RX queue for a given VLAN index.
195 * Pointer to hash RX queue structure.
202 hash_rxq_special_flow_disable_vlan(struct hash_rxq *hash_rxq,
203 enum hash_rxq_flow_type flow_type,
204 unsigned int vlan_index)
206 struct ibv_exp_flow *flow =
207 hash_rxq->special_flow[flow_type][vlan_index];
211 claim_zero(ibv_exp_destroy_flow(flow));
212 hash_rxq->special_flow[flow_type][vlan_index] = NULL;
213 DEBUG("%p: special flow %s (index %d) VLAN %u (index %u) disabled",
214 (void *)hash_rxq, hash_rxq_flow_type_str(flow_type), flow_type,
215 hash_rxq->priv->vlan_filter[vlan_index], vlan_index);
219 * Enable a special flow in a hash RX queue.
222 * Pointer to hash RX queue structure.
229 * 0 on success, errno value on failure.
232 hash_rxq_special_flow_enable(struct hash_rxq *hash_rxq,
233 enum hash_rxq_flow_type flow_type)
235 struct priv *priv = hash_rxq->priv;
239 assert((unsigned int)flow_type < RTE_DIM(hash_rxq->special_flow));
240 assert(RTE_DIM(hash_rxq->special_flow[flow_type]) ==
241 RTE_DIM(priv->vlan_filter));
242 /* Add a special flow for each VLAN filter when relevant. */
244 ret = hash_rxq_special_flow_enable_vlan(hash_rxq, flow_type, i);
246 /* Failure, rollback. */
248 hash_rxq_special_flow_disable_vlan(hash_rxq,
253 } while (special_flow_init[flow_type].per_vlan &&
254 ++i < priv->vlan_filter_n);
259 * Disable a special flow in a hash RX queue.
262 * Pointer to hash RX queue structure.
267 hash_rxq_special_flow_disable(struct hash_rxq *hash_rxq,
268 enum hash_rxq_flow_type flow_type)
272 assert((unsigned int)flow_type < RTE_DIM(hash_rxq->special_flow));
273 for (i = 0; (i != RTE_DIM(hash_rxq->special_flow[flow_type])); ++i)
274 hash_rxq_special_flow_disable_vlan(hash_rxq, flow_type, i);
278 * Enable a special flow in all hash RX queues.
286 * 0 on success, errno value on failure.
289 priv_special_flow_enable(struct priv *priv, enum hash_rxq_flow_type flow_type)
293 if (!priv_allow_flow_type(priv, flow_type))
295 for (i = 0; (i != priv->hash_rxqs_n); ++i) {
296 struct hash_rxq *hash_rxq = &(*priv->hash_rxqs)[i];
299 ret = hash_rxq_special_flow_enable(hash_rxq, flow_type);
302 /* Failure, rollback. */
304 hash_rxq = &(*priv->hash_rxqs)[--i];
305 hash_rxq_special_flow_disable(hash_rxq, flow_type);
313 * Disable a special flow in all hash RX queues.
321 priv_special_flow_disable(struct priv *priv, enum hash_rxq_flow_type flow_type)
325 for (i = 0; (i != priv->hash_rxqs_n); ++i) {
326 struct hash_rxq *hash_rxq = &(*priv->hash_rxqs)[i];
328 hash_rxq_special_flow_disable(hash_rxq, flow_type);
333 * Enable all special flows in all hash RX queues.
339 priv_special_flow_enable_all(struct priv *priv)
341 enum hash_rxq_flow_type flow_type;
345 for (flow_type = HASH_RXQ_FLOW_TYPE_PROMISC;
346 flow_type != HASH_RXQ_FLOW_TYPE_MAC;
350 ret = priv_special_flow_enable(priv, flow_type);
353 /* Failure, rollback. */
355 priv_special_flow_disable(priv, --flow_type);
362 * Disable all special flows in all hash RX queues.
368 priv_special_flow_disable_all(struct priv *priv)
370 enum hash_rxq_flow_type flow_type;
372 for (flow_type = HASH_RXQ_FLOW_TYPE_PROMISC;
373 flow_type != HASH_RXQ_FLOW_TYPE_MAC;
375 priv_special_flow_disable(priv, flow_type);
379 * DPDK callback to enable promiscuous mode.
382 * Pointer to Ethernet device structure.
385 mlx5_promiscuous_enable(struct rte_eth_dev *dev)
387 struct priv *priv = dev->data->dev_private;
390 if (mlx5_is_secondary())
394 priv->promisc_req = 1;
395 ret = priv_rehash_flows(priv);
397 ERROR("error while enabling promiscuous mode: %s",
403 * DPDK callback to disable promiscuous mode.
406 * Pointer to Ethernet device structure.
409 mlx5_promiscuous_disable(struct rte_eth_dev *dev)
411 struct priv *priv = dev->data->dev_private;
414 if (mlx5_is_secondary())
418 priv->promisc_req = 0;
419 ret = priv_rehash_flows(priv);
421 ERROR("error while disabling promiscuous mode: %s",
427 * DPDK callback to enable allmulti mode.
430 * Pointer to Ethernet device structure.
433 mlx5_allmulticast_enable(struct rte_eth_dev *dev)
435 struct priv *priv = dev->data->dev_private;
438 if (mlx5_is_secondary())
442 priv->allmulti_req = 1;
443 ret = priv_rehash_flows(priv);
445 ERROR("error while enabling allmulticast mode: %s",
451 * DPDK callback to disable allmulti mode.
454 * Pointer to Ethernet device structure.
457 mlx5_allmulticast_disable(struct rte_eth_dev *dev)
459 struct priv *priv = dev->data->dev_private;
462 if (mlx5_is_secondary())
466 priv->allmulti_req = 0;
467 ret = priv_rehash_flows(priv);
469 ERROR("error while disabling allmulticast mode: %s",