1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016 6WIND S.A.
3 * Copyright 2016 Mellanox Technologies, Ltd
12 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
14 #pragma GCC diagnostic ignored "-Wpedantic"
16 #include <infiniband/verbs.h>
18 #pragma GCC diagnostic error "-Wpedantic"
21 #include <rte_common.h>
22 #include <rte_ether.h>
23 #include <rte_eth_ctrl.h>
24 #include <rte_ethdev_driver.h>
26 #include <rte_flow_driver.h>
27 #include <rte_malloc.h>
31 #include "mlx5_defs.h"
33 #include "mlx5_glue.h"
35 /* Dev ops structure defined in mlx5.c */
36 extern const struct eth_dev_ops mlx5_dev_ops;
37 extern const struct eth_dev_ops mlx5_dev_ops_isolate;
39 /* Pattern outer Layer bits. */
40 #define MLX5_FLOW_LAYER_OUTER_L2 (1u << 0)
41 #define MLX5_FLOW_LAYER_OUTER_L3_IPV4 (1u << 1)
42 #define MLX5_FLOW_LAYER_OUTER_L3_IPV6 (1u << 2)
43 #define MLX5_FLOW_LAYER_OUTER_L4_UDP (1u << 3)
44 #define MLX5_FLOW_LAYER_OUTER_L4_TCP (1u << 4)
45 #define MLX5_FLOW_LAYER_OUTER_VLAN (1u << 5)
47 /* Pattern inner Layer bits. */
48 #define MLX5_FLOW_LAYER_INNER_L2 (1u << 6)
49 #define MLX5_FLOW_LAYER_INNER_L3_IPV4 (1u << 7)
50 #define MLX5_FLOW_LAYER_INNER_L3_IPV6 (1u << 8)
51 #define MLX5_FLOW_LAYER_INNER_L4_UDP (1u << 9)
52 #define MLX5_FLOW_LAYER_INNER_L4_TCP (1u << 10)
53 #define MLX5_FLOW_LAYER_INNER_VLAN (1u << 11)
55 /* Pattern tunnel Layer bits. */
56 #define MLX5_FLOW_LAYER_VXLAN (1u << 12)
57 #define MLX5_FLOW_LAYER_VXLAN_GPE (1u << 13)
58 #define MLX5_FLOW_LAYER_GRE (1u << 14)
59 #define MLX5_FLOW_LAYER_MPLS (1u << 15)
62 #define MLX5_FLOW_LAYER_OUTER_L3 \
63 (MLX5_FLOW_LAYER_OUTER_L3_IPV4 | MLX5_FLOW_LAYER_OUTER_L3_IPV6)
64 #define MLX5_FLOW_LAYER_OUTER_L4 \
65 (MLX5_FLOW_LAYER_OUTER_L4_UDP | MLX5_FLOW_LAYER_OUTER_L4_TCP)
66 #define MLX5_FLOW_LAYER_OUTER \
67 (MLX5_FLOW_LAYER_OUTER_L2 | MLX5_FLOW_LAYER_OUTER_L3 | \
68 MLX5_FLOW_LAYER_OUTER_L4)
71 #define MLX5_FLOW_LAYER_TUNNEL \
72 (MLX5_FLOW_LAYER_VXLAN | MLX5_FLOW_LAYER_VXLAN_GPE | \
73 MLX5_FLOW_LAYER_GRE | MLX5_FLOW_LAYER_MPLS)
76 #define MLX5_FLOW_LAYER_INNER_L3 \
77 (MLX5_FLOW_LAYER_INNER_L3_IPV4 | MLX5_FLOW_LAYER_INNER_L3_IPV6)
78 #define MLX5_FLOW_LAYER_INNER_L4 \
79 (MLX5_FLOW_LAYER_INNER_L4_UDP | MLX5_FLOW_LAYER_INNER_L4_TCP)
80 #define MLX5_FLOW_LAYER_INNER \
81 (MLX5_FLOW_LAYER_INNER_L2 | MLX5_FLOW_LAYER_INNER_L3 | \
82 MLX5_FLOW_LAYER_INNER_L4)
84 /* Actions that modify the fate of matching traffic. */
85 #define MLX5_FLOW_FATE_DROP (1u << 0)
86 #define MLX5_FLOW_FATE_QUEUE (1u << 1)
87 #define MLX5_FLOW_FATE_RSS (1u << 2)
89 /* Modify a packet. */
90 #define MLX5_FLOW_MOD_FLAG (1u << 0)
91 #define MLX5_FLOW_MOD_MARK (1u << 1)
92 #define MLX5_FLOW_MOD_COUNT (1u << 2)
94 /* possible L3 layers protocols filtering. */
95 #define MLX5_IP_PROTOCOL_TCP 6
96 #define MLX5_IP_PROTOCOL_UDP 17
97 #define MLX5_IP_PROTOCOL_GRE 47
98 #define MLX5_IP_PROTOCOL_MPLS 147
100 /* Priority reserved for default flows. */
101 #define MLX5_FLOW_PRIO_RSVD ((uint32_t)-1)
103 enum mlx5_expansion {
105 MLX5_EXPANSION_ROOT_OUTER,
106 MLX5_EXPANSION_ROOT_ETH_VLAN,
107 MLX5_EXPANSION_ROOT_OUTER_ETH_VLAN,
108 MLX5_EXPANSION_OUTER_ETH,
109 MLX5_EXPANSION_OUTER_ETH_VLAN,
110 MLX5_EXPANSION_OUTER_VLAN,
111 MLX5_EXPANSION_OUTER_IPV4,
112 MLX5_EXPANSION_OUTER_IPV4_UDP,
113 MLX5_EXPANSION_OUTER_IPV4_TCP,
114 MLX5_EXPANSION_OUTER_IPV6,
115 MLX5_EXPANSION_OUTER_IPV6_UDP,
116 MLX5_EXPANSION_OUTER_IPV6_TCP,
117 MLX5_EXPANSION_VXLAN,
118 MLX5_EXPANSION_VXLAN_GPE,
122 MLX5_EXPANSION_ETH_VLAN,
125 MLX5_EXPANSION_IPV4_UDP,
126 MLX5_EXPANSION_IPV4_TCP,
128 MLX5_EXPANSION_IPV6_UDP,
129 MLX5_EXPANSION_IPV6_TCP,
132 /** Supported expansion of items. */
133 static const struct rte_flow_expand_node mlx5_support_expansion[] = {
134 [MLX5_EXPANSION_ROOT] = {
135 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH,
137 MLX5_EXPANSION_IPV6),
138 .type = RTE_FLOW_ITEM_TYPE_END,
140 [MLX5_EXPANSION_ROOT_OUTER] = {
141 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_ETH,
142 MLX5_EXPANSION_OUTER_IPV4,
143 MLX5_EXPANSION_OUTER_IPV6),
144 .type = RTE_FLOW_ITEM_TYPE_END,
146 [MLX5_EXPANSION_ROOT_ETH_VLAN] = {
147 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH_VLAN),
148 .type = RTE_FLOW_ITEM_TYPE_END,
150 [MLX5_EXPANSION_ROOT_OUTER_ETH_VLAN] = {
151 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_ETH_VLAN),
152 .type = RTE_FLOW_ITEM_TYPE_END,
154 [MLX5_EXPANSION_OUTER_ETH] = {
155 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_IPV4,
156 MLX5_EXPANSION_OUTER_IPV6,
157 MLX5_EXPANSION_MPLS),
158 .type = RTE_FLOW_ITEM_TYPE_ETH,
161 [MLX5_EXPANSION_OUTER_ETH_VLAN] = {
162 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_VLAN),
163 .type = RTE_FLOW_ITEM_TYPE_ETH,
166 [MLX5_EXPANSION_OUTER_VLAN] = {
167 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_IPV4,
168 MLX5_EXPANSION_OUTER_IPV6),
169 .type = RTE_FLOW_ITEM_TYPE_VLAN,
171 [MLX5_EXPANSION_OUTER_IPV4] = {
172 .next = RTE_FLOW_EXPAND_RSS_NEXT
173 (MLX5_EXPANSION_OUTER_IPV4_UDP,
174 MLX5_EXPANSION_OUTER_IPV4_TCP,
176 .type = RTE_FLOW_ITEM_TYPE_IPV4,
177 .rss_types = ETH_RSS_IPV4 | ETH_RSS_FRAG_IPV4 |
178 ETH_RSS_NONFRAG_IPV4_OTHER,
180 [MLX5_EXPANSION_OUTER_IPV4_UDP] = {
181 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_VXLAN,
182 MLX5_EXPANSION_VXLAN_GPE),
183 .type = RTE_FLOW_ITEM_TYPE_UDP,
184 .rss_types = ETH_RSS_NONFRAG_IPV4_UDP,
186 [MLX5_EXPANSION_OUTER_IPV4_TCP] = {
187 .type = RTE_FLOW_ITEM_TYPE_TCP,
188 .rss_types = ETH_RSS_NONFRAG_IPV4_TCP,
190 [MLX5_EXPANSION_OUTER_IPV6] = {
191 .next = RTE_FLOW_EXPAND_RSS_NEXT
192 (MLX5_EXPANSION_OUTER_IPV6_UDP,
193 MLX5_EXPANSION_OUTER_IPV6_TCP),
194 .type = RTE_FLOW_ITEM_TYPE_IPV6,
195 .rss_types = ETH_RSS_IPV6 | ETH_RSS_FRAG_IPV6 |
196 ETH_RSS_NONFRAG_IPV6_OTHER,
198 [MLX5_EXPANSION_OUTER_IPV6_UDP] = {
199 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_VXLAN,
200 MLX5_EXPANSION_VXLAN_GPE),
201 .type = RTE_FLOW_ITEM_TYPE_UDP,
202 .rss_types = ETH_RSS_NONFRAG_IPV6_UDP,
204 [MLX5_EXPANSION_OUTER_IPV6_TCP] = {
205 .type = RTE_FLOW_ITEM_TYPE_TCP,
206 .rss_types = ETH_RSS_NONFRAG_IPV6_TCP,
208 [MLX5_EXPANSION_VXLAN] = {
209 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH),
210 .type = RTE_FLOW_ITEM_TYPE_VXLAN,
212 [MLX5_EXPANSION_VXLAN_GPE] = {
213 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH,
215 MLX5_EXPANSION_IPV6),
216 .type = RTE_FLOW_ITEM_TYPE_VXLAN_GPE,
218 [MLX5_EXPANSION_GRE] = {
219 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4),
220 .type = RTE_FLOW_ITEM_TYPE_GRE,
222 [MLX5_EXPANSION_MPLS] = {
223 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4,
224 MLX5_EXPANSION_IPV6),
225 .type = RTE_FLOW_ITEM_TYPE_MPLS,
227 [MLX5_EXPANSION_ETH] = {
228 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4,
229 MLX5_EXPANSION_IPV6),
230 .type = RTE_FLOW_ITEM_TYPE_ETH,
232 [MLX5_EXPANSION_ETH_VLAN] = {
233 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_VLAN),
234 .type = RTE_FLOW_ITEM_TYPE_ETH,
236 [MLX5_EXPANSION_VLAN] = {
237 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4,
238 MLX5_EXPANSION_IPV6),
239 .type = RTE_FLOW_ITEM_TYPE_VLAN,
241 [MLX5_EXPANSION_IPV4] = {
242 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4_UDP,
243 MLX5_EXPANSION_IPV4_TCP),
244 .type = RTE_FLOW_ITEM_TYPE_IPV4,
245 .rss_types = ETH_RSS_IPV4 | ETH_RSS_FRAG_IPV4 |
246 ETH_RSS_NONFRAG_IPV4_OTHER,
248 [MLX5_EXPANSION_IPV4_UDP] = {
249 .type = RTE_FLOW_ITEM_TYPE_UDP,
250 .rss_types = ETH_RSS_NONFRAG_IPV4_UDP,
252 [MLX5_EXPANSION_IPV4_TCP] = {
253 .type = RTE_FLOW_ITEM_TYPE_TCP,
254 .rss_types = ETH_RSS_NONFRAG_IPV4_TCP,
256 [MLX5_EXPANSION_IPV6] = {
257 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV6_UDP,
258 MLX5_EXPANSION_IPV6_TCP),
259 .type = RTE_FLOW_ITEM_TYPE_IPV6,
260 .rss_types = ETH_RSS_IPV6 | ETH_RSS_FRAG_IPV6 |
261 ETH_RSS_NONFRAG_IPV6_OTHER,
263 [MLX5_EXPANSION_IPV6_UDP] = {
264 .type = RTE_FLOW_ITEM_TYPE_UDP,
265 .rss_types = ETH_RSS_NONFRAG_IPV6_UDP,
267 [MLX5_EXPANSION_IPV6_TCP] = {
268 .type = RTE_FLOW_ITEM_TYPE_TCP,
269 .rss_types = ETH_RSS_NONFRAG_IPV6_TCP,
273 /** Handles information leading to a drop fate. */
274 struct mlx5_flow_verbs {
275 LIST_ENTRY(mlx5_flow_verbs) next;
276 unsigned int size; /**< Size of the attribute. */
278 struct ibv_flow_attr *attr;
279 /**< Pointer to the Specification buffer. */
280 uint8_t *specs; /**< Pointer to the specifications. */
282 struct ibv_flow *flow; /**< Verbs flow pointer. */
283 struct mlx5_hrxq *hrxq; /**< Hash Rx queue object. */
284 uint64_t hash_fields; /**< Verbs hash Rx queue hash fields. */
287 /* Counters information. */
288 struct mlx5_flow_counter {
289 LIST_ENTRY(mlx5_flow_counter) next; /**< Pointer to the next counter. */
290 uint32_t shared:1; /**< Share counter ID with other flow rules. */
291 uint32_t ref_cnt:31; /**< Reference counter. */
292 uint32_t id; /**< Counter ID. */
293 struct ibv_counter_set *cs; /**< Holds the counters for the rule. */
294 uint64_t hits; /**< Number of packets matched by the rule. */
295 uint64_t bytes; /**< Number of bytes matched by the rule. */
298 /* Flow structure. */
300 TAILQ_ENTRY(rte_flow) next; /**< Pointer to the next flow structure. */
301 struct rte_flow_attr attributes; /**< User flow attribute. */
302 uint32_t l3_protocol_en:1; /**< Protocol filtering requested. */
304 /**< Bit-fields of present layers see MLX5_FLOW_LAYER_*. */
306 /**< Bit-fields of present modifier see MLX5_FLOW_MOD_*. */
308 /**< Bit-fields of present fate see MLX5_FLOW_FATE_*. */
309 uint8_t l3_protocol; /**< valid when l3_protocol_en is set. */
310 LIST_HEAD(verbs, mlx5_flow_verbs) verbs; /**< Verbs flows list. */
311 struct mlx5_flow_verbs *cur_verbs;
312 /**< Current Verbs flow structure being filled. */
313 struct mlx5_flow_counter *counter; /**< Holds Verbs flow counter. */
314 struct rte_flow_action_rss rss;/**< RSS context. */
315 uint8_t key[MLX5_RSS_HASH_KEY_LEN]; /**< RSS hash key. */
316 uint16_t (*queue)[]; /**< Destination queues to redirect traffic to. */
317 void *nl_flow; /**< Netlink flow buffer if relevant. */
320 static const struct rte_flow_ops mlx5_flow_ops = {
321 .validate = mlx5_flow_validate,
322 .create = mlx5_flow_create,
323 .destroy = mlx5_flow_destroy,
324 .flush = mlx5_flow_flush,
325 .isolate = mlx5_flow_isolate,
326 .query = mlx5_flow_query,
329 /* Convert FDIR request to Generic flow. */
331 struct rte_flow_attr attr;
332 struct rte_flow_action actions[2];
333 struct rte_flow_item items[4];
334 struct rte_flow_item_eth l2;
335 struct rte_flow_item_eth l2_mask;
337 struct rte_flow_item_ipv4 ipv4;
338 struct rte_flow_item_ipv6 ipv6;
341 struct rte_flow_item_ipv4 ipv4;
342 struct rte_flow_item_ipv6 ipv6;
345 struct rte_flow_item_udp udp;
346 struct rte_flow_item_tcp tcp;
349 struct rte_flow_item_udp udp;
350 struct rte_flow_item_tcp tcp;
352 struct rte_flow_action_queue queue;
355 /* Verbs specification header. */
356 struct ibv_spec_header {
357 enum ibv_flow_spec_type type;
362 * Number of sub priorities.
363 * For each kind of pattern matching i.e. L2, L3, L4 to have a correct
364 * matching on the NIC (firmware dependent) L4 most have the higher priority
365 * followed by L3 and ending with L2.
367 #define MLX5_PRIORITY_MAP_L2 2
368 #define MLX5_PRIORITY_MAP_L3 1
369 #define MLX5_PRIORITY_MAP_L4 0
370 #define MLX5_PRIORITY_MAP_MAX 3
372 /* Map of Verbs to Flow priority with 8 Verbs priorities. */
373 static const uint32_t priority_map_3[][MLX5_PRIORITY_MAP_MAX] = {
374 { 0, 1, 2 }, { 2, 3, 4 }, { 5, 6, 7 },
377 /* Map of Verbs to Flow priority with 16 Verbs priorities. */
378 static const uint32_t priority_map_5[][MLX5_PRIORITY_MAP_MAX] = {
379 { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 },
380 { 9, 10, 11 }, { 12, 13, 14 },
383 /* Tunnel information. */
384 struct mlx5_flow_tunnel_info {
385 uint32_t tunnel; /**< Tunnel bit (see MLX5_FLOW_*). */
386 uint32_t ptype; /**< Tunnel Ptype (see RTE_PTYPE_*). */
389 static struct mlx5_flow_tunnel_info tunnels_info[] = {
391 .tunnel = MLX5_FLOW_LAYER_VXLAN,
392 .ptype = RTE_PTYPE_TUNNEL_VXLAN | RTE_PTYPE_L4_UDP,
395 .tunnel = MLX5_FLOW_LAYER_VXLAN_GPE,
396 .ptype = RTE_PTYPE_TUNNEL_VXLAN_GPE | RTE_PTYPE_L4_UDP,
399 .tunnel = MLX5_FLOW_LAYER_GRE,
400 .ptype = RTE_PTYPE_TUNNEL_GRE,
403 .tunnel = MLX5_FLOW_LAYER_MPLS | MLX5_FLOW_LAYER_OUTER_L4_UDP,
404 .ptype = RTE_PTYPE_TUNNEL_MPLS_IN_GRE | RTE_PTYPE_L4_UDP,
407 .tunnel = MLX5_FLOW_LAYER_MPLS,
408 .ptype = RTE_PTYPE_TUNNEL_MPLS_IN_GRE,
413 * Discover the maximum number of priority available.
416 * Pointer to Ethernet device.
419 * number of supported flow priority on success, a negative errno
420 * value otherwise and rte_errno is set.
423 mlx5_flow_discover_priorities(struct rte_eth_dev *dev)
426 struct ibv_flow_attr attr;
427 struct ibv_flow_spec_eth eth;
428 struct ibv_flow_spec_action_drop drop;
434 .type = IBV_FLOW_SPEC_ETH,
435 .size = sizeof(struct ibv_flow_spec_eth),
438 .size = sizeof(struct ibv_flow_spec_action_drop),
439 .type = IBV_FLOW_SPEC_ACTION_DROP,
442 struct ibv_flow *flow;
443 struct mlx5_hrxq *drop = mlx5_hrxq_drop_new(dev);
444 uint16_t vprio[] = { 8, 16 };
452 for (i = 0; i != RTE_DIM(vprio); i++) {
453 flow_attr.attr.priority = vprio[i] - 1;
454 flow = mlx5_glue->create_flow(drop->qp, &flow_attr.attr);
457 claim_zero(mlx5_glue->destroy_flow(flow));
462 priority = RTE_DIM(priority_map_3);
465 priority = RTE_DIM(priority_map_5);
470 "port %u verbs maximum priority: %d expected 8/16",
471 dev->data->port_id, vprio[i]);
474 mlx5_hrxq_drop_release(dev);
475 DRV_LOG(INFO, "port %u flow maximum priority: %d",
476 dev->data->port_id, priority);
481 * Adjust flow priority.
484 * Pointer to Ethernet device.
486 * Pointer to an rte flow.
489 mlx5_flow_adjust_priority(struct rte_eth_dev *dev, struct rte_flow *flow)
491 struct priv *priv = dev->data->dev_private;
492 uint32_t priority = flow->attributes.priority;
493 uint32_t subpriority = flow->cur_verbs->attr->priority;
495 switch (priv->config.flow_prio) {
496 case RTE_DIM(priority_map_3):
497 priority = priority_map_3[priority][subpriority];
499 case RTE_DIM(priority_map_5):
500 priority = priority_map_5[priority][subpriority];
503 flow->cur_verbs->attr->priority = priority;
507 * Get a flow counter.
510 * Pointer to Ethernet device.
512 * Indicate if this counter is shared with other flows.
514 * Counter identifier.
517 * A pointer to the counter, NULL otherwise and rte_errno is set.
519 static struct mlx5_flow_counter *
520 mlx5_flow_counter_new(struct rte_eth_dev *dev, uint32_t shared, uint32_t id)
522 struct priv *priv = dev->data->dev_private;
523 struct mlx5_flow_counter *cnt;
525 LIST_FOREACH(cnt, &priv->flow_counters, next) {
526 if (!cnt->shared || cnt->shared != shared)
533 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
535 struct mlx5_flow_counter tmpl = {
538 .cs = mlx5_glue->create_counter_set
540 &(struct ibv_counter_set_init_attr){
541 .counter_set_id = id,
551 cnt = rte_calloc(__func__, 1, sizeof(*cnt), 0);
557 LIST_INSERT_HEAD(&priv->flow_counters, cnt, next);
565 * Release a flow counter.
568 * Pointer to the counter handler.
571 mlx5_flow_counter_release(struct mlx5_flow_counter *counter)
573 if (--counter->ref_cnt == 0) {
574 claim_zero(mlx5_glue->destroy_counter_set(counter->cs));
575 LIST_REMOVE(counter, next);
581 * Verify the @p attributes will be correctly understood by the NIC and store
582 * them in the @p flow if everything is correct.
585 * Pointer to Ethernet device.
586 * @param[in] attributes
587 * Pointer to flow attributes
588 * @param[in, out] flow
589 * Pointer to the rte_flow structure.
591 * Pointer to error structure.
594 * 0 on success, a negative errno value otherwise and rte_errno is set.
597 mlx5_flow_attributes(struct rte_eth_dev *dev,
598 const struct rte_flow_attr *attributes,
599 struct rte_flow *flow,
600 struct rte_flow_error *error)
602 uint32_t priority_max =
603 ((struct priv *)dev->data->dev_private)->config.flow_prio - 1;
605 if (attributes->group)
606 return rte_flow_error_set(error, ENOTSUP,
607 RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
609 "groups is not supported");
610 if (attributes->priority != MLX5_FLOW_PRIO_RSVD &&
611 attributes->priority >= priority_max)
612 return rte_flow_error_set(error, ENOTSUP,
613 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
615 "priority out of range");
616 if (attributes->egress)
617 return rte_flow_error_set(error, ENOTSUP,
618 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
620 "egress is not supported");
621 if (attributes->transfer)
622 return rte_flow_error_set(error, ENOTSUP,
623 RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
625 "transfer is not supported");
626 if (!attributes->ingress)
627 return rte_flow_error_set(error, ENOTSUP,
628 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
630 "ingress attribute is mandatory");
631 flow->attributes = *attributes;
632 if (attributes->priority == MLX5_FLOW_PRIO_RSVD)
633 flow->attributes.priority = priority_max;
638 * Verify the @p item specifications (spec, last, mask) are compatible with the
642 * Item specification.
644 * @p item->mask or flow default bit-masks.
645 * @param[in] nic_mask
646 * Bit-masks covering supported fields by the NIC to compare with user mask.
648 * Bit-masks size in bytes.
650 * Pointer to error structure.
653 * 0 on success, a negative errno value otherwise and rte_errno is set.
656 mlx5_flow_item_acceptable(const struct rte_flow_item *item,
658 const uint8_t *nic_mask,
660 struct rte_flow_error *error)
665 for (i = 0; i < size; ++i)
666 if ((nic_mask[i] | mask[i]) != nic_mask[i])
667 return rte_flow_error_set(error, ENOTSUP,
668 RTE_FLOW_ERROR_TYPE_ITEM,
670 "mask enables non supported"
672 if (!item->spec && (item->mask || item->last))
673 return rte_flow_error_set(error, EINVAL,
674 RTE_FLOW_ERROR_TYPE_ITEM,
676 "mask/last without a spec is not"
678 if (item->spec && item->last) {
684 for (i = 0; i < size; ++i) {
685 spec[i] = ((const uint8_t *)item->spec)[i] & mask[i];
686 last[i] = ((const uint8_t *)item->last)[i] & mask[i];
688 ret = memcmp(spec, last, size);
690 return rte_flow_error_set(error, ENOTSUP,
691 RTE_FLOW_ERROR_TYPE_ITEM,
693 "range is not supported");
699 * Add a verbs item specification into @p flow.
701 * @param[in, out] flow
702 * Pointer to flow structure.
704 * Create specification.
706 * Size in bytes of the specification to copy.
709 mlx5_flow_spec_verbs_add(struct rte_flow *flow, void *src, unsigned int size)
711 struct mlx5_flow_verbs *verbs = flow->cur_verbs;
716 dst = (void *)(verbs->specs + verbs->size);
717 memcpy(dst, src, size);
718 ++verbs->attr->num_of_specs;
724 * Adjust verbs hash fields according to the @p flow information.
726 * @param[in, out] flow.
727 * Pointer to flow structure.
729 * 1 when the hash field is for a tunnel item.
730 * @param[in] layer_types
732 * @param[in] hash_fields
736 mlx5_flow_verbs_hashfields_adjust(struct rte_flow *flow,
737 int tunnel __rte_unused,
738 uint32_t layer_types, uint64_t hash_fields)
740 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
741 hash_fields |= (tunnel ? IBV_RX_HASH_INNER : 0);
742 if (flow->rss.level == 2 && !tunnel)
744 else if (flow->rss.level < 2 && tunnel)
747 if (!(flow->rss.types & layer_types))
749 flow->cur_verbs->hash_fields |= hash_fields;
753 * Convert the @p item into a Verbs specification after ensuring the NIC
754 * will understand and process it correctly.
755 * If the necessary size for the conversion is greater than the @p flow_size,
756 * nothing is written in @p flow, the validation is still performed.
759 * Item specification.
760 * @param[in, out] flow
761 * Pointer to flow structure.
762 * @param[in] flow_size
763 * Size in bytes of the available space in @p flow, if too small, nothing is
766 * Pointer to error structure.
769 * On success the number of bytes consumed/necessary, if the returned value
770 * is lesser or equal to @p flow_size, the @p item has fully been converted,
771 * otherwise another call with this returned memory size should be done.
772 * On error, a negative errno value is returned and rte_errno is set.
775 mlx5_flow_item_eth(const struct rte_flow_item *item, struct rte_flow *flow,
776 const size_t flow_size, struct rte_flow_error *error)
778 const struct rte_flow_item_eth *spec = item->spec;
779 const struct rte_flow_item_eth *mask = item->mask;
780 const struct rte_flow_item_eth nic_mask = {
781 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
782 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
783 .type = RTE_BE16(0xffff),
785 const int tunnel = !!(flow->layers & MLX5_FLOW_LAYER_TUNNEL);
786 const unsigned int size = sizeof(struct ibv_flow_spec_eth);
787 struct ibv_flow_spec_eth eth = {
788 .type = IBV_FLOW_SPEC_ETH | (tunnel ? IBV_FLOW_SPEC_INNER : 0),
793 if (flow->layers & (tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
794 MLX5_FLOW_LAYER_OUTER_L2))
795 return rte_flow_error_set(error, ENOTSUP,
796 RTE_FLOW_ERROR_TYPE_ITEM,
798 "L2 layers already configured");
800 mask = &rte_flow_item_eth_mask;
801 ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
802 (const uint8_t *)&nic_mask,
803 sizeof(struct rte_flow_item_eth),
807 flow->layers |= tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
808 MLX5_FLOW_LAYER_OUTER_L2;
809 if (size > flow_size)
814 memcpy(ð.val.dst_mac, spec->dst.addr_bytes, ETHER_ADDR_LEN);
815 memcpy(ð.val.src_mac, spec->src.addr_bytes, ETHER_ADDR_LEN);
816 eth.val.ether_type = spec->type;
817 memcpy(ð.mask.dst_mac, mask->dst.addr_bytes, ETHER_ADDR_LEN);
818 memcpy(ð.mask.src_mac, mask->src.addr_bytes, ETHER_ADDR_LEN);
819 eth.mask.ether_type = mask->type;
820 /* Remove unwanted bits from values. */
821 for (i = 0; i < ETHER_ADDR_LEN; ++i) {
822 eth.val.dst_mac[i] &= eth.mask.dst_mac[i];
823 eth.val.src_mac[i] &= eth.mask.src_mac[i];
825 eth.val.ether_type &= eth.mask.ether_type;
827 flow->cur_verbs->attr->priority = MLX5_PRIORITY_MAP_L2;
828 mlx5_flow_spec_verbs_add(flow, ð, size);
833 * Update the VLAN tag in the Verbs Ethernet specification.
835 * @param[in, out] attr
836 * Pointer to Verbs attributes structure.
838 * Verbs structure containing the VLAN information to copy.
841 mlx5_flow_item_vlan_update(struct ibv_flow_attr *attr,
842 struct ibv_flow_spec_eth *eth)
845 const enum ibv_flow_spec_type search = eth->type;
846 struct ibv_spec_header *hdr = (struct ibv_spec_header *)
847 ((uint8_t *)attr + sizeof(struct ibv_flow_attr));
849 for (i = 0; i != attr->num_of_specs; ++i) {
850 if (hdr->type == search) {
851 struct ibv_flow_spec_eth *e =
852 (struct ibv_flow_spec_eth *)hdr;
854 e->val.vlan_tag = eth->val.vlan_tag;
855 e->mask.vlan_tag = eth->mask.vlan_tag;
856 e->val.ether_type = eth->val.ether_type;
857 e->mask.ether_type = eth->mask.ether_type;
860 hdr = (struct ibv_spec_header *)((uint8_t *)hdr + hdr->size);
865 * Convert the @p item into @p flow (or by updating the already present
866 * Ethernet Verbs) specification after ensuring the NIC will understand and
867 * process it correctly.
868 * If the necessary size for the conversion is greater than the @p flow_size,
869 * nothing is written in @p flow, the validation is still performed.
872 * Item specification.
873 * @param[in, out] flow
874 * Pointer to flow structure.
875 * @param[in] flow_size
876 * Size in bytes of the available space in @p flow, if too small, nothing is
879 * Pointer to error structure.
882 * On success the number of bytes consumed/necessary, if the returned value
883 * is lesser or equal to @p flow_size, the @p item has fully been converted,
884 * otherwise another call with this returned memory size should be done.
885 * On error, a negative errno value is returned and rte_errno is set.
888 mlx5_flow_item_vlan(const struct rte_flow_item *item, struct rte_flow *flow,
889 const size_t flow_size, struct rte_flow_error *error)
891 const struct rte_flow_item_vlan *spec = item->spec;
892 const struct rte_flow_item_vlan *mask = item->mask;
893 const struct rte_flow_item_vlan nic_mask = {
894 .tci = RTE_BE16(0x0fff),
895 .inner_type = RTE_BE16(0xffff),
897 unsigned int size = sizeof(struct ibv_flow_spec_eth);
898 const int tunnel = !!(flow->layers & MLX5_FLOW_LAYER_TUNNEL);
899 struct ibv_flow_spec_eth eth = {
900 .type = IBV_FLOW_SPEC_ETH | (tunnel ? IBV_FLOW_SPEC_INNER : 0),
904 const uint32_t l34m = tunnel ? (MLX5_FLOW_LAYER_INNER_L3 |
905 MLX5_FLOW_LAYER_INNER_L4) :
906 (MLX5_FLOW_LAYER_OUTER_L3 | MLX5_FLOW_LAYER_OUTER_L4);
907 const uint32_t vlanm = tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
908 MLX5_FLOW_LAYER_OUTER_VLAN;
909 const uint32_t l2m = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
910 MLX5_FLOW_LAYER_OUTER_L2;
912 if (flow->layers & vlanm)
913 return rte_flow_error_set(error, ENOTSUP,
914 RTE_FLOW_ERROR_TYPE_ITEM,
916 "VLAN layer already configured");
917 else if ((flow->layers & l34m) != 0)
918 return rte_flow_error_set(error, ENOTSUP,
919 RTE_FLOW_ERROR_TYPE_ITEM,
921 "L2 layer cannot follow L3/L4 layer");
923 mask = &rte_flow_item_vlan_mask;
924 ret = mlx5_flow_item_acceptable
925 (item, (const uint8_t *)mask,
926 (const uint8_t *)&nic_mask,
927 sizeof(struct rte_flow_item_vlan), error);
931 eth.val.vlan_tag = spec->tci;
932 eth.mask.vlan_tag = mask->tci;
933 eth.val.vlan_tag &= eth.mask.vlan_tag;
934 eth.val.ether_type = spec->inner_type;
935 eth.mask.ether_type = mask->inner_type;
936 eth.val.ether_type &= eth.mask.ether_type;
939 * From verbs perspective an empty VLAN is equivalent
940 * to a packet without VLAN layer.
942 if (!eth.mask.vlan_tag)
943 return rte_flow_error_set(error, EINVAL,
944 RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
946 "VLAN cannot be empty");
947 if (!(flow->layers & l2m)) {
948 if (size <= flow_size) {
949 flow->cur_verbs->attr->priority = MLX5_PRIORITY_MAP_L2;
950 mlx5_flow_spec_verbs_add(flow, ð, size);
954 mlx5_flow_item_vlan_update(flow->cur_verbs->attr,
956 size = 0; /* Only an update is done in eth specification. */
958 flow->layers |= tunnel ?
959 (MLX5_FLOW_LAYER_INNER_L2 | MLX5_FLOW_LAYER_INNER_VLAN) :
960 (MLX5_FLOW_LAYER_OUTER_L2 | MLX5_FLOW_LAYER_OUTER_VLAN);
965 * Convert the @p item into a Verbs specification after ensuring the NIC
966 * will understand and process it correctly.
967 * If the necessary size for the conversion is greater than the @p flow_size,
968 * nothing is written in @p flow, the validation is still performed.
971 * Item specification.
972 * @param[in, out] flow
973 * Pointer to flow structure.
974 * @param[in] flow_size
975 * Size in bytes of the available space in @p flow, if too small, nothing is
978 * Pointer to error structure.
981 * On success the number of bytes consumed/necessary, if the returned value
982 * is lesser or equal to @p flow_size, the @p item has fully been converted,
983 * otherwise another call with this returned memory size should be done.
984 * On error, a negative errno value is returned and rte_errno is set.
987 mlx5_flow_item_ipv4(const struct rte_flow_item *item, struct rte_flow *flow,
988 const size_t flow_size, struct rte_flow_error *error)
990 const struct rte_flow_item_ipv4 *spec = item->spec;
991 const struct rte_flow_item_ipv4 *mask = item->mask;
992 const struct rte_flow_item_ipv4 nic_mask = {
994 .src_addr = RTE_BE32(0xffffffff),
995 .dst_addr = RTE_BE32(0xffffffff),
996 .type_of_service = 0xff,
997 .next_proto_id = 0xff,
1000 const int tunnel = !!(flow->layers & MLX5_FLOW_LAYER_TUNNEL);
1001 unsigned int size = sizeof(struct ibv_flow_spec_ipv4_ext);
1002 struct ibv_flow_spec_ipv4_ext ipv4 = {
1003 .type = IBV_FLOW_SPEC_IPV4_EXT |
1004 (tunnel ? IBV_FLOW_SPEC_INNER : 0),
1009 if (flow->layers & (tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
1010 MLX5_FLOW_LAYER_OUTER_L3))
1011 return rte_flow_error_set(error, ENOTSUP,
1012 RTE_FLOW_ERROR_TYPE_ITEM,
1014 "multiple L3 layers not supported");
1015 else if (flow->layers & (tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1016 MLX5_FLOW_LAYER_OUTER_L4))
1017 return rte_flow_error_set(error, ENOTSUP,
1018 RTE_FLOW_ERROR_TYPE_ITEM,
1020 "L3 cannot follow an L4 layer.");
1022 mask = &rte_flow_item_ipv4_mask;
1023 ret = mlx5_flow_item_acceptable
1024 (item, (const uint8_t *)mask,
1025 (const uint8_t *)&nic_mask,
1026 sizeof(struct rte_flow_item_ipv4), error);
1029 flow->layers |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
1030 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1032 ipv4.val = (struct ibv_flow_ipv4_ext_filter){
1033 .src_ip = spec->hdr.src_addr,
1034 .dst_ip = spec->hdr.dst_addr,
1035 .proto = spec->hdr.next_proto_id,
1036 .tos = spec->hdr.type_of_service,
1038 ipv4.mask = (struct ibv_flow_ipv4_ext_filter){
1039 .src_ip = mask->hdr.src_addr,
1040 .dst_ip = mask->hdr.dst_addr,
1041 .proto = mask->hdr.next_proto_id,
1042 .tos = mask->hdr.type_of_service,
1044 /* Remove unwanted bits from values. */
1045 ipv4.val.src_ip &= ipv4.mask.src_ip;
1046 ipv4.val.dst_ip &= ipv4.mask.dst_ip;
1047 ipv4.val.proto &= ipv4.mask.proto;
1048 ipv4.val.tos &= ipv4.mask.tos;
1050 flow->l3_protocol_en = !!ipv4.mask.proto;
1051 flow->l3_protocol = ipv4.val.proto;
1052 if (size <= flow_size) {
1053 mlx5_flow_verbs_hashfields_adjust
1055 (ETH_RSS_IPV4 | ETH_RSS_FRAG_IPV4 |
1056 ETH_RSS_NONFRAG_IPV4_OTHER),
1057 (IBV_RX_HASH_SRC_IPV4 | IBV_RX_HASH_DST_IPV4));
1058 flow->cur_verbs->attr->priority = MLX5_PRIORITY_MAP_L3;
1059 mlx5_flow_spec_verbs_add(flow, &ipv4, size);
1065 * Convert the @p item into a Verbs specification after ensuring the NIC
1066 * will understand and process it correctly.
1067 * If the necessary size for the conversion is greater than the @p flow_size,
1068 * nothing is written in @p flow, the validation is still performed.
1071 * Item specification.
1072 * @param[in, out] flow
1073 * Pointer to flow structure.
1074 * @param[in] flow_size
1075 * Size in bytes of the available space in @p flow, if too small, nothing is
1078 * Pointer to error structure.
1081 * On success the number of bytes consumed/necessary, if the returned value
1082 * is lesser or equal to @p flow_size, the @p item has fully been converted,
1083 * otherwise another call with this returned memory size should be done.
1084 * On error, a negative errno value is returned and rte_errno is set.
1087 mlx5_flow_item_ipv6(const struct rte_flow_item *item, struct rte_flow *flow,
1088 const size_t flow_size, struct rte_flow_error *error)
1090 const struct rte_flow_item_ipv6 *spec = item->spec;
1091 const struct rte_flow_item_ipv6 *mask = item->mask;
1092 const struct rte_flow_item_ipv6 nic_mask = {
1095 "\xff\xff\xff\xff\xff\xff\xff\xff"
1096 "\xff\xff\xff\xff\xff\xff\xff\xff",
1098 "\xff\xff\xff\xff\xff\xff\xff\xff"
1099 "\xff\xff\xff\xff\xff\xff\xff\xff",
1100 .vtc_flow = RTE_BE32(0xffffffff),
1105 const int tunnel = !!(flow->layers & MLX5_FLOW_LAYER_TUNNEL);
1106 unsigned int size = sizeof(struct ibv_flow_spec_ipv6);
1107 struct ibv_flow_spec_ipv6 ipv6 = {
1108 .type = IBV_FLOW_SPEC_IPV6 | (tunnel ? IBV_FLOW_SPEC_INNER : 0),
1113 if (flow->layers & (tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
1114 MLX5_FLOW_LAYER_OUTER_L3))
1115 return rte_flow_error_set(error, ENOTSUP,
1116 RTE_FLOW_ERROR_TYPE_ITEM,
1118 "multiple L3 layers not supported");
1119 else if (flow->layers & (tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1120 MLX5_FLOW_LAYER_OUTER_L4))
1121 return rte_flow_error_set(error, ENOTSUP,
1122 RTE_FLOW_ERROR_TYPE_ITEM,
1124 "L3 cannot follow an L4 layer.");
1126 * IPv6 is not recognised by the NIC inside a GRE tunnel.
1127 * Such support has to be disabled as the rule will be
1128 * accepted. Issue reproduced with Mellanox OFED 4.3-3.0.2.1 and
1129 * Mellanox OFED 4.4-1.0.0.0.
1131 if (tunnel && flow->layers & MLX5_FLOW_LAYER_GRE)
1132 return rte_flow_error_set(error, ENOTSUP,
1133 RTE_FLOW_ERROR_TYPE_ITEM,
1135 "IPv6 inside a GRE tunnel is"
1136 " not recognised.");
1138 mask = &rte_flow_item_ipv6_mask;
1139 ret = mlx5_flow_item_acceptable
1140 (item, (const uint8_t *)mask,
1141 (const uint8_t *)&nic_mask,
1142 sizeof(struct rte_flow_item_ipv6), error);
1145 flow->layers |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
1146 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1149 uint32_t vtc_flow_val;
1150 uint32_t vtc_flow_mask;
1152 memcpy(&ipv6.val.src_ip, spec->hdr.src_addr,
1153 RTE_DIM(ipv6.val.src_ip));
1154 memcpy(&ipv6.val.dst_ip, spec->hdr.dst_addr,
1155 RTE_DIM(ipv6.val.dst_ip));
1156 memcpy(&ipv6.mask.src_ip, mask->hdr.src_addr,
1157 RTE_DIM(ipv6.mask.src_ip));
1158 memcpy(&ipv6.mask.dst_ip, mask->hdr.dst_addr,
1159 RTE_DIM(ipv6.mask.dst_ip));
1160 vtc_flow_val = rte_be_to_cpu_32(spec->hdr.vtc_flow);
1161 vtc_flow_mask = rte_be_to_cpu_32(mask->hdr.vtc_flow);
1162 ipv6.val.flow_label =
1163 rte_cpu_to_be_32((vtc_flow_val & IPV6_HDR_FL_MASK) >>
1165 ipv6.val.traffic_class = (vtc_flow_val & IPV6_HDR_TC_MASK) >>
1167 ipv6.val.next_hdr = spec->hdr.proto;
1168 ipv6.val.hop_limit = spec->hdr.hop_limits;
1169 ipv6.mask.flow_label =
1170 rte_cpu_to_be_32((vtc_flow_mask & IPV6_HDR_FL_MASK) >>
1172 ipv6.mask.traffic_class = (vtc_flow_mask & IPV6_HDR_TC_MASK) >>
1174 ipv6.mask.next_hdr = mask->hdr.proto;
1175 ipv6.mask.hop_limit = mask->hdr.hop_limits;
1176 /* Remove unwanted bits from values. */
1177 for (i = 0; i < RTE_DIM(ipv6.val.src_ip); ++i) {
1178 ipv6.val.src_ip[i] &= ipv6.mask.src_ip[i];
1179 ipv6.val.dst_ip[i] &= ipv6.mask.dst_ip[i];
1181 ipv6.val.flow_label &= ipv6.mask.flow_label;
1182 ipv6.val.traffic_class &= ipv6.mask.traffic_class;
1183 ipv6.val.next_hdr &= ipv6.mask.next_hdr;
1184 ipv6.val.hop_limit &= ipv6.mask.hop_limit;
1186 flow->l3_protocol_en = !!ipv6.mask.next_hdr;
1187 flow->l3_protocol = ipv6.val.next_hdr;
1188 if (size <= flow_size) {
1189 mlx5_flow_verbs_hashfields_adjust
1191 (ETH_RSS_IPV6 | ETH_RSS_NONFRAG_IPV6_OTHER),
1192 (IBV_RX_HASH_SRC_IPV6 | IBV_RX_HASH_DST_IPV6));
1193 flow->cur_verbs->attr->priority = MLX5_PRIORITY_MAP_L3;
1194 mlx5_flow_spec_verbs_add(flow, &ipv6, size);
1200 * Convert the @p item into a Verbs specification after ensuring the NIC
1201 * will understand and process it correctly.
1202 * If the necessary size for the conversion is greater than the @p flow_size,
1203 * nothing is written in @p flow, the validation is still performed.
1206 * Item specification.
1207 * @param[in, out] flow
1208 * Pointer to flow structure.
1209 * @param[in] flow_size
1210 * Size in bytes of the available space in @p flow, if too small, nothing is
1213 * Pointer to error structure.
1216 * On success the number of bytes consumed/necessary, if the returned value
1217 * is lesser or equal to @p flow_size, the @p item has fully been converted,
1218 * otherwise another call with this returned memory size should be done.
1219 * On error, a negative errno value is returned and rte_errno is set.
1222 mlx5_flow_item_udp(const struct rte_flow_item *item, struct rte_flow *flow,
1223 const size_t flow_size, struct rte_flow_error *error)
1225 const struct rte_flow_item_udp *spec = item->spec;
1226 const struct rte_flow_item_udp *mask = item->mask;
1227 const int tunnel = !!(flow->layers & MLX5_FLOW_LAYER_TUNNEL);
1228 unsigned int size = sizeof(struct ibv_flow_spec_tcp_udp);
1229 struct ibv_flow_spec_tcp_udp udp = {
1230 .type = IBV_FLOW_SPEC_UDP | (tunnel ? IBV_FLOW_SPEC_INNER : 0),
1235 if (flow->l3_protocol_en && flow->l3_protocol != MLX5_IP_PROTOCOL_UDP)
1236 return rte_flow_error_set(error, ENOTSUP,
1237 RTE_FLOW_ERROR_TYPE_ITEM,
1239 "protocol filtering not compatible"
1241 if (!(flow->layers & (tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
1242 MLX5_FLOW_LAYER_OUTER_L3)))
1243 return rte_flow_error_set(error, ENOTSUP,
1244 RTE_FLOW_ERROR_TYPE_ITEM,
1246 "L3 is mandatory to filter"
1248 if (flow->layers & (tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1249 MLX5_FLOW_LAYER_OUTER_L4))
1250 return rte_flow_error_set(error, ENOTSUP,
1251 RTE_FLOW_ERROR_TYPE_ITEM,
1253 "L4 layer is already"
1256 mask = &rte_flow_item_udp_mask;
1257 ret = mlx5_flow_item_acceptable
1258 (item, (const uint8_t *)mask,
1259 (const uint8_t *)&rte_flow_item_udp_mask,
1260 sizeof(struct rte_flow_item_udp), error);
1263 flow->layers |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
1264 MLX5_FLOW_LAYER_OUTER_L4_UDP;
1266 udp.val.dst_port = spec->hdr.dst_port;
1267 udp.val.src_port = spec->hdr.src_port;
1268 udp.mask.dst_port = mask->hdr.dst_port;
1269 udp.mask.src_port = mask->hdr.src_port;
1270 /* Remove unwanted bits from values. */
1271 udp.val.src_port &= udp.mask.src_port;
1272 udp.val.dst_port &= udp.mask.dst_port;
1274 if (size <= flow_size) {
1275 mlx5_flow_verbs_hashfields_adjust(flow, tunnel, ETH_RSS_UDP,
1276 (IBV_RX_HASH_SRC_PORT_UDP |
1277 IBV_RX_HASH_DST_PORT_UDP));
1278 flow->cur_verbs->attr->priority = MLX5_PRIORITY_MAP_L4;
1279 mlx5_flow_spec_verbs_add(flow, &udp, size);
1285 * Convert the @p item into a Verbs specification after ensuring the NIC
1286 * will understand and process it correctly.
1287 * If the necessary size for the conversion is greater than the @p flow_size,
1288 * nothing is written in @p flow, the validation is still performed.
1291 * Item specification.
1292 * @param[in, out] flow
1293 * Pointer to flow structure.
1294 * @param[in] flow_size
1295 * Size in bytes of the available space in @p flow, if too small, nothing is
1298 * Pointer to error structure.
1301 * On success the number of bytes consumed/necessary, if the returned value
1302 * is lesser or equal to @p flow_size, the @p item has fully been converted,
1303 * otherwise another call with this returned memory size should be done.
1304 * On error, a negative errno value is returned and rte_errno is set.
1307 mlx5_flow_item_tcp(const struct rte_flow_item *item, struct rte_flow *flow,
1308 const size_t flow_size, struct rte_flow_error *error)
1310 const struct rte_flow_item_tcp *spec = item->spec;
1311 const struct rte_flow_item_tcp *mask = item->mask;
1312 const int tunnel = !!(flow->layers & MLX5_FLOW_LAYER_TUNNEL);
1313 unsigned int size = sizeof(struct ibv_flow_spec_tcp_udp);
1314 struct ibv_flow_spec_tcp_udp tcp = {
1315 .type = IBV_FLOW_SPEC_TCP | (tunnel ? IBV_FLOW_SPEC_INNER : 0),
1320 if (flow->l3_protocol_en && flow->l3_protocol != MLX5_IP_PROTOCOL_TCP)
1321 return rte_flow_error_set(error, ENOTSUP,
1322 RTE_FLOW_ERROR_TYPE_ITEM,
1324 "protocol filtering not compatible"
1326 if (!(flow->layers & (tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
1327 MLX5_FLOW_LAYER_OUTER_L3)))
1328 return rte_flow_error_set(error, ENOTSUP,
1329 RTE_FLOW_ERROR_TYPE_ITEM,
1331 "L3 is mandatory to filter on L4");
1332 if (flow->layers & (tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1333 MLX5_FLOW_LAYER_OUTER_L4))
1334 return rte_flow_error_set(error, ENOTSUP,
1335 RTE_FLOW_ERROR_TYPE_ITEM,
1337 "L4 layer is already present");
1339 mask = &rte_flow_item_tcp_mask;
1340 ret = mlx5_flow_item_acceptable
1341 (item, (const uint8_t *)mask,
1342 (const uint8_t *)&rte_flow_item_tcp_mask,
1343 sizeof(struct rte_flow_item_tcp), error);
1346 flow->layers |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
1347 MLX5_FLOW_LAYER_OUTER_L4_TCP;
1349 tcp.val.dst_port = spec->hdr.dst_port;
1350 tcp.val.src_port = spec->hdr.src_port;
1351 tcp.mask.dst_port = mask->hdr.dst_port;
1352 tcp.mask.src_port = mask->hdr.src_port;
1353 /* Remove unwanted bits from values. */
1354 tcp.val.src_port &= tcp.mask.src_port;
1355 tcp.val.dst_port &= tcp.mask.dst_port;
1357 if (size <= flow_size) {
1358 mlx5_flow_verbs_hashfields_adjust(flow, tunnel, ETH_RSS_TCP,
1359 (IBV_RX_HASH_SRC_PORT_TCP |
1360 IBV_RX_HASH_DST_PORT_TCP));
1361 flow->cur_verbs->attr->priority = MLX5_PRIORITY_MAP_L4;
1362 mlx5_flow_spec_verbs_add(flow, &tcp, size);
1368 * Convert the @p item into a Verbs specification after ensuring the NIC
1369 * will understand and process it correctly.
1370 * If the necessary size for the conversion is greater than the @p flow_size,
1371 * nothing is written in @p flow, the validation is still performed.
1374 * Item specification.
1375 * @param[in, out] flow
1376 * Pointer to flow structure.
1377 * @param[in] flow_size
1378 * Size in bytes of the available space in @p flow, if too small, nothing is
1381 * Pointer to error structure.
1384 * On success the number of bytes consumed/necessary, if the returned value
1385 * is lesser or equal to @p flow_size, the @p item has fully been converted,
1386 * otherwise another call with this returned memory size should be done.
1387 * On error, a negative errno value is returned and rte_errno is set.
1390 mlx5_flow_item_vxlan(const struct rte_flow_item *item, struct rte_flow *flow,
1391 const size_t flow_size, struct rte_flow_error *error)
1393 const struct rte_flow_item_vxlan *spec = item->spec;
1394 const struct rte_flow_item_vxlan *mask = item->mask;
1395 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
1396 struct ibv_flow_spec_tunnel vxlan = {
1397 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
1404 } id = { .vlan_id = 0, };
1406 if (flow->layers & MLX5_FLOW_LAYER_TUNNEL)
1407 return rte_flow_error_set(error, ENOTSUP,
1408 RTE_FLOW_ERROR_TYPE_ITEM,
1410 "a tunnel is already present");
1412 * Verify only UDPv4 is present as defined in
1413 * https://tools.ietf.org/html/rfc7348
1415 if (!(flow->layers & MLX5_FLOW_LAYER_OUTER_L4_UDP))
1416 return rte_flow_error_set(error, ENOTSUP,
1417 RTE_FLOW_ERROR_TYPE_ITEM,
1419 "no outer UDP layer found");
1421 mask = &rte_flow_item_vxlan_mask;
1422 ret = mlx5_flow_item_acceptable
1423 (item, (const uint8_t *)mask,
1424 (const uint8_t *)&rte_flow_item_vxlan_mask,
1425 sizeof(struct rte_flow_item_vxlan), error);
1429 memcpy(&id.vni[1], spec->vni, 3);
1430 vxlan.val.tunnel_id = id.vlan_id;
1431 memcpy(&id.vni[1], mask->vni, 3);
1432 vxlan.mask.tunnel_id = id.vlan_id;
1433 /* Remove unwanted bits from values. */
1434 vxlan.val.tunnel_id &= vxlan.mask.tunnel_id;
1437 * Tunnel id 0 is equivalent as not adding a VXLAN layer, if
1438 * only this layer is defined in the Verbs specification it is
1439 * interpreted as wildcard and all packets will match this
1440 * rule, if it follows a full stack layer (ex: eth / ipv4 /
1441 * udp), all packets matching the layers before will also
1442 * match this rule. To avoid such situation, VNI 0 is
1443 * currently refused.
1445 if (!vxlan.val.tunnel_id)
1446 return rte_flow_error_set(error, EINVAL,
1447 RTE_FLOW_ERROR_TYPE_ITEM,
1449 "VXLAN vni cannot be 0");
1450 if (!(flow->layers & MLX5_FLOW_LAYER_OUTER))
1451 return rte_flow_error_set(error, EINVAL,
1452 RTE_FLOW_ERROR_TYPE_ITEM,
1454 "VXLAN tunnel must be fully defined");
1455 if (size <= flow_size) {
1456 mlx5_flow_spec_verbs_add(flow, &vxlan, size);
1457 flow->cur_verbs->attr->priority = MLX5_PRIORITY_MAP_L2;
1459 flow->layers |= MLX5_FLOW_LAYER_VXLAN;
1464 * Convert the @p item into a Verbs specification after ensuring the NIC
1465 * will understand and process it correctly.
1466 * If the necessary size for the conversion is greater than the @p flow_size,
1467 * nothing is written in @p flow, the validation is still performed.
1470 * Pointer to Ethernet device.
1472 * Item specification.
1473 * @param[in, out] flow
1474 * Pointer to flow structure.
1475 * @param[in] flow_size
1476 * Size in bytes of the available space in @p flow, if too small, nothing is
1479 * Pointer to error structure.
1482 * On success the number of bytes consumed/necessary, if the returned value
1483 * is lesser or equal to @p flow_size, the @p item has fully been converted,
1484 * otherwise another call with this returned memory size should be done.
1485 * On error, a negative errno value is returned and rte_errno is set.
1488 mlx5_flow_item_vxlan_gpe(struct rte_eth_dev *dev,
1489 const struct rte_flow_item *item,
1490 struct rte_flow *flow, const size_t flow_size,
1491 struct rte_flow_error *error)
1493 const struct rte_flow_item_vxlan_gpe *spec = item->spec;
1494 const struct rte_flow_item_vxlan_gpe *mask = item->mask;
1495 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
1496 struct ibv_flow_spec_tunnel vxlan_gpe = {
1497 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
1504 } id = { .vlan_id = 0, };
1506 if (!((struct priv *)dev->data->dev_private)->config.l3_vxlan_en)
1507 return rte_flow_error_set(error, ENOTSUP,
1508 RTE_FLOW_ERROR_TYPE_ITEM,
1510 "L3 VXLAN is not enabled by device"
1511 " parameter and/or not configured in"
1513 if (flow->layers & MLX5_FLOW_LAYER_TUNNEL)
1514 return rte_flow_error_set(error, ENOTSUP,
1515 RTE_FLOW_ERROR_TYPE_ITEM,
1517 "a tunnel is already present");
1519 * Verify only UDPv4 is present as defined in
1520 * https://tools.ietf.org/html/rfc7348
1522 if (!(flow->layers & MLX5_FLOW_LAYER_OUTER_L4_UDP))
1523 return rte_flow_error_set(error, ENOTSUP,
1524 RTE_FLOW_ERROR_TYPE_ITEM,
1526 "no outer UDP layer found");
1528 mask = &rte_flow_item_vxlan_gpe_mask;
1529 ret = mlx5_flow_item_acceptable
1530 (item, (const uint8_t *)mask,
1531 (const uint8_t *)&rte_flow_item_vxlan_gpe_mask,
1532 sizeof(struct rte_flow_item_vxlan_gpe), error);
1536 memcpy(&id.vni[1], spec->vni, 3);
1537 vxlan_gpe.val.tunnel_id = id.vlan_id;
1538 memcpy(&id.vni[1], mask->vni, 3);
1539 vxlan_gpe.mask.tunnel_id = id.vlan_id;
1541 return rte_flow_error_set
1543 RTE_FLOW_ERROR_TYPE_ITEM,
1545 "VxLAN-GPE protocol not supported");
1546 /* Remove unwanted bits from values. */
1547 vxlan_gpe.val.tunnel_id &= vxlan_gpe.mask.tunnel_id;
1550 * Tunnel id 0 is equivalent as not adding a VXLAN layer, if only this
1551 * layer is defined in the Verbs specification it is interpreted as
1552 * wildcard and all packets will match this rule, if it follows a full
1553 * stack layer (ex: eth / ipv4 / udp), all packets matching the layers
1554 * before will also match this rule. To avoid such situation, VNI 0
1555 * is currently refused.
1557 if (!vxlan_gpe.val.tunnel_id)
1558 return rte_flow_error_set(error, EINVAL,
1559 RTE_FLOW_ERROR_TYPE_ITEM,
1561 "VXLAN-GPE vni cannot be 0");
1562 if (!(flow->layers & MLX5_FLOW_LAYER_OUTER))
1563 return rte_flow_error_set(error, EINVAL,
1564 RTE_FLOW_ERROR_TYPE_ITEM,
1566 "VXLAN-GPE tunnel must be fully"
1568 if (size <= flow_size) {
1569 mlx5_flow_spec_verbs_add(flow, &vxlan_gpe, size);
1570 flow->cur_verbs->attr->priority = MLX5_PRIORITY_MAP_L2;
1572 flow->layers |= MLX5_FLOW_LAYER_VXLAN_GPE;
1577 * Update the protocol in Verbs IPv4/IPv6 spec.
1579 * @param[in, out] attr
1580 * Pointer to Verbs attributes structure.
1582 * Specification type to search in order to update the IP protocol.
1583 * @param[in] protocol
1584 * Protocol value to set if none is present in the specification.
1587 mlx5_flow_item_gre_ip_protocol_update(struct ibv_flow_attr *attr,
1588 enum ibv_flow_spec_type search,
1592 struct ibv_spec_header *hdr = (struct ibv_spec_header *)
1593 ((uint8_t *)attr + sizeof(struct ibv_flow_attr));
1597 for (i = 0; i != attr->num_of_specs; ++i) {
1598 if (hdr->type == search) {
1600 struct ibv_flow_spec_ipv4_ext *ipv4;
1601 struct ibv_flow_spec_ipv6 *ipv6;
1605 case IBV_FLOW_SPEC_IPV4_EXT:
1606 ip.ipv4 = (struct ibv_flow_spec_ipv4_ext *)hdr;
1607 if (!ip.ipv4->val.proto) {
1608 ip.ipv4->val.proto = protocol;
1609 ip.ipv4->mask.proto = 0xff;
1612 case IBV_FLOW_SPEC_IPV6:
1613 ip.ipv6 = (struct ibv_flow_spec_ipv6 *)hdr;
1614 if (!ip.ipv6->val.next_hdr) {
1615 ip.ipv6->val.next_hdr = protocol;
1616 ip.ipv6->mask.next_hdr = 0xff;
1624 hdr = (struct ibv_spec_header *)((uint8_t *)hdr + hdr->size);
1629 * Convert the @p item into a Verbs specification after ensuring the NIC
1630 * will understand and process it correctly.
1631 * It will also update the previous L3 layer with the protocol value matching
1633 * If the necessary size for the conversion is greater than the @p flow_size,
1634 * nothing is written in @p flow, the validation is still performed.
1637 * Pointer to Ethernet device.
1639 * Item specification.
1640 * @param[in, out] flow
1641 * Pointer to flow structure.
1642 * @param[in] flow_size
1643 * Size in bytes of the available space in @p flow, if too small, nothing is
1646 * Pointer to error structure.
1649 * On success the number of bytes consumed/necessary, if the returned value
1650 * is lesser or equal to @p flow_size, the @p item has fully been converted,
1651 * otherwise another call with this returned memory size should be done.
1652 * On error, a negative errno value is returned and rte_errno is set.
1655 mlx5_flow_item_gre(const struct rte_flow_item *item,
1656 struct rte_flow *flow, const size_t flow_size,
1657 struct rte_flow_error *error)
1659 struct mlx5_flow_verbs *verbs = flow->cur_verbs;
1660 const struct rte_flow_item_gre *spec = item->spec;
1661 const struct rte_flow_item_gre *mask = item->mask;
1662 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
1663 unsigned int size = sizeof(struct ibv_flow_spec_gre);
1664 struct ibv_flow_spec_gre tunnel = {
1665 .type = IBV_FLOW_SPEC_GRE,
1669 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
1670 struct ibv_flow_spec_tunnel tunnel = {
1671 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
1677 if (flow->l3_protocol_en && flow->l3_protocol != MLX5_IP_PROTOCOL_GRE)
1678 return rte_flow_error_set(error, ENOTSUP,
1679 RTE_FLOW_ERROR_TYPE_ITEM,
1681 "protocol filtering not compatible"
1682 " with this GRE layer");
1683 if (flow->layers & MLX5_FLOW_LAYER_TUNNEL)
1684 return rte_flow_error_set(error, ENOTSUP,
1685 RTE_FLOW_ERROR_TYPE_ITEM,
1687 "a tunnel is already present");
1688 if (!(flow->layers & MLX5_FLOW_LAYER_OUTER_L3))
1689 return rte_flow_error_set(error, ENOTSUP,
1690 RTE_FLOW_ERROR_TYPE_ITEM,
1692 "L3 Layer is missing");
1694 mask = &rte_flow_item_gre_mask;
1695 ret = mlx5_flow_item_acceptable
1696 (item, (const uint8_t *)mask,
1697 (const uint8_t *)&rte_flow_item_gre_mask,
1698 sizeof(struct rte_flow_item_gre), error);
1701 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
1703 tunnel.val.c_ks_res0_ver = spec->c_rsvd0_ver;
1704 tunnel.val.protocol = spec->protocol;
1705 tunnel.mask.c_ks_res0_ver = mask->c_rsvd0_ver;
1706 tunnel.mask.protocol = mask->protocol;
1707 /* Remove unwanted bits from values. */
1708 tunnel.val.c_ks_res0_ver &= tunnel.mask.c_ks_res0_ver;
1709 tunnel.val.protocol &= tunnel.mask.protocol;
1710 tunnel.val.key &= tunnel.mask.key;
1713 if (spec && (spec->protocol & mask->protocol))
1714 return rte_flow_error_set(error, ENOTSUP,
1715 RTE_FLOW_ERROR_TYPE_ITEM,
1717 "without MPLS support the"
1718 " specification cannot be used for"
1720 #endif /* !HAVE_IBV_DEVICE_MPLS_SUPPORT */
1721 if (size <= flow_size) {
1722 if (flow->layers & MLX5_FLOW_LAYER_OUTER_L3_IPV4)
1723 mlx5_flow_item_gre_ip_protocol_update
1724 (verbs->attr, IBV_FLOW_SPEC_IPV4_EXT,
1725 MLX5_IP_PROTOCOL_GRE);
1727 mlx5_flow_item_gre_ip_protocol_update
1728 (verbs->attr, IBV_FLOW_SPEC_IPV6,
1729 MLX5_IP_PROTOCOL_GRE);
1730 mlx5_flow_spec_verbs_add(flow, &tunnel, size);
1731 flow->cur_verbs->attr->priority = MLX5_PRIORITY_MAP_L2;
1733 flow->layers |= MLX5_FLOW_LAYER_GRE;
1738 * Convert the @p item into a Verbs specification after ensuring the NIC
1739 * will understand and process it correctly.
1740 * If the necessary size for the conversion is greater than the @p flow_size,
1741 * nothing is written in @p flow, the validation is still performed.
1744 * Item specification.
1745 * @param[in, out] flow
1746 * Pointer to flow structure.
1747 * @param[in] flow_size
1748 * Size in bytes of the available space in @p flow, if too small, nothing is
1751 * Pointer to error structure.
1754 * On success the number of bytes consumed/necessary, if the returned value
1755 * is lesser or equal to @p flow_size, the @p item has fully been converted,
1756 * otherwise another call with this returned memory size should be done.
1757 * On error, a negative errno value is returned and rte_errno is set.
1760 mlx5_flow_item_mpls(const struct rte_flow_item *item __rte_unused,
1761 struct rte_flow *flow __rte_unused,
1762 const size_t flow_size __rte_unused,
1763 struct rte_flow_error *error)
1765 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
1766 const struct rte_flow_item_mpls *spec = item->spec;
1767 const struct rte_flow_item_mpls *mask = item->mask;
1768 unsigned int size = sizeof(struct ibv_flow_spec_mpls);
1769 struct ibv_flow_spec_mpls mpls = {
1770 .type = IBV_FLOW_SPEC_MPLS,
1775 if (flow->l3_protocol_en && flow->l3_protocol != MLX5_IP_PROTOCOL_MPLS)
1776 return rte_flow_error_set(error, ENOTSUP,
1777 RTE_FLOW_ERROR_TYPE_ITEM,
1779 "protocol filtering not compatible"
1780 " with MPLS layer");
1781 if (flow->layers & MLX5_FLOW_LAYER_TUNNEL)
1782 return rte_flow_error_set(error, ENOTSUP,
1783 RTE_FLOW_ERROR_TYPE_ITEM,
1785 "a tunnel is already"
1788 mask = &rte_flow_item_mpls_mask;
1789 ret = mlx5_flow_item_acceptable
1790 (item, (const uint8_t *)mask,
1791 (const uint8_t *)&rte_flow_item_mpls_mask,
1792 sizeof(struct rte_flow_item_mpls), error);
1796 memcpy(&mpls.val.label, spec, sizeof(mpls.val.label));
1797 memcpy(&mpls.mask.label, mask, sizeof(mpls.mask.label));
1798 /* Remove unwanted bits from values. */
1799 mpls.val.label &= mpls.mask.label;
1801 if (size <= flow_size) {
1802 mlx5_flow_spec_verbs_add(flow, &mpls, size);
1803 flow->cur_verbs->attr->priority = MLX5_PRIORITY_MAP_L2;
1805 flow->layers |= MLX5_FLOW_LAYER_MPLS;
1807 #endif /* !HAVE_IBV_DEVICE_MPLS_SUPPORT */
1808 return rte_flow_error_set(error, ENOTSUP,
1809 RTE_FLOW_ERROR_TYPE_ITEM,
1811 "MPLS is not supported by Verbs, please"
1816 * Convert the @p pattern into a Verbs specifications after ensuring the NIC
1817 * will understand and process it correctly.
1818 * The conversion is performed item per item, each of them is written into
1819 * the @p flow if its size is lesser or equal to @p flow_size.
1820 * Validation and memory consumption computation are still performed until the
1821 * end of @p pattern, unless an error is encountered.
1823 * @param[in] pattern
1825 * @param[in, out] flow
1826 * Pointer to the rte_flow structure.
1827 * @param[in] flow_size
1828 * Size in bytes of the available space in @p flow, if too small some
1829 * garbage may be present.
1831 * Pointer to error structure.
1834 * On success the number of bytes consumed/necessary, if the returned value
1835 * is lesser or equal to @p flow_size, the @pattern has fully been
1836 * converted, otherwise another call with this returned memory size should
1838 * On error, a negative errno value is returned and rte_errno is set.
1841 mlx5_flow_items(struct rte_eth_dev *dev,
1842 const struct rte_flow_item pattern[],
1843 struct rte_flow *flow, const size_t flow_size,
1844 struct rte_flow_error *error)
1846 int remain = flow_size;
1849 for (; pattern->type != RTE_FLOW_ITEM_TYPE_END; pattern++) {
1852 switch (pattern->type) {
1853 case RTE_FLOW_ITEM_TYPE_VOID:
1855 case RTE_FLOW_ITEM_TYPE_ETH:
1856 ret = mlx5_flow_item_eth(pattern, flow, remain, error);
1858 case RTE_FLOW_ITEM_TYPE_VLAN:
1859 ret = mlx5_flow_item_vlan(pattern, flow, remain, error);
1861 case RTE_FLOW_ITEM_TYPE_IPV4:
1862 ret = mlx5_flow_item_ipv4(pattern, flow, remain, error);
1864 case RTE_FLOW_ITEM_TYPE_IPV6:
1865 ret = mlx5_flow_item_ipv6(pattern, flow, remain, error);
1867 case RTE_FLOW_ITEM_TYPE_UDP:
1868 ret = mlx5_flow_item_udp(pattern, flow, remain, error);
1870 case RTE_FLOW_ITEM_TYPE_TCP:
1871 ret = mlx5_flow_item_tcp(pattern, flow, remain, error);
1873 case RTE_FLOW_ITEM_TYPE_VXLAN:
1874 ret = mlx5_flow_item_vxlan(pattern, flow, remain,
1877 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1878 ret = mlx5_flow_item_vxlan_gpe(dev, pattern, flow,
1881 case RTE_FLOW_ITEM_TYPE_GRE:
1882 ret = mlx5_flow_item_gre(pattern, flow, remain, error);
1884 case RTE_FLOW_ITEM_TYPE_MPLS:
1885 ret = mlx5_flow_item_mpls(pattern, flow, remain, error);
1888 return rte_flow_error_set(error, ENOTSUP,
1889 RTE_FLOW_ERROR_TYPE_ITEM,
1891 "item not supported");
1901 if (!flow->layers) {
1902 const struct rte_flow_item item = {
1903 .type = RTE_FLOW_ITEM_TYPE_ETH,
1906 return mlx5_flow_item_eth(&item, flow, flow_size, error);
1912 * Convert the @p action into a Verbs specification after ensuring the NIC
1913 * will understand and process it correctly.
1914 * If the necessary size for the conversion is greater than the @p flow_size,
1915 * nothing is written in @p flow, the validation is still performed.
1918 * Action configuration.
1919 * @param[in, out] flow
1920 * Pointer to flow structure.
1921 * @param[in] flow_size
1922 * Size in bytes of the available space in @p flow, if too small, nothing is
1925 * Pointer to error structure.
1928 * On success the number of bytes consumed/necessary, if the returned value
1929 * is lesser or equal to @p flow_size, the @p action has fully been
1930 * converted, otherwise another call with this returned memory size should
1932 * On error, a negative errno value is returned and rte_errno is set.
1935 mlx5_flow_action_drop(const struct rte_flow_action *action,
1936 struct rte_flow *flow, const size_t flow_size,
1937 struct rte_flow_error *error)
1939 unsigned int size = sizeof(struct ibv_flow_spec_action_drop);
1940 struct ibv_flow_spec_action_drop drop = {
1941 .type = IBV_FLOW_SPEC_ACTION_DROP,
1946 return rte_flow_error_set(error, ENOTSUP,
1947 RTE_FLOW_ERROR_TYPE_ACTION,
1949 "multiple fate actions are not"
1951 if (flow->modifier & (MLX5_FLOW_MOD_FLAG | MLX5_FLOW_MOD_MARK))
1952 return rte_flow_error_set(error, ENOTSUP,
1953 RTE_FLOW_ERROR_TYPE_ACTION,
1955 "drop is not compatible with"
1956 " flag/mark action");
1957 if (size < flow_size)
1958 mlx5_flow_spec_verbs_add(flow, &drop, size);
1959 flow->fate |= MLX5_FLOW_FATE_DROP;
1964 * Convert the @p action into @p flow after ensuring the NIC will understand
1965 * and process it correctly.
1968 * Pointer to Ethernet device structure.
1970 * Action configuration.
1971 * @param[in, out] flow
1972 * Pointer to flow structure.
1974 * Pointer to error structure.
1977 * 0 on success, a negative errno value otherwise and rte_errno is set.
1980 mlx5_flow_action_queue(struct rte_eth_dev *dev,
1981 const struct rte_flow_action *action,
1982 struct rte_flow *flow,
1983 struct rte_flow_error *error)
1985 struct priv *priv = dev->data->dev_private;
1986 const struct rte_flow_action_queue *queue = action->conf;
1989 return rte_flow_error_set(error, ENOTSUP,
1990 RTE_FLOW_ERROR_TYPE_ACTION,
1992 "multiple fate actions are not"
1994 if (queue->index >= priv->rxqs_n)
1995 return rte_flow_error_set(error, EINVAL,
1996 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1998 "queue index out of range");
1999 if (!(*priv->rxqs)[queue->index])
2000 return rte_flow_error_set(error, EINVAL,
2001 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2003 "queue is not configured");
2005 (*flow->queue)[0] = queue->index;
2006 flow->rss.queue_num = 1;
2007 flow->fate |= MLX5_FLOW_FATE_QUEUE;
2012 * Ensure the @p action will be understood and used correctly by the NIC.
2015 * Pointer to Ethernet device structure.
2017 * Pointer to flow actions array.
2018 * @param flow[in, out]
2019 * Pointer to the rte_flow structure.
2020 * @param error[in, out]
2021 * Pointer to error structure.
2024 * On success @p flow->queue array and @p flow->rss are filled and valid.
2025 * On error, a negative errno value is returned and rte_errno is set.
2028 mlx5_flow_action_rss(struct rte_eth_dev *dev,
2029 const struct rte_flow_action *action,
2030 struct rte_flow *flow,
2031 struct rte_flow_error *error)
2033 struct priv *priv = dev->data->dev_private;
2034 const struct rte_flow_action_rss *rss = action->conf;
2038 return rte_flow_error_set(error, ENOTSUP,
2039 RTE_FLOW_ERROR_TYPE_ACTION,
2041 "multiple fate actions are not"
2043 if (rss->func != RTE_ETH_HASH_FUNCTION_DEFAULT &&
2044 rss->func != RTE_ETH_HASH_FUNCTION_TOEPLITZ)
2045 return rte_flow_error_set(error, ENOTSUP,
2046 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2048 "RSS hash function not supported");
2049 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
2054 return rte_flow_error_set(error, ENOTSUP,
2055 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2057 "tunnel RSS is not supported");
2058 if (rss->key_len < MLX5_RSS_HASH_KEY_LEN)
2059 return rte_flow_error_set(error, ENOTSUP,
2060 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2062 "RSS hash key too small");
2063 if (rss->key_len > MLX5_RSS_HASH_KEY_LEN)
2064 return rte_flow_error_set(error, ENOTSUP,
2065 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2067 "RSS hash key too large");
2068 if (rss->queue_num > priv->config.ind_table_max_size)
2069 return rte_flow_error_set(error, ENOTSUP,
2070 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2072 "number of queues too large");
2073 if (rss->types & MLX5_RSS_HF_MASK)
2074 return rte_flow_error_set(error, ENOTSUP,
2075 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2077 "some RSS protocols are not"
2079 for (i = 0; i != rss->queue_num; ++i) {
2080 if (!(*priv->rxqs)[rss->queue[i]])
2081 return rte_flow_error_set
2083 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2085 "queue is not configured");
2088 memcpy((*flow->queue), rss->queue,
2089 rss->queue_num * sizeof(uint16_t));
2090 flow->rss.queue_num = rss->queue_num;
2091 memcpy(flow->key, rss->key, MLX5_RSS_HASH_KEY_LEN);
2092 flow->rss.types = rss->types;
2093 flow->rss.level = rss->level;
2094 flow->fate |= MLX5_FLOW_FATE_RSS;
2099 * Convert the @p action into a Verbs specification after ensuring the NIC
2100 * will understand and process it correctly.
2101 * If the necessary size for the conversion is greater than the @p flow_size,
2102 * nothing is written in @p flow, the validation is still performed.
2105 * Action configuration.
2106 * @param[in, out] flow
2107 * Pointer to flow structure.
2108 * @param[in] flow_size
2109 * Size in bytes of the available space in @p flow, if too small, nothing is
2112 * Pointer to error structure.
2115 * On success the number of bytes consumed/necessary, if the returned value
2116 * is lesser or equal to @p flow_size, the @p action has fully been
2117 * converted, otherwise another call with this returned memory size should
2119 * On error, a negative errno value is returned and rte_errno is set.
2122 mlx5_flow_action_flag(const struct rte_flow_action *action,
2123 struct rte_flow *flow, const size_t flow_size,
2124 struct rte_flow_error *error)
2126 unsigned int size = sizeof(struct ibv_flow_spec_action_tag);
2127 struct ibv_flow_spec_action_tag tag = {
2128 .type = IBV_FLOW_SPEC_ACTION_TAG,
2130 .tag_id = mlx5_flow_mark_set(MLX5_FLOW_MARK_DEFAULT),
2132 struct mlx5_flow_verbs *verbs = flow->cur_verbs;
2134 if (flow->modifier & MLX5_FLOW_MOD_FLAG)
2135 return rte_flow_error_set(error, ENOTSUP,
2136 RTE_FLOW_ERROR_TYPE_ACTION,
2138 "flag action already present");
2139 if (flow->fate & MLX5_FLOW_FATE_DROP)
2140 return rte_flow_error_set(error, ENOTSUP,
2141 RTE_FLOW_ERROR_TYPE_ACTION,
2143 "flag is not compatible with drop"
2145 if (flow->modifier & MLX5_FLOW_MOD_MARK)
2147 else if (size <= flow_size && verbs)
2148 mlx5_flow_spec_verbs_add(flow, &tag, size);
2149 flow->modifier |= MLX5_FLOW_MOD_FLAG;
2154 * Update verbs specification to modify the flag to mark.
2156 * @param[in, out] verbs
2157 * Pointer to the mlx5_flow_verbs structure.
2158 * @param[in] mark_id
2159 * Mark identifier to replace the flag.
2162 mlx5_flow_verbs_mark_update(struct mlx5_flow_verbs *verbs, uint32_t mark_id)
2164 struct ibv_spec_header *hdr;
2169 /* Update Verbs specification. */
2170 hdr = (struct ibv_spec_header *)verbs->specs;
2173 for (i = 0; i != verbs->attr->num_of_specs; ++i) {
2174 if (hdr->type == IBV_FLOW_SPEC_ACTION_TAG) {
2175 struct ibv_flow_spec_action_tag *t =
2176 (struct ibv_flow_spec_action_tag *)hdr;
2178 t->tag_id = mlx5_flow_mark_set(mark_id);
2180 hdr = (struct ibv_spec_header *)((uintptr_t)hdr + hdr->size);
2185 * Convert the @p action into @p flow (or by updating the already present
2186 * Flag Verbs specification) after ensuring the NIC will understand and
2187 * process it correctly.
2188 * If the necessary size for the conversion is greater than the @p flow_size,
2189 * nothing is written in @p flow, the validation is still performed.
2192 * Action configuration.
2193 * @param[in, out] flow
2194 * Pointer to flow structure.
2195 * @param[in] flow_size
2196 * Size in bytes of the available space in @p flow, if too small, nothing is
2199 * Pointer to error structure.
2202 * On success the number of bytes consumed/necessary, if the returned value
2203 * is lesser or equal to @p flow_size, the @p action has fully been
2204 * converted, otherwise another call with this returned memory size should
2206 * On error, a negative errno value is returned and rte_errno is set.
2209 mlx5_flow_action_mark(const struct rte_flow_action *action,
2210 struct rte_flow *flow, const size_t flow_size,
2211 struct rte_flow_error *error)
2213 const struct rte_flow_action_mark *mark = action->conf;
2214 unsigned int size = sizeof(struct ibv_flow_spec_action_tag);
2215 struct ibv_flow_spec_action_tag tag = {
2216 .type = IBV_FLOW_SPEC_ACTION_TAG,
2219 struct mlx5_flow_verbs *verbs = flow->cur_verbs;
2222 return rte_flow_error_set(error, EINVAL,
2223 RTE_FLOW_ERROR_TYPE_ACTION,
2225 "configuration cannot be null");
2226 if (mark->id >= MLX5_FLOW_MARK_MAX)
2227 return rte_flow_error_set(error, EINVAL,
2228 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2230 "mark id must in 0 <= id < "
2231 RTE_STR(MLX5_FLOW_MARK_MAX));
2232 if (flow->modifier & MLX5_FLOW_MOD_MARK)
2233 return rte_flow_error_set(error, ENOTSUP,
2234 RTE_FLOW_ERROR_TYPE_ACTION,
2236 "mark action already present");
2237 if (flow->fate & MLX5_FLOW_FATE_DROP)
2238 return rte_flow_error_set(error, ENOTSUP,
2239 RTE_FLOW_ERROR_TYPE_ACTION,
2241 "mark is not compatible with drop"
2243 if (flow->modifier & MLX5_FLOW_MOD_FLAG) {
2244 mlx5_flow_verbs_mark_update(verbs, mark->id);
2246 } else if (size <= flow_size) {
2247 tag.tag_id = mlx5_flow_mark_set(mark->id);
2248 mlx5_flow_spec_verbs_add(flow, &tag, size);
2250 flow->modifier |= MLX5_FLOW_MOD_MARK;
2255 * Convert the @p action into a Verbs specification after ensuring the NIC
2256 * will understand and process it correctly.
2257 * If the necessary size for the conversion is greater than the @p flow_size,
2258 * nothing is written in @p flow, the validation is still performed.
2261 * Action configuration.
2262 * @param flow[in, out]
2263 * Pointer to flow structure.
2264 * @param flow_size[in]
2265 * Size in bytes of the available space in @p flow, if too small, nothing is
2267 * @param error[int, out]
2268 * Pointer to error structure.
2271 * On success the number of bytes consumed/necessary, if the returned value
2272 * is lesser or equal to @p flow_size, the @p action has fully been
2273 * converted, otherwise another call with this returned memory size should
2275 * On error, a negative errno value is returned and rte_errno is set.
2278 mlx5_flow_action_count(struct rte_eth_dev *dev,
2279 const struct rte_flow_action *action,
2280 struct rte_flow *flow,
2281 const size_t flow_size __rte_unused,
2282 struct rte_flow_error *error)
2284 const struct rte_flow_action_count *count = action->conf;
2285 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
2286 unsigned int size = sizeof(struct ibv_flow_spec_counter_action);
2287 struct ibv_flow_spec_counter_action counter = {
2288 .type = IBV_FLOW_SPEC_ACTION_COUNT,
2293 if (!flow->counter) {
2294 flow->counter = mlx5_flow_counter_new(dev, count->shared,
2297 return rte_flow_error_set(error, ENOTSUP,
2298 RTE_FLOW_ERROR_TYPE_ACTION,
2300 "cannot get counter"
2303 if (!((struct priv *)dev->data->dev_private)->config.flow_counter_en)
2304 return rte_flow_error_set(error, ENOTSUP,
2305 RTE_FLOW_ERROR_TYPE_ACTION,
2307 "flow counters are not supported.");
2308 flow->modifier |= MLX5_FLOW_MOD_COUNT;
2309 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
2310 counter.counter_set_handle = flow->counter->cs->handle;
2311 if (size <= flow_size)
2312 mlx5_flow_spec_verbs_add(flow, &counter, size);
2319 * Convert the @p action into @p flow after ensuring the NIC will understand
2320 * and process it correctly.
2321 * The conversion is performed action per action, each of them is written into
2322 * the @p flow if its size is lesser or equal to @p flow_size.
2323 * Validation and memory consumption computation are still performed until the
2324 * end of @p action, unless an error is encountered.
2327 * Pointer to Ethernet device structure.
2328 * @param[in] actions
2329 * Pointer to flow actions array.
2330 * @param[in, out] flow
2331 * Pointer to the rte_flow structure.
2332 * @param[in] flow_size
2333 * Size in bytes of the available space in @p flow, if too small some
2334 * garbage may be present.
2336 * Pointer to error structure.
2339 * On success the number of bytes consumed/necessary, if the returned value
2340 * is lesser or equal to @p flow_size, the @p actions has fully been
2341 * converted, otherwise another call with this returned memory size should
2343 * On error, a negative errno value is returned and rte_errno is set.
2346 mlx5_flow_actions(struct rte_eth_dev *dev,
2347 const struct rte_flow_action actions[],
2348 struct rte_flow *flow, const size_t flow_size,
2349 struct rte_flow_error *error)
2352 int remain = flow_size;
2355 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2356 switch (actions->type) {
2357 case RTE_FLOW_ACTION_TYPE_VOID:
2359 case RTE_FLOW_ACTION_TYPE_FLAG:
2360 ret = mlx5_flow_action_flag(actions, flow, remain,
2363 case RTE_FLOW_ACTION_TYPE_MARK:
2364 ret = mlx5_flow_action_mark(actions, flow, remain,
2367 case RTE_FLOW_ACTION_TYPE_DROP:
2368 ret = mlx5_flow_action_drop(actions, flow, remain,
2371 case RTE_FLOW_ACTION_TYPE_QUEUE:
2372 ret = mlx5_flow_action_queue(dev, actions, flow, error);
2374 case RTE_FLOW_ACTION_TYPE_RSS:
2375 ret = mlx5_flow_action_rss(dev, actions, flow, error);
2377 case RTE_FLOW_ACTION_TYPE_COUNT:
2378 ret = mlx5_flow_action_count(dev, actions, flow, remain,
2382 return rte_flow_error_set(error, ENOTSUP,
2383 RTE_FLOW_ERROR_TYPE_ACTION,
2385 "action not supported");
2396 return rte_flow_error_set(error, ENOTSUP,
2397 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2399 "no fate action found");
2404 * Validate flow rule and fill flow structure accordingly.
2407 * Pointer to Ethernet device.
2409 * Pointer to flow structure.
2411 * Size of allocated space for @p flow.
2413 * Flow rule attributes.
2414 * @param[in] pattern
2415 * Pattern specification (list terminated by the END pattern item).
2416 * @param[in] actions
2417 * Associated actions (list terminated by the END action).
2419 * Perform verbose error reporting if not NULL.
2422 * A positive value representing the size of the flow object in bytes
2423 * regardless of @p flow_size on success, a negative errno value otherwise
2424 * and rte_errno is set.
2427 mlx5_flow_merge_switch(struct rte_eth_dev *dev,
2428 struct rte_flow *flow,
2430 const struct rte_flow_attr *attr,
2431 const struct rte_flow_item pattern[],
2432 const struct rte_flow_action actions[],
2433 struct rte_flow_error *error)
2435 unsigned int n = mlx5_dev_to_port_id(dev->device, NULL, 0);
2436 uint16_t port_id[!n + n];
2437 struct mlx5_nl_flow_ptoi ptoi[!n + n + 1];
2438 size_t off = RTE_ALIGN_CEIL(sizeof(*flow), alignof(max_align_t));
2440 unsigned int own = 0;
2443 /* At least one port is needed when no switch domain is present. */
2446 port_id[0] = dev->data->port_id;
2448 n = RTE_MIN(mlx5_dev_to_port_id(dev->device, port_id, n), n);
2450 for (i = 0; i != n; ++i) {
2451 struct rte_eth_dev_info dev_info;
2453 rte_eth_dev_info_get(port_id[i], &dev_info);
2454 if (port_id[i] == dev->data->port_id)
2456 ptoi[i].port_id = port_id[i];
2457 ptoi[i].ifindex = dev_info.if_index;
2459 /* Ensure first entry of ptoi[] is the current device. */
2462 ptoi[0] = ptoi[own];
2463 ptoi[own] = ptoi[n];
2465 /* An entry with zero ifindex terminates ptoi[]. */
2466 ptoi[n].port_id = 0;
2467 ptoi[n].ifindex = 0;
2468 if (flow_size < off)
2470 ret = mlx5_nl_flow_transpose((uint8_t *)flow + off,
2471 flow_size ? flow_size - off : 0,
2472 ptoi, attr, pattern, actions, error);
2476 *flow = (struct rte_flow){
2477 .attributes = *attr,
2478 .nl_flow = (uint8_t *)flow + off,
2481 * Generate a reasonably unique handle based on the address
2482 * of the target buffer.
2484 * This is straightforward on 32-bit systems where the flow
2485 * pointer can be used directly. Otherwise, its least
2486 * significant part is taken after shifting it by the
2487 * previous power of two of the pointed buffer size.
2489 if (sizeof(flow) <= 4)
2490 mlx5_nl_flow_brand(flow->nl_flow, (uintptr_t)flow);
2495 rte_log2_u32(rte_align32prevpow2(flow_size)));
2501 mlx5_find_graph_root(const struct rte_flow_item pattern[], uint32_t rss_level)
2503 const struct rte_flow_item *item;
2504 unsigned int has_vlan = 0;
2506 for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
2507 if (item->type == RTE_FLOW_ITEM_TYPE_VLAN) {
2513 return rss_level < 2 ? MLX5_EXPANSION_ROOT_ETH_VLAN :
2514 MLX5_EXPANSION_ROOT_OUTER_ETH_VLAN;
2515 return rss_level < 2 ? MLX5_EXPANSION_ROOT :
2516 MLX5_EXPANSION_ROOT_OUTER;
2520 * Convert the @p attributes, @p pattern, @p action, into an flow for the NIC
2521 * after ensuring the NIC will understand and process it correctly.
2522 * The conversion is only performed item/action per item/action, each of
2523 * them is written into the @p flow if its size is lesser or equal to @p
2525 * Validation and memory consumption computation are still performed until the
2526 * end, unless an error is encountered.
2529 * Pointer to Ethernet device.
2530 * @param[in, out] flow
2531 * Pointer to flow structure.
2532 * @param[in] flow_size
2533 * Size in bytes of the available space in @p flow, if too small some
2534 * garbage may be present.
2535 * @param[in] attributes
2536 * Flow rule attributes.
2537 * @param[in] pattern
2538 * Pattern specification (list terminated by the END pattern item).
2539 * @param[in] actions
2540 * Associated actions (list terminated by the END action).
2542 * Perform verbose error reporting if not NULL.
2545 * On success the number of bytes consumed/necessary, if the returned value
2546 * is lesser or equal to @p flow_size, the flow has fully been converted and
2547 * can be applied, otherwise another call with this returned memory size
2549 * On error, a negative errno value is returned and rte_errno is set.
2552 mlx5_flow_merge(struct rte_eth_dev *dev, struct rte_flow *flow,
2553 const size_t flow_size,
2554 const struct rte_flow_attr *attributes,
2555 const struct rte_flow_item pattern[],
2556 const struct rte_flow_action actions[],
2557 struct rte_flow_error *error)
2559 struct rte_flow local_flow = { .layers = 0, };
2560 size_t size = sizeof(*flow);
2562 struct rte_flow_expand_rss buf;
2563 uint8_t buffer[2048];
2565 struct rte_flow_expand_rss *buf = &expand_buffer.buf;
2566 struct mlx5_flow_verbs *original_verbs = NULL;
2567 size_t original_verbs_size = 0;
2568 uint32_t original_layers = 0;
2569 int expanded_pattern_idx = 0;
2573 if (attributes->transfer)
2574 return mlx5_flow_merge_switch(dev, flow, flow_size,
2575 attributes, pattern,
2577 if (size > flow_size)
2579 ret = mlx5_flow_attributes(dev, attributes, flow, error);
2582 ret = mlx5_flow_actions(dev, actions, &local_flow, 0, error);
2585 if (local_flow.rss.types) {
2586 unsigned int graph_root;
2588 graph_root = mlx5_find_graph_root(pattern,
2589 local_flow.rss.level);
2590 ret = rte_flow_expand_rss(buf, sizeof(expand_buffer.buffer),
2591 pattern, local_flow.rss.types,
2592 mlx5_support_expansion,
2595 (unsigned int)ret < sizeof(expand_buffer.buffer));
2598 buf->entry[0].pattern = (void *)(uintptr_t)pattern;
2600 size += RTE_ALIGN_CEIL(local_flow.rss.queue_num * sizeof(uint16_t),
2602 if (size <= flow_size)
2603 flow->queue = (void *)(flow + 1);
2604 LIST_INIT(&flow->verbs);
2608 for (i = 0; i != buf->entries; ++i) {
2612 flow->layers = original_layers;
2613 size += sizeof(struct ibv_flow_attr) +
2614 sizeof(struct mlx5_flow_verbs);
2616 if (size < flow_size) {
2617 flow->cur_verbs = (void *)((uintptr_t)flow + off);
2618 flow->cur_verbs->attr = (void *)(flow->cur_verbs + 1);
2619 flow->cur_verbs->specs =
2620 (void *)(flow->cur_verbs->attr + 1);
2622 /* First iteration convert the pattern into Verbs. */
2624 /* Actions don't need to be converted several time. */
2625 ret = mlx5_flow_actions(dev, actions, flow,
2626 (size < flow_size) ?
2627 flow_size - size : 0,
2634 * Next iteration means the pattern has already been
2635 * converted and an expansion is necessary to match
2636 * the user RSS request. For that only the expanded
2637 * items will be converted, the common part with the
2638 * user pattern are just copied into the next buffer
2641 size += original_verbs_size;
2642 if (size < flow_size) {
2643 rte_memcpy(flow->cur_verbs->attr,
2644 original_verbs->attr,
2645 original_verbs_size +
2646 sizeof(struct ibv_flow_attr));
2647 flow->cur_verbs->size = original_verbs_size;
2650 ret = mlx5_flow_items
2652 (const struct rte_flow_item *)
2653 &buf->entry[i].pattern[expanded_pattern_idx],
2655 (size < flow_size) ? flow_size - size : 0, error);
2659 if (size <= flow_size) {
2660 mlx5_flow_adjust_priority(dev, flow);
2661 LIST_INSERT_HEAD(&flow->verbs, flow->cur_verbs, next);
2664 * Keep a pointer of the first verbs conversion and the layers
2665 * it has encountered.
2668 original_verbs = flow->cur_verbs;
2669 original_verbs_size = size - off2;
2670 original_layers = flow->layers;
2672 * move the index of the expanded pattern to the
2673 * first item not addressed yet.
2675 if (pattern->type == RTE_FLOW_ITEM_TYPE_END) {
2676 expanded_pattern_idx++;
2678 const struct rte_flow_item *item = pattern;
2680 for (item = pattern;
2681 item->type != RTE_FLOW_ITEM_TYPE_END;
2683 expanded_pattern_idx++;
2687 /* Restore the origin layers in the flow. */
2688 flow->layers = original_layers;
2693 * Lookup and set the ptype in the data Rx part. A single Ptype can be used,
2694 * if several tunnel rules are used on this queue, the tunnel ptype will be
2698 * Rx queue to update.
2701 mlx5_flow_rxq_tunnel_ptype_update(struct mlx5_rxq_ctrl *rxq_ctrl)
2704 uint32_t tunnel_ptype = 0;
2706 /* Look up for the ptype to use. */
2707 for (i = 0; i != MLX5_FLOW_TUNNEL; ++i) {
2708 if (!rxq_ctrl->flow_tunnels_n[i])
2710 if (!tunnel_ptype) {
2711 tunnel_ptype = tunnels_info[i].ptype;
2717 rxq_ctrl->rxq.tunnel = tunnel_ptype;
2721 * Set the Rx queue flags (Mark/Flag and Tunnel Ptypes) according to the flow.
2724 * Pointer to Ethernet device.
2726 * Pointer to flow structure.
2729 mlx5_flow_rxq_flags_set(struct rte_eth_dev *dev, struct rte_flow *flow)
2731 struct priv *priv = dev->data->dev_private;
2732 const int mark = !!(flow->modifier &
2733 (MLX5_FLOW_MOD_FLAG | MLX5_FLOW_MOD_MARK));
2734 const int tunnel = !!(flow->layers & MLX5_FLOW_LAYER_TUNNEL);
2737 for (i = 0; i != flow->rss.queue_num; ++i) {
2738 int idx = (*flow->queue)[i];
2739 struct mlx5_rxq_ctrl *rxq_ctrl =
2740 container_of((*priv->rxqs)[idx],
2741 struct mlx5_rxq_ctrl, rxq);
2744 rxq_ctrl->rxq.mark = 1;
2745 rxq_ctrl->flow_mark_n++;
2750 /* Increase the counter matching the flow. */
2751 for (j = 0; j != MLX5_FLOW_TUNNEL; ++j) {
2752 if ((tunnels_info[j].tunnel & flow->layers) ==
2753 tunnels_info[j].tunnel) {
2754 rxq_ctrl->flow_tunnels_n[j]++;
2758 mlx5_flow_rxq_tunnel_ptype_update(rxq_ctrl);
2764 * Clear the Rx queue flags (Mark/Flag and Tunnel Ptype) associated with the
2765 * @p flow if no other flow uses it with the same kind of request.
2768 * Pointer to Ethernet device.
2770 * Pointer to the flow.
2773 mlx5_flow_rxq_flags_trim(struct rte_eth_dev *dev, struct rte_flow *flow)
2775 struct priv *priv = dev->data->dev_private;
2776 const int mark = !!(flow->modifier &
2777 (MLX5_FLOW_MOD_FLAG | MLX5_FLOW_MOD_MARK));
2778 const int tunnel = !!(flow->layers & MLX5_FLOW_LAYER_TUNNEL);
2781 assert(dev->data->dev_started);
2782 for (i = 0; i != flow->rss.queue_num; ++i) {
2783 int idx = (*flow->queue)[i];
2784 struct mlx5_rxq_ctrl *rxq_ctrl =
2785 container_of((*priv->rxqs)[idx],
2786 struct mlx5_rxq_ctrl, rxq);
2789 rxq_ctrl->flow_mark_n--;
2790 rxq_ctrl->rxq.mark = !!rxq_ctrl->flow_mark_n;
2795 /* Decrease the counter matching the flow. */
2796 for (j = 0; j != MLX5_FLOW_TUNNEL; ++j) {
2797 if ((tunnels_info[j].tunnel & flow->layers) ==
2798 tunnels_info[j].tunnel) {
2799 rxq_ctrl->flow_tunnels_n[j]--;
2803 mlx5_flow_rxq_tunnel_ptype_update(rxq_ctrl);
2809 * Clear the Mark/Flag and Tunnel ptype information in all Rx queues.
2812 * Pointer to Ethernet device.
2815 mlx5_flow_rxq_flags_clear(struct rte_eth_dev *dev)
2817 struct priv *priv = dev->data->dev_private;
2820 for (i = 0; i != priv->rxqs_n; ++i) {
2821 struct mlx5_rxq_ctrl *rxq_ctrl;
2824 if (!(*priv->rxqs)[i])
2826 rxq_ctrl = container_of((*priv->rxqs)[i],
2827 struct mlx5_rxq_ctrl, rxq);
2828 rxq_ctrl->flow_mark_n = 0;
2829 rxq_ctrl->rxq.mark = 0;
2830 for (j = 0; j != MLX5_FLOW_TUNNEL; ++j)
2831 rxq_ctrl->flow_tunnels_n[j] = 0;
2832 rxq_ctrl->rxq.tunnel = 0;
2837 * Validate a flow supported by the NIC.
2839 * @see rte_flow_validate()
2843 mlx5_flow_validate(struct rte_eth_dev *dev,
2844 const struct rte_flow_attr *attr,
2845 const struct rte_flow_item items[],
2846 const struct rte_flow_action actions[],
2847 struct rte_flow_error *error)
2849 int ret = mlx5_flow_merge(dev, NULL, 0, attr, items, actions, error);
2860 * Pointer to Ethernet device.
2861 * @param[in, out] flow
2862 * Pointer to flow structure.
2865 mlx5_flow_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
2867 struct priv *priv = dev->data->dev_private;
2868 struct mlx5_flow_verbs *verbs;
2870 if (flow->nl_flow && priv->mnl_socket)
2871 mlx5_nl_flow_destroy(priv->mnl_socket, flow->nl_flow, NULL);
2872 LIST_FOREACH(verbs, &flow->verbs, next) {
2874 claim_zero(mlx5_glue->destroy_flow(verbs->flow));
2878 if (flow->fate & MLX5_FLOW_FATE_DROP)
2879 mlx5_hrxq_drop_release(dev);
2881 mlx5_hrxq_release(dev, verbs->hrxq);
2885 if (flow->counter) {
2886 mlx5_flow_counter_release(flow->counter);
2887 flow->counter = NULL;
2895 * Pointer to Ethernet device structure.
2896 * @param[in, out] flow
2897 * Pointer to flow structure.
2899 * Pointer to error structure.
2902 * 0 on success, a negative errno value otherwise and rte_errno is set.
2905 mlx5_flow_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
2906 struct rte_flow_error *error)
2908 struct priv *priv = dev->data->dev_private;
2909 struct mlx5_flow_verbs *verbs;
2912 LIST_FOREACH(verbs, &flow->verbs, next) {
2913 if (flow->fate & MLX5_FLOW_FATE_DROP) {
2914 verbs->hrxq = mlx5_hrxq_drop_new(dev);
2918 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2920 "cannot get drop hash queue");
2924 struct mlx5_hrxq *hrxq;
2926 hrxq = mlx5_hrxq_get(dev, flow->key,
2927 MLX5_RSS_HASH_KEY_LEN,
2930 flow->rss.queue_num);
2932 hrxq = mlx5_hrxq_new(dev, flow->key,
2933 MLX5_RSS_HASH_KEY_LEN,
2936 flow->rss.queue_num,
2938 MLX5_FLOW_LAYER_TUNNEL));
2942 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2944 "cannot get hash queue");
2950 mlx5_glue->create_flow(verbs->hrxq->qp, verbs->attr);
2952 rte_flow_error_set(error, errno,
2953 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2955 "hardware refuses to create flow");
2959 if (flow->nl_flow &&
2961 mlx5_nl_flow_create(priv->mnl_socket, flow->nl_flow, error))
2965 err = rte_errno; /* Save rte_errno before cleanup. */
2966 LIST_FOREACH(verbs, &flow->verbs, next) {
2968 if (flow->fate & MLX5_FLOW_FATE_DROP)
2969 mlx5_hrxq_drop_release(dev);
2971 mlx5_hrxq_release(dev, verbs->hrxq);
2975 rte_errno = err; /* Restore rte_errno. */
2980 * Create a flow and add it to @p list.
2983 * Pointer to Ethernet device.
2985 * Pointer to a TAILQ flow list.
2987 * Flow rule attributes.
2989 * Pattern specification (list terminated by the END pattern item).
2990 * @param[in] actions
2991 * Associated actions (list terminated by the END action).
2993 * Perform verbose error reporting if not NULL.
2996 * A flow on success, NULL otherwise and rte_errno is set.
2998 static struct rte_flow *
2999 mlx5_flow_list_create(struct rte_eth_dev *dev,
3000 struct mlx5_flows *list,
3001 const struct rte_flow_attr *attr,
3002 const struct rte_flow_item items[],
3003 const struct rte_flow_action actions[],
3004 struct rte_flow_error *error)
3006 struct rte_flow *flow = NULL;
3010 ret = mlx5_flow_merge(dev, flow, size, attr, items, actions, error);
3014 flow = rte_calloc(__func__, 1, size, 0);
3016 rte_flow_error_set(error, ENOMEM,
3017 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3019 "not enough memory to create flow");
3022 ret = mlx5_flow_merge(dev, flow, size, attr, items, actions, error);
3027 assert((size_t)ret == size);
3028 if (dev->data->dev_started) {
3029 ret = mlx5_flow_apply(dev, flow, error);
3031 ret = rte_errno; /* Save rte_errno before cleanup. */
3033 mlx5_flow_remove(dev, flow);
3036 rte_errno = ret; /* Restore rte_errno. */
3040 TAILQ_INSERT_TAIL(list, flow, next);
3041 mlx5_flow_rxq_flags_set(dev, flow);
3048 * @see rte_flow_create()
3052 mlx5_flow_create(struct rte_eth_dev *dev,
3053 const struct rte_flow_attr *attr,
3054 const struct rte_flow_item items[],
3055 const struct rte_flow_action actions[],
3056 struct rte_flow_error *error)
3058 return mlx5_flow_list_create
3059 (dev, &((struct priv *)dev->data->dev_private)->flows,
3060 attr, items, actions, error);
3064 * Destroy a flow in a list.
3067 * Pointer to Ethernet device.
3069 * Pointer to a TAILQ flow list.
3074 mlx5_flow_list_destroy(struct rte_eth_dev *dev, struct mlx5_flows *list,
3075 struct rte_flow *flow)
3077 mlx5_flow_remove(dev, flow);
3078 TAILQ_REMOVE(list, flow, next);
3080 * Update RX queue flags only if port is started, otherwise it is
3083 if (dev->data->dev_started)
3084 mlx5_flow_rxq_flags_trim(dev, flow);
3089 * Destroy all flows.
3092 * Pointer to Ethernet device.
3094 * Pointer to a TAILQ flow list.
3097 mlx5_flow_list_flush(struct rte_eth_dev *dev, struct mlx5_flows *list)
3099 while (!TAILQ_EMPTY(list)) {
3100 struct rte_flow *flow;
3102 flow = TAILQ_FIRST(list);
3103 mlx5_flow_list_destroy(dev, list, flow);
3111 * Pointer to Ethernet device.
3113 * Pointer to a TAILQ flow list.
3116 mlx5_flow_stop(struct rte_eth_dev *dev, struct mlx5_flows *list)
3118 struct rte_flow *flow;
3120 TAILQ_FOREACH_REVERSE(flow, list, mlx5_flows, next)
3121 mlx5_flow_remove(dev, flow);
3122 mlx5_flow_rxq_flags_clear(dev);
3129 * Pointer to Ethernet device.
3131 * Pointer to a TAILQ flow list.
3134 * 0 on success, a negative errno value otherwise and rte_errno is set.
3137 mlx5_flow_start(struct rte_eth_dev *dev, struct mlx5_flows *list)
3139 struct rte_flow *flow;
3140 struct rte_flow_error error;
3143 TAILQ_FOREACH(flow, list, next) {
3144 ret = mlx5_flow_apply(dev, flow, &error);
3147 mlx5_flow_rxq_flags_set(dev, flow);
3151 ret = rte_errno; /* Save rte_errno before cleanup. */
3152 mlx5_flow_stop(dev, list);
3153 rte_errno = ret; /* Restore rte_errno. */
3158 * Verify the flow list is empty
3161 * Pointer to Ethernet device.
3163 * @return the number of flows not released.
3166 mlx5_flow_verify(struct rte_eth_dev *dev)
3168 struct priv *priv = dev->data->dev_private;
3169 struct rte_flow *flow;
3172 TAILQ_FOREACH(flow, &priv->flows, next) {
3173 DRV_LOG(DEBUG, "port %u flow %p still referenced",
3174 dev->data->port_id, (void *)flow);
3181 * Enable a control flow configured from the control plane.
3184 * Pointer to Ethernet device.
3186 * An Ethernet flow spec to apply.
3188 * An Ethernet flow mask to apply.
3190 * A VLAN flow spec to apply.
3192 * A VLAN flow mask to apply.
3195 * 0 on success, a negative errno value otherwise and rte_errno is set.
3198 mlx5_ctrl_flow_vlan(struct rte_eth_dev *dev,
3199 struct rte_flow_item_eth *eth_spec,
3200 struct rte_flow_item_eth *eth_mask,
3201 struct rte_flow_item_vlan *vlan_spec,
3202 struct rte_flow_item_vlan *vlan_mask)
3204 struct priv *priv = dev->data->dev_private;
3205 const struct rte_flow_attr attr = {
3207 .priority = MLX5_FLOW_PRIO_RSVD,
3209 struct rte_flow_item items[] = {
3211 .type = RTE_FLOW_ITEM_TYPE_ETH,
3217 .type = (vlan_spec) ? RTE_FLOW_ITEM_TYPE_VLAN :
3218 RTE_FLOW_ITEM_TYPE_END,
3224 .type = RTE_FLOW_ITEM_TYPE_END,
3227 uint16_t queue[priv->reta_idx_n];
3228 struct rte_flow_action_rss action_rss = {
3229 .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
3231 .types = priv->rss_conf.rss_hf,
3232 .key_len = priv->rss_conf.rss_key_len,
3233 .queue_num = priv->reta_idx_n,
3234 .key = priv->rss_conf.rss_key,
3237 struct rte_flow_action actions[] = {
3239 .type = RTE_FLOW_ACTION_TYPE_RSS,
3240 .conf = &action_rss,
3243 .type = RTE_FLOW_ACTION_TYPE_END,
3246 struct rte_flow *flow;
3247 struct rte_flow_error error;
3250 if (!priv->reta_idx_n) {
3254 for (i = 0; i != priv->reta_idx_n; ++i)
3255 queue[i] = (*priv->reta_idx)[i];
3256 flow = mlx5_flow_list_create(dev, &priv->ctrl_flows, &attr, items,
3264 * Enable a flow control configured from the control plane.
3267 * Pointer to Ethernet device.
3269 * An Ethernet flow spec to apply.
3271 * An Ethernet flow mask to apply.
3274 * 0 on success, a negative errno value otherwise and rte_errno is set.
3277 mlx5_ctrl_flow(struct rte_eth_dev *dev,
3278 struct rte_flow_item_eth *eth_spec,
3279 struct rte_flow_item_eth *eth_mask)
3281 return mlx5_ctrl_flow_vlan(dev, eth_spec, eth_mask, NULL, NULL);
3287 * @see rte_flow_destroy()
3291 mlx5_flow_destroy(struct rte_eth_dev *dev,
3292 struct rte_flow *flow,
3293 struct rte_flow_error *error __rte_unused)
3295 struct priv *priv = dev->data->dev_private;
3297 mlx5_flow_list_destroy(dev, &priv->flows, flow);
3302 * Destroy all flows.
3304 * @see rte_flow_flush()
3308 mlx5_flow_flush(struct rte_eth_dev *dev,
3309 struct rte_flow_error *error __rte_unused)
3311 struct priv *priv = dev->data->dev_private;
3313 mlx5_flow_list_flush(dev, &priv->flows);
3320 * @see rte_flow_isolate()
3324 mlx5_flow_isolate(struct rte_eth_dev *dev,
3326 struct rte_flow_error *error)
3328 struct priv *priv = dev->data->dev_private;
3330 if (dev->data->dev_started) {
3331 rte_flow_error_set(error, EBUSY,
3332 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3334 "port must be stopped first");
3337 priv->isolated = !!enable;
3339 dev->dev_ops = &mlx5_dev_ops_isolate;
3341 dev->dev_ops = &mlx5_dev_ops;
3346 * Query flow counter.
3349 * Pointer to the flow.
3352 * 0 on success, a negative errno value otherwise and rte_errno is set.
3355 mlx5_flow_query_count(struct rte_flow *flow __rte_unused,
3356 void *data __rte_unused,
3357 struct rte_flow_error *error)
3359 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
3360 if (flow->modifier & MLX5_FLOW_MOD_COUNT) {
3361 struct rte_flow_query_count *qc = data;
3362 uint64_t counters[2] = {0, 0};
3363 struct ibv_query_counter_set_attr query_cs_attr = {
3364 .cs = flow->counter->cs,
3365 .query_flags = IBV_COUNTER_SET_FORCE_UPDATE,
3367 struct ibv_counter_set_data query_out = {
3369 .outlen = 2 * sizeof(uint64_t),
3371 int err = mlx5_glue->query_counter_set(&query_cs_attr,
3375 return rte_flow_error_set
3377 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3379 "cannot read counter");
3382 qc->hits = counters[0] - flow->counter->hits;
3383 qc->bytes = counters[1] - flow->counter->bytes;
3385 flow->counter->hits = counters[0];
3386 flow->counter->bytes = counters[1];
3390 return rte_flow_error_set(error, ENOTSUP,
3391 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3393 "flow does not have counter");
3395 return rte_flow_error_set(error, ENOTSUP,
3396 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3398 "counters are not available");
3404 * @see rte_flow_query()
3408 mlx5_flow_query(struct rte_eth_dev *dev __rte_unused,
3409 struct rte_flow *flow,
3410 const struct rte_flow_action *actions,
3412 struct rte_flow_error *error)
3416 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
3417 switch (actions->type) {
3418 case RTE_FLOW_ACTION_TYPE_VOID:
3420 case RTE_FLOW_ACTION_TYPE_COUNT:
3421 ret = mlx5_flow_query_count(flow, data, error);
3424 return rte_flow_error_set(error, ENOTSUP,
3425 RTE_FLOW_ERROR_TYPE_ACTION,
3427 "action not supported");
3436 * Convert a flow director filter to a generic flow.
3439 * Pointer to Ethernet device.
3440 * @param fdir_filter
3441 * Flow director filter to add.
3443 * Generic flow parameters structure.
3446 * 0 on success, a negative errno value otherwise and rte_errno is set.
3449 mlx5_fdir_filter_convert(struct rte_eth_dev *dev,
3450 const struct rte_eth_fdir_filter *fdir_filter,
3451 struct mlx5_fdir *attributes)
3453 struct priv *priv = dev->data->dev_private;
3454 const struct rte_eth_fdir_input *input = &fdir_filter->input;
3455 const struct rte_eth_fdir_masks *mask =
3456 &dev->data->dev_conf.fdir_conf.mask;
3458 /* Validate queue number. */
3459 if (fdir_filter->action.rx_queue >= priv->rxqs_n) {
3460 DRV_LOG(ERR, "port %u invalid queue number %d",
3461 dev->data->port_id, fdir_filter->action.rx_queue);
3465 attributes->attr.ingress = 1;
3466 attributes->items[0] = (struct rte_flow_item) {
3467 .type = RTE_FLOW_ITEM_TYPE_ETH,
3468 .spec = &attributes->l2,
3469 .mask = &attributes->l2_mask,
3471 switch (fdir_filter->action.behavior) {
3472 case RTE_ETH_FDIR_ACCEPT:
3473 attributes->actions[0] = (struct rte_flow_action){
3474 .type = RTE_FLOW_ACTION_TYPE_QUEUE,
3475 .conf = &attributes->queue,
3478 case RTE_ETH_FDIR_REJECT:
3479 attributes->actions[0] = (struct rte_flow_action){
3480 .type = RTE_FLOW_ACTION_TYPE_DROP,
3484 DRV_LOG(ERR, "port %u invalid behavior %d",
3486 fdir_filter->action.behavior);
3487 rte_errno = ENOTSUP;
3490 attributes->queue.index = fdir_filter->action.rx_queue;
3492 switch (fdir_filter->input.flow_type) {
3493 case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
3494 case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
3495 case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
3496 attributes->l3.ipv4.hdr = (struct ipv4_hdr){
3497 .src_addr = input->flow.ip4_flow.src_ip,
3498 .dst_addr = input->flow.ip4_flow.dst_ip,
3499 .time_to_live = input->flow.ip4_flow.ttl,
3500 .type_of_service = input->flow.ip4_flow.tos,
3501 .next_proto_id = input->flow.ip4_flow.proto,
3503 attributes->l3_mask.ipv4.hdr = (struct ipv4_hdr){
3504 .src_addr = mask->ipv4_mask.src_ip,
3505 .dst_addr = mask->ipv4_mask.dst_ip,
3506 .time_to_live = mask->ipv4_mask.ttl,
3507 .type_of_service = mask->ipv4_mask.tos,
3508 .next_proto_id = mask->ipv4_mask.proto,
3510 attributes->items[1] = (struct rte_flow_item){
3511 .type = RTE_FLOW_ITEM_TYPE_IPV4,
3512 .spec = &attributes->l3,
3513 .mask = &attributes->l3_mask,
3516 case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
3517 case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
3518 case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
3519 attributes->l3.ipv6.hdr = (struct ipv6_hdr){
3520 .hop_limits = input->flow.ipv6_flow.hop_limits,
3521 .proto = input->flow.ipv6_flow.proto,
3524 memcpy(attributes->l3.ipv6.hdr.src_addr,
3525 input->flow.ipv6_flow.src_ip,
3526 RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
3527 memcpy(attributes->l3.ipv6.hdr.dst_addr,
3528 input->flow.ipv6_flow.dst_ip,
3529 RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
3530 memcpy(attributes->l3_mask.ipv6.hdr.src_addr,
3531 mask->ipv6_mask.src_ip,
3532 RTE_DIM(attributes->l3_mask.ipv6.hdr.src_addr));
3533 memcpy(attributes->l3_mask.ipv6.hdr.dst_addr,
3534 mask->ipv6_mask.dst_ip,
3535 RTE_DIM(attributes->l3_mask.ipv6.hdr.src_addr));
3536 attributes->items[1] = (struct rte_flow_item){
3537 .type = RTE_FLOW_ITEM_TYPE_IPV6,
3538 .spec = &attributes->l3,
3539 .mask = &attributes->l3_mask,
3543 DRV_LOG(ERR, "port %u invalid flow type%d",
3544 dev->data->port_id, fdir_filter->input.flow_type);
3545 rte_errno = ENOTSUP;
3549 switch (fdir_filter->input.flow_type) {
3550 case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
3551 attributes->l4.udp.hdr = (struct udp_hdr){
3552 .src_port = input->flow.udp4_flow.src_port,
3553 .dst_port = input->flow.udp4_flow.dst_port,
3555 attributes->l4_mask.udp.hdr = (struct udp_hdr){
3556 .src_port = mask->src_port_mask,
3557 .dst_port = mask->dst_port_mask,
3559 attributes->items[2] = (struct rte_flow_item){
3560 .type = RTE_FLOW_ITEM_TYPE_UDP,
3561 .spec = &attributes->l4,
3562 .mask = &attributes->l4_mask,
3565 case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
3566 attributes->l4.tcp.hdr = (struct tcp_hdr){
3567 .src_port = input->flow.tcp4_flow.src_port,
3568 .dst_port = input->flow.tcp4_flow.dst_port,
3570 attributes->l4_mask.tcp.hdr = (struct tcp_hdr){
3571 .src_port = mask->src_port_mask,
3572 .dst_port = mask->dst_port_mask,
3574 attributes->items[2] = (struct rte_flow_item){
3575 .type = RTE_FLOW_ITEM_TYPE_TCP,
3576 .spec = &attributes->l4,
3577 .mask = &attributes->l4_mask,
3580 case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
3581 attributes->l4.udp.hdr = (struct udp_hdr){
3582 .src_port = input->flow.udp6_flow.src_port,
3583 .dst_port = input->flow.udp6_flow.dst_port,
3585 attributes->l4_mask.udp.hdr = (struct udp_hdr){
3586 .src_port = mask->src_port_mask,
3587 .dst_port = mask->dst_port_mask,
3589 attributes->items[2] = (struct rte_flow_item){
3590 .type = RTE_FLOW_ITEM_TYPE_UDP,
3591 .spec = &attributes->l4,
3592 .mask = &attributes->l4_mask,
3595 case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
3596 attributes->l4.tcp.hdr = (struct tcp_hdr){
3597 .src_port = input->flow.tcp6_flow.src_port,
3598 .dst_port = input->flow.tcp6_flow.dst_port,
3600 attributes->l4_mask.tcp.hdr = (struct tcp_hdr){
3601 .src_port = mask->src_port_mask,
3602 .dst_port = mask->dst_port_mask,
3604 attributes->items[2] = (struct rte_flow_item){
3605 .type = RTE_FLOW_ITEM_TYPE_TCP,
3606 .spec = &attributes->l4,
3607 .mask = &attributes->l4_mask,
3610 case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
3611 case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
3614 DRV_LOG(ERR, "port %u invalid flow type%d",
3615 dev->data->port_id, fdir_filter->input.flow_type);
3616 rte_errno = ENOTSUP;
3623 * Add new flow director filter and store it in list.
3626 * Pointer to Ethernet device.
3627 * @param fdir_filter
3628 * Flow director filter to add.
3631 * 0 on success, a negative errno value otherwise and rte_errno is set.
3634 mlx5_fdir_filter_add(struct rte_eth_dev *dev,
3635 const struct rte_eth_fdir_filter *fdir_filter)
3637 struct priv *priv = dev->data->dev_private;
3638 struct mlx5_fdir attributes = {
3641 .dst.addr_bytes = "\x00\x00\x00\x00\x00\x00",
3642 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
3646 struct rte_flow_error error;
3647 struct rte_flow *flow;
3650 ret = mlx5_fdir_filter_convert(dev, fdir_filter, &attributes);
3653 flow = mlx5_flow_list_create(dev, &priv->flows, &attributes.attr,
3654 attributes.items, attributes.actions,
3657 DRV_LOG(DEBUG, "port %u FDIR created %p", dev->data->port_id,
3665 * Delete specific filter.
3668 * Pointer to Ethernet device.
3669 * @param fdir_filter
3670 * Filter to be deleted.
3673 * 0 on success, a negative errno value otherwise and rte_errno is set.
3676 mlx5_fdir_filter_delete(struct rte_eth_dev *dev __rte_unused,
3677 const struct rte_eth_fdir_filter *fdir_filter
3680 rte_errno = ENOTSUP;
3685 * Update queue for specific filter.
3688 * Pointer to Ethernet device.
3689 * @param fdir_filter
3690 * Filter to be updated.
3693 * 0 on success, a negative errno value otherwise and rte_errno is set.
3696 mlx5_fdir_filter_update(struct rte_eth_dev *dev,
3697 const struct rte_eth_fdir_filter *fdir_filter)
3701 ret = mlx5_fdir_filter_delete(dev, fdir_filter);
3704 return mlx5_fdir_filter_add(dev, fdir_filter);
3708 * Flush all filters.
3711 * Pointer to Ethernet device.
3714 mlx5_fdir_filter_flush(struct rte_eth_dev *dev)
3716 struct priv *priv = dev->data->dev_private;
3718 mlx5_flow_list_flush(dev, &priv->flows);
3722 * Get flow director information.
3725 * Pointer to Ethernet device.
3726 * @param[out] fdir_info
3727 * Resulting flow director information.
3730 mlx5_fdir_info_get(struct rte_eth_dev *dev, struct rte_eth_fdir_info *fdir_info)
3732 struct rte_eth_fdir_masks *mask =
3733 &dev->data->dev_conf.fdir_conf.mask;
3735 fdir_info->mode = dev->data->dev_conf.fdir_conf.mode;
3736 fdir_info->guarant_spc = 0;
3737 rte_memcpy(&fdir_info->mask, mask, sizeof(fdir_info->mask));
3738 fdir_info->max_flexpayload = 0;
3739 fdir_info->flow_types_mask[0] = 0;
3740 fdir_info->flex_payload_unit = 0;
3741 fdir_info->max_flex_payload_segment_num = 0;
3742 fdir_info->flex_payload_limit = 0;
3743 memset(&fdir_info->flex_conf, 0, sizeof(fdir_info->flex_conf));
3747 * Deal with flow director operations.
3750 * Pointer to Ethernet device.
3752 * Operation to perform.
3754 * Pointer to operation-specific structure.
3757 * 0 on success, a negative errno value otherwise and rte_errno is set.
3760 mlx5_fdir_ctrl_func(struct rte_eth_dev *dev, enum rte_filter_op filter_op,
3763 enum rte_fdir_mode fdir_mode =
3764 dev->data->dev_conf.fdir_conf.mode;
3766 if (filter_op == RTE_ETH_FILTER_NOP)
3768 if (fdir_mode != RTE_FDIR_MODE_PERFECT &&
3769 fdir_mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
3770 DRV_LOG(ERR, "port %u flow director mode %d not supported",
3771 dev->data->port_id, fdir_mode);
3775 switch (filter_op) {
3776 case RTE_ETH_FILTER_ADD:
3777 return mlx5_fdir_filter_add(dev, arg);
3778 case RTE_ETH_FILTER_UPDATE:
3779 return mlx5_fdir_filter_update(dev, arg);
3780 case RTE_ETH_FILTER_DELETE:
3781 return mlx5_fdir_filter_delete(dev, arg);
3782 case RTE_ETH_FILTER_FLUSH:
3783 mlx5_fdir_filter_flush(dev);
3785 case RTE_ETH_FILTER_INFO:
3786 mlx5_fdir_info_get(dev, arg);
3789 DRV_LOG(DEBUG, "port %u unknown operation %u",
3790 dev->data->port_id, filter_op);
3798 * Manage filter operations.
3801 * Pointer to Ethernet device structure.
3802 * @param filter_type
3805 * Operation to perform.
3807 * Pointer to operation-specific structure.
3810 * 0 on success, a negative errno value otherwise and rte_errno is set.
3813 mlx5_dev_filter_ctrl(struct rte_eth_dev *dev,
3814 enum rte_filter_type filter_type,
3815 enum rte_filter_op filter_op,
3818 switch (filter_type) {
3819 case RTE_ETH_FILTER_GENERIC:
3820 if (filter_op != RTE_ETH_FILTER_GET) {
3824 *(const void **)arg = &mlx5_flow_ops;
3826 case RTE_ETH_FILTER_FDIR:
3827 return mlx5_fdir_ctrl_func(dev, filter_op, arg);
3829 DRV_LOG(ERR, "port %u filter type (%d) not supported",
3830 dev->data->port_id, filter_type);
3831 rte_errno = ENOTSUP;