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 /* DPDK headers don't like -pedantic. */
50 #pragma GCC diagnostic ignored "-Wpedantic"
52 #include <rte_ethdev.h>
54 #pragma GCC diagnostic error "-Wpedantic"
58 #include "mlx5_rxtx.h"
59 #include "mlx5_utils.h"
61 /* Initialization data for special flows. */
62 static const struct special_flow_init special_flow_init[] = {
63 [HASH_RXQ_FLOW_TYPE_PROMISC] = {
64 .dst_mac_val = "\x00\x00\x00\x00\x00\x00",
65 .dst_mac_mask = "\x00\x00\x00\x00\x00\x00",
77 [HASH_RXQ_FLOW_TYPE_ALLMULTI] = {
78 .dst_mac_val = "\x01\x00\x00\x00\x00\x00",
79 .dst_mac_mask = "\x01\x00\x00\x00\x00\x00",
89 [HASH_RXQ_FLOW_TYPE_BROADCAST] = {
90 .dst_mac_val = "\xff\xff\xff\xff\xff\xff",
91 .dst_mac_mask = "\xff\xff\xff\xff\xff\xff",
101 [HASH_RXQ_FLOW_TYPE_IPV6MULTI] = {
102 .dst_mac_val = "\x33\x33\x00\x00\x00\x00",
103 .dst_mac_mask = "\xff\xff\x00\x00\x00\x00",
105 1 << HASH_RXQ_UDPV6 |
114 * Enable a special flow in a hash RX queue for a given VLAN index.
117 * Pointer to hash RX queue structure.
124 * 0 on success, errno value on failure.
127 hash_rxq_special_flow_enable_vlan(struct hash_rxq *hash_rxq,
128 enum hash_rxq_flow_type flow_type,
129 unsigned int vlan_index)
131 struct priv *priv = hash_rxq->priv;
132 struct ibv_exp_flow *flow;
133 FLOW_ATTR_SPEC_ETH(data, priv_flow_attr(priv, NULL, 0, hash_rxq->type));
134 struct ibv_exp_flow_attr *attr = &data->attr;
135 struct ibv_exp_flow_spec_eth *spec = &data->spec;
138 unsigned int vlan_enabled = (priv->vlan_filter_n &&
139 special_flow_init[flow_type].per_vlan);
140 unsigned int vlan_id = priv->vlan_filter[vlan_index];
142 /* Check if flow is relevant for this hash_rxq. */
143 if (!(special_flow_init[flow_type].hash_types & (1 << hash_rxq->type)))
145 /* Check if flow already exists. */
146 if (hash_rxq->special_flow[flow_type][vlan_index] != NULL)
150 * No padding must be inserted by the compiler between attr and spec.
151 * This layout is expected by libibverbs.
153 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
154 priv_flow_attr(priv, attr, sizeof(data), hash_rxq->type);
155 /* The first specification must be Ethernet. */
156 assert(spec->type == IBV_EXP_FLOW_SPEC_ETH);
157 assert(spec->size == sizeof(*spec));
159 mac = special_flow_init[flow_type].dst_mac_val;
160 mask = special_flow_init[flow_type].dst_mac_mask;
161 *spec = (struct ibv_exp_flow_spec_eth){
162 .type = IBV_EXP_FLOW_SPEC_ETH,
163 .size = sizeof(*spec),
166 mac[0], mac[1], mac[2],
167 mac[3], mac[4], mac[5],
169 .vlan_tag = (vlan_enabled ? htons(vlan_id) : 0),
173 mask[0], mask[1], mask[2],
174 mask[3], mask[4], mask[5],
176 .vlan_tag = (vlan_enabled ? htons(0xfff) : 0),
181 flow = ibv_exp_create_flow(hash_rxq->qp, attr);
183 /* It's not clear whether errno is always set in this case. */
184 ERROR("%p: flow configuration failed, errno=%d: %s",
185 (void *)hash_rxq, errno,
186 (errno ? strerror(errno) : "Unknown error"));
191 hash_rxq->special_flow[flow_type][vlan_index] = flow;
192 DEBUG("%p: special flow %s (index %d) VLAN %u (index %u) enabled",
193 (void *)hash_rxq, hash_rxq_flow_type_str(flow_type), flow_type,
194 vlan_id, vlan_index);
199 * Disable a special flow in a hash RX queue for a given VLAN index.
202 * Pointer to hash RX queue structure.
209 hash_rxq_special_flow_disable_vlan(struct hash_rxq *hash_rxq,
210 enum hash_rxq_flow_type flow_type,
211 unsigned int vlan_index)
213 struct ibv_exp_flow *flow =
214 hash_rxq->special_flow[flow_type][vlan_index];
218 claim_zero(ibv_exp_destroy_flow(flow));
219 hash_rxq->special_flow[flow_type][vlan_index] = NULL;
220 DEBUG("%p: special flow %s (index %d) VLAN %u (index %u) disabled",
221 (void *)hash_rxq, hash_rxq_flow_type_str(flow_type), flow_type,
222 hash_rxq->priv->vlan_filter[vlan_index], vlan_index);
226 * Enable a special flow in a hash RX queue.
229 * Pointer to hash RX queue structure.
236 * 0 on success, errno value on failure.
239 hash_rxq_special_flow_enable(struct hash_rxq *hash_rxq,
240 enum hash_rxq_flow_type flow_type)
242 struct priv *priv = hash_rxq->priv;
246 assert((unsigned int)flow_type < RTE_DIM(hash_rxq->special_flow));
247 assert(RTE_DIM(hash_rxq->special_flow[flow_type]) ==
248 RTE_DIM(priv->vlan_filter));
249 /* Add a special flow for each VLAN filter when relevant. */
251 ret = hash_rxq_special_flow_enable_vlan(hash_rxq, flow_type, i);
253 /* Failure, rollback. */
255 hash_rxq_special_flow_disable_vlan(hash_rxq,
260 } while (special_flow_init[flow_type].per_vlan &&
261 ++i < priv->vlan_filter_n);
266 * Disable a special flow in a hash RX queue.
269 * Pointer to hash RX queue structure.
274 hash_rxq_special_flow_disable(struct hash_rxq *hash_rxq,
275 enum hash_rxq_flow_type flow_type)
279 assert((unsigned int)flow_type < RTE_DIM(hash_rxq->special_flow));
280 for (i = 0; (i != RTE_DIM(hash_rxq->special_flow[flow_type])); ++i)
281 hash_rxq_special_flow_disable_vlan(hash_rxq, flow_type, i);
285 * Enable a special flow in all hash RX queues.
293 * 0 on success, errno value on failure.
296 priv_special_flow_enable(struct priv *priv, enum hash_rxq_flow_type flow_type)
300 if (!priv_allow_flow_type(priv, flow_type))
302 for (i = 0; (i != priv->hash_rxqs_n); ++i) {
303 struct hash_rxq *hash_rxq = &(*priv->hash_rxqs)[i];
306 ret = hash_rxq_special_flow_enable(hash_rxq, flow_type);
309 /* Failure, rollback. */
311 hash_rxq = &(*priv->hash_rxqs)[--i];
312 hash_rxq_special_flow_disable(hash_rxq, flow_type);
320 * Disable a special flow in all hash RX queues.
328 priv_special_flow_disable(struct priv *priv, enum hash_rxq_flow_type flow_type)
332 for (i = 0; (i != priv->hash_rxqs_n); ++i) {
333 struct hash_rxq *hash_rxq = &(*priv->hash_rxqs)[i];
335 hash_rxq_special_flow_disable(hash_rxq, flow_type);
340 * Enable all special flows in all hash RX queues.
346 priv_special_flow_enable_all(struct priv *priv)
348 enum hash_rxq_flow_type flow_type;
350 for (flow_type = HASH_RXQ_FLOW_TYPE_PROMISC;
351 flow_type != HASH_RXQ_FLOW_TYPE_MAC;
355 ret = priv_special_flow_enable(priv, flow_type);
358 /* Failure, rollback. */
360 priv_special_flow_disable(priv, --flow_type);
367 * Disable all special flows in all hash RX queues.
373 priv_special_flow_disable_all(struct priv *priv)
375 enum hash_rxq_flow_type flow_type;
377 for (flow_type = HASH_RXQ_FLOW_TYPE_PROMISC;
378 flow_type != HASH_RXQ_FLOW_TYPE_MAC;
380 priv_special_flow_disable(priv, flow_type);
384 * DPDK callback to enable promiscuous mode.
387 * Pointer to Ethernet device structure.
390 mlx5_promiscuous_enable(struct rte_eth_dev *dev)
392 struct priv *priv = dev->data->dev_private;
395 if (mlx5_is_secondary())
399 priv->promisc_req = 1;
400 ret = priv_rehash_flows(priv);
402 ERROR("error while enabling promiscuous mode: %s",
408 * DPDK callback to disable promiscuous mode.
411 * Pointer to Ethernet device structure.
414 mlx5_promiscuous_disable(struct rte_eth_dev *dev)
416 struct priv *priv = dev->data->dev_private;
419 if (mlx5_is_secondary())
423 priv->promisc_req = 0;
424 ret = priv_rehash_flows(priv);
426 ERROR("error while disabling promiscuous mode: %s",
432 * DPDK callback to enable allmulti mode.
435 * Pointer to Ethernet device structure.
438 mlx5_allmulticast_enable(struct rte_eth_dev *dev)
440 struct priv *priv = dev->data->dev_private;
443 if (mlx5_is_secondary())
447 priv->allmulti_req = 1;
448 ret = priv_rehash_flows(priv);
450 ERROR("error while enabling allmulticast mode: %s",
456 * DPDK callback to disable allmulti mode.
459 * Pointer to Ethernet device structure.
462 mlx5_allmulticast_disable(struct rte_eth_dev *dev)
464 struct priv *priv = dev->data->dev_private;
467 if (mlx5_is_secondary())
471 priv->allmulti_req = 0;
472 ret = priv_rehash_flows(priv);
474 ERROR("error while disabling allmulticast mode: %s",