1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016 6WIND S.A.
3 * Copyright 2016 Mellanox Technologies, Ltd
11 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
13 #pragma GCC diagnostic ignored "-Wpedantic"
15 #include <infiniband/verbs.h>
17 #pragma GCC diagnostic error "-Wpedantic"
20 #include <rte_common.h>
21 #include <rte_ether.h>
22 #include <rte_eth_ctrl.h>
23 #include <rte_ethdev_driver.h>
25 #include <rte_flow_driver.h>
26 #include <rte_malloc.h>
30 #include "mlx5_defs.h"
32 #include "mlx5_glue.h"
34 /* Dev ops structure defined in mlx5.c */
35 extern const struct eth_dev_ops mlx5_dev_ops;
36 extern const struct eth_dev_ops mlx5_dev_ops_isolate;
38 /* Pattern Layer bits. */
39 #define MLX5_FLOW_LAYER_OUTER_L2 (1u << 0)
40 #define MLX5_FLOW_LAYER_OUTER_L3_IPV4 (1u << 1)
41 #define MLX5_FLOW_LAYER_OUTER_L3_IPV6 (1u << 2)
42 #define MLX5_FLOW_LAYER_OUTER_L4_UDP (1u << 3)
43 #define MLX5_FLOW_LAYER_OUTER_L4_TCP (1u << 4)
44 #define MLX5_FLOW_LAYER_OUTER_VLAN (1u << 5)
46 #define MLX5_FLOW_LAYER_OUTER_L3 \
47 (MLX5_FLOW_LAYER_OUTER_L3_IPV4 | MLX5_FLOW_LAYER_OUTER_L3_IPV6)
48 #define MLX5_FLOW_LAYER_OUTER_L4 \
49 (MLX5_FLOW_LAYER_OUTER_L4_UDP | MLX5_FLOW_LAYER_OUTER_L4_TCP)
51 /* Actions that modify the fate of matching traffic. */
52 #define MLX5_FLOW_FATE_DROP (1u << 0)
53 #define MLX5_FLOW_FATE_QUEUE (1u << 1)
55 /** Handles information leading to a drop fate. */
56 struct mlx5_flow_verbs {
57 unsigned int size; /**< Size of the attribute. */
59 struct ibv_flow_attr *attr;
60 /**< Pointer to the Specification buffer. */
61 uint8_t *specs; /**< Pointer to the specifications. */
63 struct ibv_flow *flow; /**< Verbs flow pointer. */
64 struct mlx5_hrxq *hrxq; /**< Hash Rx queue object. */
69 TAILQ_ENTRY(rte_flow) next; /**< Pointer to the next flow structure. */
70 struct rte_flow_attr attributes; /**< User flow attribute. */
72 /**< Bit-fields of present layers see MLX5_FLOW_LAYER_*. */
74 /**< Bit-fields of present fate see MLX5_FLOW_FATE_*. */
75 struct mlx5_flow_verbs verbs; /* Verbs flow. */
76 uint16_t queue; /**< Destination queue to redirect traffic to. */
79 static const struct rte_flow_ops mlx5_flow_ops = {
80 .validate = mlx5_flow_validate,
81 .create = mlx5_flow_create,
82 .destroy = mlx5_flow_destroy,
83 .flush = mlx5_flow_flush,
84 .isolate = mlx5_flow_isolate,
87 /* Convert FDIR request to Generic flow. */
89 struct rte_flow_attr attr;
90 struct rte_flow_action actions[2];
91 struct rte_flow_item items[4];
92 struct rte_flow_item_eth l2;
93 struct rte_flow_item_eth l2_mask;
95 struct rte_flow_item_ipv4 ipv4;
96 struct rte_flow_item_ipv6 ipv6;
99 struct rte_flow_item_ipv4 ipv4;
100 struct rte_flow_item_ipv6 ipv6;
103 struct rte_flow_item_udp udp;
104 struct rte_flow_item_tcp tcp;
107 struct rte_flow_item_udp udp;
108 struct rte_flow_item_tcp tcp;
110 struct rte_flow_action_queue queue;
113 /* Verbs specification header. */
114 struct ibv_spec_header {
115 enum ibv_flow_spec_type type;
120 * Discover the maximum number of priority available.
123 * Pointer to Ethernet device.
126 * number of supported flow priority on success, a negative errno value
127 * otherwise and rte_errno is set.
130 mlx5_flow_discover_priorities(struct rte_eth_dev *dev)
133 struct ibv_flow_attr attr;
134 struct ibv_flow_spec_eth eth;
135 struct ibv_flow_spec_action_drop drop;
141 .type = IBV_FLOW_SPEC_ETH,
142 .size = sizeof(struct ibv_flow_spec_eth),
145 .size = sizeof(struct ibv_flow_spec_action_drop),
146 .type = IBV_FLOW_SPEC_ACTION_DROP,
149 struct ibv_flow *flow;
150 struct mlx5_hrxq *drop = mlx5_hrxq_drop_new(dev);
151 uint16_t vprio[] = { 8, 16 };
158 for (i = 0; i != RTE_DIM(vprio); i++) {
159 flow_attr.attr.priority = vprio[i] - 1;
160 flow = mlx5_glue->create_flow(drop->qp, &flow_attr.attr);
163 claim_zero(mlx5_glue->destroy_flow(flow));
165 mlx5_hrxq_drop_release(dev);
166 DRV_LOG(INFO, "port %u flow maximum priority: %d",
167 dev->data->port_id, vprio[i - 1]);
172 * Verify the @p attributes will be correctly understood by the NIC and store
173 * them in the @p flow if everything is correct.
176 * Pointer to Ethernet device.
177 * @param[in] attributes
178 * Pointer to flow attributes
179 * @param[in, out] flow
180 * Pointer to the rte_flow structure.
182 * Pointer to error structure.
185 * 0 on success, a negative errno value otherwise and rte_errno is set.
188 mlx5_flow_attributes(struct rte_eth_dev *dev,
189 const struct rte_flow_attr *attributes,
190 struct rte_flow *flow,
191 struct rte_flow_error *error)
193 uint32_t priority_max =
194 ((struct priv *)dev->data->dev_private)->config.flow_prio;
196 if (attributes->group)
197 return rte_flow_error_set(error, ENOTSUP,
198 RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
200 "groups is not supported");
201 if (attributes->priority >= priority_max)
202 return rte_flow_error_set(error, ENOTSUP,
203 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
205 "priority out of range");
206 if (attributes->egress)
207 return rte_flow_error_set(error, ENOTSUP,
208 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
210 "egress is not supported");
211 if (attributes->transfer)
212 return rte_flow_error_set(error, ENOTSUP,
213 RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
215 "transfer is not supported");
216 if (!attributes->ingress)
217 return rte_flow_error_set(error, ENOTSUP,
218 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
220 "ingress attribute is mandatory");
221 flow->attributes = *attributes;
226 * Verify the @p item specifications (spec, last, mask) are compatible with the
230 * Item specification.
232 * @p item->mask or flow default bit-masks.
233 * @param[in] nic_mask
234 * Bit-masks covering supported fields by the NIC to compare with user mask.
236 * Bit-masks size in bytes.
238 * Pointer to error structure.
241 * 0 on success, a negative errno value otherwise and rte_errno is set.
244 mlx5_flow_item_acceptable(const struct rte_flow_item *item,
246 const uint8_t *nic_mask,
248 struct rte_flow_error *error)
253 for (i = 0; i < size; ++i)
254 if ((nic_mask[i] | mask[i]) != nic_mask[i])
255 return rte_flow_error_set(error, ENOTSUP,
256 RTE_FLOW_ERROR_TYPE_ITEM,
258 "mask enables non supported"
260 if (!item->spec && (item->mask || item->last))
261 return rte_flow_error_set(error, EINVAL,
262 RTE_FLOW_ERROR_TYPE_ITEM,
264 "mask/last without a spec is not"
266 if (item->spec && item->last) {
272 for (i = 0; i < size; ++i) {
273 spec[i] = ((const uint8_t *)item->spec)[i] & mask[i];
274 last[i] = ((const uint8_t *)item->last)[i] & mask[i];
276 ret = memcmp(spec, last, size);
278 return rte_flow_error_set(error, ENOTSUP,
279 RTE_FLOW_ERROR_TYPE_ITEM,
281 "range is not supported");
287 * Add a verbs specification into @p flow.
289 * @param[in, out] flow
290 * Pointer to flow structure.
292 * Create specification.
294 * Size in bytes of the specification to copy.
297 mlx5_flow_spec_verbs_add(struct rte_flow *flow, void *src, unsigned int size)
299 if (flow->verbs.specs) {
302 dst = (void *)(flow->verbs.specs + flow->verbs.size);
303 memcpy(dst, src, size);
304 ++flow->verbs.attr->num_of_specs;
306 flow->verbs.size += size;
310 * Convert the @p item into a Verbs specification after ensuring the NIC
311 * will understand and process it correctly.
312 * If the necessary size for the conversion is greater than the @p flow_size,
313 * nothing is written in @p flow, the validation is still performed.
316 * Item specification.
317 * @param[in, out] flow
318 * Pointer to flow structure.
319 * @param[in] flow_size
320 * Size in bytes of the available space in @p flow, if too small, nothing is
323 * Pointer to error structure.
326 * On success the number of bytes consumed/necessary, if the returned value
327 * is lesser or equal to @p flow_size, the @p item has fully been converted,
328 * otherwise another call with this returned memory size should be done.
329 * On error, a negative errno value is returned and rte_errno is set.
332 mlx5_flow_item_eth(const struct rte_flow_item *item, struct rte_flow *flow,
333 const size_t flow_size, struct rte_flow_error *error)
335 const struct rte_flow_item_eth *spec = item->spec;
336 const struct rte_flow_item_eth *mask = item->mask;
337 const struct rte_flow_item_eth nic_mask = {
338 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
339 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
340 .type = RTE_BE16(0xffff),
342 const unsigned int size = sizeof(struct ibv_flow_spec_eth);
343 struct ibv_flow_spec_eth eth = {
344 .type = IBV_FLOW_SPEC_ETH,
349 if (flow->layers & MLX5_FLOW_LAYER_OUTER_L2)
350 return rte_flow_error_set(error, ENOTSUP,
351 RTE_FLOW_ERROR_TYPE_ITEM,
353 "L2 layers already configured");
355 mask = &rte_flow_item_eth_mask;
356 ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
357 (const uint8_t *)&nic_mask,
358 sizeof(struct rte_flow_item_eth),
362 flow->layers |= MLX5_FLOW_LAYER_OUTER_L2;
363 if (size > flow_size)
368 memcpy(ð.val.dst_mac, spec->dst.addr_bytes, ETHER_ADDR_LEN);
369 memcpy(ð.val.src_mac, spec->src.addr_bytes, ETHER_ADDR_LEN);
370 eth.val.ether_type = spec->type;
371 memcpy(ð.mask.dst_mac, mask->dst.addr_bytes, ETHER_ADDR_LEN);
372 memcpy(ð.mask.src_mac, mask->src.addr_bytes, ETHER_ADDR_LEN);
373 eth.mask.ether_type = mask->type;
374 /* Remove unwanted bits from values. */
375 for (i = 0; i < ETHER_ADDR_LEN; ++i) {
376 eth.val.dst_mac[i] &= eth.mask.dst_mac[i];
377 eth.val.src_mac[i] &= eth.mask.src_mac[i];
379 eth.val.ether_type &= eth.mask.ether_type;
381 mlx5_flow_spec_verbs_add(flow, ð, size);
386 * Convert the @p pattern into a Verbs specifications after ensuring the NIC
387 * will understand and process it correctly.
388 * The conversion is performed item per item, each of them is written into
389 * the @p flow if its size is lesser or equal to @p flow_size.
390 * Validation and memory consumption computation are still performed until the
391 * end of @p pattern, unless an error is encountered.
395 * @param[in, out] flow
396 * Pointer to the rte_flow structure.
397 * @param[in] flow_size
398 * Size in bytes of the available space in @p flow, if too small some
399 * garbage may be present.
401 * Pointer to error structure.
404 * On success the number of bytes consumed/necessary, if the returned value
405 * is lesser or equal to @p flow_size, the @pattern has fully been
406 * converted, otherwise another call with this returned memory size should
408 * On error, a negative errno value is returned and rte_errno is set.
411 mlx5_flow_items(const struct rte_flow_item pattern[],
412 struct rte_flow *flow, const size_t flow_size,
413 struct rte_flow_error *error)
415 int remain = flow_size;
418 for (; pattern->type != RTE_FLOW_ITEM_TYPE_END; pattern++) {
421 switch (pattern->type) {
422 case RTE_FLOW_ITEM_TYPE_VOID:
424 case RTE_FLOW_ITEM_TYPE_ETH:
425 ret = mlx5_flow_item_eth(pattern, flow, remain, error);
428 return rte_flow_error_set(error, ENOTSUP,
429 RTE_FLOW_ERROR_TYPE_ITEM,
431 "item not supported");
442 const struct rte_flow_item item = {
443 .type = RTE_FLOW_ITEM_TYPE_ETH,
446 return mlx5_flow_item_eth(&item, flow, flow_size, error);
452 * Convert the @p action into a Verbs specification after ensuring the NIC
453 * will understand and process it correctly.
454 * If the necessary size for the conversion is greater than the @p flow_size,
455 * nothing is written in @p flow, the validation is still performed.
458 * Action configuration.
459 * @param[in, out] flow
460 * Pointer to flow structure.
461 * @param[in] flow_size
462 * Size in bytes of the available space in @p flow, if too small, nothing is
465 * Pointer to error structure.
468 * On success the number of bytes consumed/necessary, if the returned value
469 * is lesser or equal to @p flow_size, the @p action has fully been
470 * converted, otherwise another call with this returned memory size should
472 * On error, a negative errno value is returned and rte_errno is set.
475 mlx5_flow_action_drop(const struct rte_flow_action *action,
476 struct rte_flow *flow, const size_t flow_size,
477 struct rte_flow_error *error)
479 unsigned int size = sizeof(struct ibv_flow_spec_action_drop);
480 struct ibv_flow_spec_action_drop drop = {
481 .type = IBV_FLOW_SPEC_ACTION_DROP,
486 return rte_flow_error_set(error, ENOTSUP,
487 RTE_FLOW_ERROR_TYPE_ACTION,
489 "multiple fate actions are not"
491 if (size < flow_size)
492 mlx5_flow_spec_verbs_add(flow, &drop, size);
493 flow->fate |= MLX5_FLOW_FATE_DROP;
498 * Convert the @p action into @p flow after ensuring the NIC will understand
499 * and process it correctly.
502 * Pointer to Ethernet device structure.
504 * Action configuration.
505 * @param[in, out] flow
506 * Pointer to flow structure.
508 * Pointer to error structure.
511 * 0 on success, a negative errno value otherwise and rte_errno is set.
514 mlx5_flow_action_queue(struct rte_eth_dev *dev,
515 const struct rte_flow_action *action,
516 struct rte_flow *flow,
517 struct rte_flow_error *error)
519 struct priv *priv = dev->data->dev_private;
520 const struct rte_flow_action_queue *queue = action->conf;
523 return rte_flow_error_set(error, ENOTSUP,
524 RTE_FLOW_ERROR_TYPE_ACTION,
526 "multiple fate actions are not"
528 if (queue->index >= priv->rxqs_n)
529 return rte_flow_error_set(error, EINVAL,
530 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
532 "queue index out of range");
533 if (!(*priv->rxqs)[queue->index])
534 return rte_flow_error_set(error, EINVAL,
535 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
537 "queue is not configured");
538 flow->queue = queue->index;
539 flow->fate |= MLX5_FLOW_FATE_QUEUE;
544 * Convert the @p action into @p flow after ensuring the NIC will understand
545 * and process it correctly.
546 * The conversion is performed action per action, each of them is written into
547 * the @p flow if its size is lesser or equal to @p flow_size.
548 * Validation and memory consumption computation are still performed until the
549 * end of @p action, unless an error is encountered.
552 * Pointer to Ethernet device structure.
554 * Pointer to flow actions array.
555 * @param[in, out] flow
556 * Pointer to the rte_flow structure.
557 * @param[in] flow_size
558 * Size in bytes of the available space in @p flow, if too small some
559 * garbage may be present.
561 * Pointer to error structure.
564 * On success the number of bytes consumed/necessary, if the returned value
565 * is lesser or equal to @p flow_size, the @p actions has fully been
566 * converted, otherwise another call with this returned memory size should
568 * On error, a negative errno value is returned and rte_errno is set.
571 mlx5_flow_actions(struct rte_eth_dev *dev,
572 const struct rte_flow_action actions[],
573 struct rte_flow *flow, const size_t flow_size,
574 struct rte_flow_error *error)
577 int remain = flow_size;
580 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
581 switch (actions->type) {
582 case RTE_FLOW_ACTION_TYPE_VOID:
584 case RTE_FLOW_ACTION_TYPE_DROP:
585 ret = mlx5_flow_action_drop(actions, flow, remain,
588 case RTE_FLOW_ACTION_TYPE_QUEUE:
589 ret = mlx5_flow_action_queue(dev, actions, flow, error);
592 return rte_flow_error_set(error, ENOTSUP,
593 RTE_FLOW_ERROR_TYPE_ACTION,
595 "action not supported");
606 return rte_flow_error_set(error, ENOTSUP,
607 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
609 "no fate action found");
614 * Convert the @p attributes, @p pattern, @p action, into an flow for the NIC
615 * after ensuring the NIC will understand and process it correctly.
616 * The conversion is only performed item/action per item/action, each of
617 * them is written into the @p flow if its size is lesser or equal to @p
619 * Validation and memory consumption computation are still performed until the
620 * end, unless an error is encountered.
623 * Pointer to Ethernet device.
624 * @param[in, out] flow
625 * Pointer to flow structure.
626 * @param[in] flow_size
627 * Size in bytes of the available space in @p flow, if too small some
628 * garbage may be present.
629 * @param[in] attributes
630 * Flow rule attributes.
632 * Pattern specification (list terminated by the END pattern item).
634 * Associated actions (list terminated by the END action).
636 * Perform verbose error reporting if not NULL.
639 * On success the number of bytes consumed/necessary, if the returned value
640 * is lesser or equal to @p flow_size, the flow has fully been converted and
641 * can be applied, otherwise another call with this returned memory size
643 * On error, a negative errno value is returned and rte_errno is set.
646 mlx5_flow_merge(struct rte_eth_dev *dev, struct rte_flow *flow,
647 const size_t flow_size,
648 const struct rte_flow_attr *attributes,
649 const struct rte_flow_item pattern[],
650 const struct rte_flow_action actions[],
651 struct rte_flow_error *error)
653 struct rte_flow local_flow = { .layers = 0, };
654 size_t size = sizeof(*flow) + sizeof(struct ibv_flow_attr);
655 int remain = (flow_size > size) ? flow_size - size : 0;
660 ret = mlx5_flow_attributes(dev, attributes, flow, error);
663 ret = mlx5_flow_items(pattern, flow, remain, error);
667 remain = (flow_size > size) ? flow_size - size : 0;
668 ret = mlx5_flow_actions(dev, actions, flow, remain, error);
672 if (size <= flow_size)
673 flow->verbs.attr->priority = flow->attributes.priority;
678 * Validate a flow supported by the NIC.
680 * @see rte_flow_validate()
684 mlx5_flow_validate(struct rte_eth_dev *dev,
685 const struct rte_flow_attr *attr,
686 const struct rte_flow_item items[],
687 const struct rte_flow_action actions[],
688 struct rte_flow_error *error)
690 int ret = mlx5_flow_merge(dev, NULL, 0, attr, items, actions, error);
701 * Pointer to Ethernet device.
702 * @param[in, out] flow
703 * Pointer to flow structure.
706 mlx5_flow_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
708 if (flow->fate & MLX5_FLOW_FATE_DROP) {
709 if (flow->verbs.flow) {
710 claim_zero(mlx5_glue->destroy_flow(flow->verbs.flow));
711 flow->verbs.flow = NULL;
714 if (flow->verbs.hrxq) {
715 if (flow->fate & MLX5_FLOW_FATE_DROP)
716 mlx5_hrxq_drop_release(dev);
717 else if (flow->fate & MLX5_FLOW_FATE_QUEUE)
718 mlx5_hrxq_release(dev, flow->verbs.hrxq);
719 flow->verbs.hrxq = NULL;
727 * Pointer to Ethernet device structure.
728 * @param[in, out] flow
729 * Pointer to flow structure.
731 * Pointer to error structure.
734 * 0 on success, a negative errno value otherwise and rte_errno is set.
737 mlx5_flow_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
738 struct rte_flow_error *error)
740 if (flow->fate & MLX5_FLOW_FATE_DROP) {
741 flow->verbs.hrxq = mlx5_hrxq_drop_new(dev);
742 if (!flow->verbs.hrxq)
743 return rte_flow_error_set
745 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
747 "cannot allocate Drop queue");
748 } else if (flow->fate & MLX5_FLOW_FATE_QUEUE) {
749 struct mlx5_hrxq *hrxq;
751 hrxq = mlx5_hrxq_get(dev, rss_hash_default_key,
752 rss_hash_default_key_len, 0,
753 &flow->queue, 1, 0, 0);
755 hrxq = mlx5_hrxq_new(dev, rss_hash_default_key,
756 rss_hash_default_key_len, 0,
757 &flow->queue, 1, 0, 0);
759 return rte_flow_error_set(error, rte_errno,
760 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
762 "cannot create flow");
763 flow->verbs.hrxq = hrxq;
766 mlx5_glue->create_flow(flow->verbs.hrxq->qp, flow->verbs.attr);
767 if (!flow->verbs.flow) {
768 if (flow->fate & MLX5_FLOW_FATE_DROP)
769 mlx5_hrxq_drop_release(dev);
771 mlx5_hrxq_release(dev, flow->verbs.hrxq);
772 flow->verbs.hrxq = NULL;
773 return rte_flow_error_set(error, errno,
774 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
776 "kernel module refuses to create"
783 * Create a flow and add it to @p list.
786 * Pointer to Ethernet device.
788 * Pointer to a TAILQ flow list.
790 * Flow rule attributes.
792 * Pattern specification (list terminated by the END pattern item).
794 * Associated actions (list terminated by the END action).
796 * Perform verbose error reporting if not NULL.
799 * A flow on success, NULL otherwise and rte_errno is set.
801 static struct rte_flow *
802 mlx5_flow_list_create(struct rte_eth_dev *dev,
803 struct mlx5_flows *list,
804 const struct rte_flow_attr *attr,
805 const struct rte_flow_item items[],
806 const struct rte_flow_action actions[],
807 struct rte_flow_error *error)
809 struct rte_flow *flow;
813 ret = mlx5_flow_merge(dev, NULL, 0, attr, items, actions, error);
817 flow = rte_zmalloc(__func__, size, 0);
819 rte_flow_error_set(error, ENOMEM,
820 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
822 "cannot allocate memory");
825 flow->verbs.attr = (struct ibv_flow_attr *)(flow + 1);
826 flow->verbs.specs = (uint8_t *)(flow->verbs.attr + 1);
827 ret = mlx5_flow_merge(dev, flow, size, attr, items, actions, error);
830 assert((size_t)ret == size);
831 if (dev->data->dev_started) {
832 ret = mlx5_flow_apply(dev, flow, error);
836 TAILQ_INSERT_TAIL(list, flow, next);
839 ret = rte_errno; /* Save rte_errno before cleanup. */
840 mlx5_flow_remove(dev, flow);
842 rte_errno = ret; /* Restore rte_errno. */
849 * @see rte_flow_create()
853 mlx5_flow_create(struct rte_eth_dev *dev,
854 const struct rte_flow_attr *attr,
855 const struct rte_flow_item items[],
856 const struct rte_flow_action actions[],
857 struct rte_flow_error *error)
859 return mlx5_flow_list_create
860 (dev, &((struct priv *)dev->data->dev_private)->flows,
861 attr, items, actions, error);
865 * Destroy a flow in a list.
868 * Pointer to Ethernet device.
870 * Pointer to a TAILQ flow list.
875 mlx5_flow_list_destroy(struct rte_eth_dev *dev, struct mlx5_flows *list,
876 struct rte_flow *flow)
878 mlx5_flow_remove(dev, flow);
879 TAILQ_REMOVE(list, flow, next);
887 * Pointer to Ethernet device.
889 * Pointer to a TAILQ flow list.
892 mlx5_flow_list_flush(struct rte_eth_dev *dev, struct mlx5_flows *list)
894 while (!TAILQ_EMPTY(list)) {
895 struct rte_flow *flow;
897 flow = TAILQ_FIRST(list);
898 mlx5_flow_list_destroy(dev, list, flow);
906 * Pointer to Ethernet device.
908 * Pointer to a TAILQ flow list.
911 mlx5_flow_stop(struct rte_eth_dev *dev, struct mlx5_flows *list)
913 struct rte_flow *flow;
915 TAILQ_FOREACH_REVERSE(flow, list, mlx5_flows, next)
916 mlx5_flow_remove(dev, flow);
923 * Pointer to Ethernet device.
925 * Pointer to a TAILQ flow list.
928 * 0 on success, a negative errno value otherwise and rte_errno is set.
931 mlx5_flow_start(struct rte_eth_dev *dev, struct mlx5_flows *list)
933 struct rte_flow *flow;
934 struct rte_flow_error error;
937 TAILQ_FOREACH(flow, list, next) {
938 ret = mlx5_flow_apply(dev, flow, &error);
944 ret = rte_errno; /* Save rte_errno before cleanup. */
945 mlx5_flow_stop(dev, list);
946 rte_errno = ret; /* Restore rte_errno. */
951 * Verify the flow list is empty
954 * Pointer to Ethernet device.
956 * @return the number of flows not released.
959 mlx5_flow_verify(struct rte_eth_dev *dev)
961 struct priv *priv = dev->data->dev_private;
962 struct rte_flow *flow;
965 TAILQ_FOREACH(flow, &priv->flows, next) {
966 DRV_LOG(DEBUG, "port %u flow %p still referenced",
967 dev->data->port_id, (void *)flow);
974 * Enable a control flow configured from the control plane.
977 * Pointer to Ethernet device.
979 * An Ethernet flow spec to apply.
981 * An Ethernet flow mask to apply.
983 * A VLAN flow spec to apply.
985 * A VLAN flow mask to apply.
988 * 0 on success, a negative errno value otherwise and rte_errno is set.
991 mlx5_ctrl_flow_vlan(struct rte_eth_dev *dev,
992 struct rte_flow_item_eth *eth_spec,
993 struct rte_flow_item_eth *eth_mask,
994 struct rte_flow_item_vlan *vlan_spec,
995 struct rte_flow_item_vlan *vlan_mask)
997 struct priv *priv = dev->data->dev_private;
998 const struct rte_flow_attr attr = {
1000 .priority = priv->config.flow_prio - 1,
1002 struct rte_flow_item items[] = {
1004 .type = RTE_FLOW_ITEM_TYPE_ETH,
1010 .type = (vlan_spec) ? RTE_FLOW_ITEM_TYPE_VLAN :
1011 RTE_FLOW_ITEM_TYPE_END,
1017 .type = RTE_FLOW_ITEM_TYPE_END,
1020 uint16_t queue[priv->reta_idx_n];
1021 struct rte_flow_action_rss action_rss = {
1022 .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
1024 .types = priv->rss_conf.rss_hf,
1025 .key_len = priv->rss_conf.rss_key_len,
1026 .queue_num = priv->reta_idx_n,
1027 .key = priv->rss_conf.rss_key,
1030 struct rte_flow_action actions[] = {
1032 .type = RTE_FLOW_ACTION_TYPE_RSS,
1033 .conf = &action_rss,
1036 .type = RTE_FLOW_ACTION_TYPE_END,
1039 struct rte_flow *flow;
1040 struct rte_flow_error error;
1043 if (!priv->reta_idx_n) {
1047 for (i = 0; i != priv->reta_idx_n; ++i)
1048 queue[i] = (*priv->reta_idx)[i];
1049 flow = mlx5_flow_list_create(dev, &priv->ctrl_flows, &attr, items,
1057 * Enable a flow control configured from the control plane.
1060 * Pointer to Ethernet device.
1062 * An Ethernet flow spec to apply.
1064 * An Ethernet flow mask to apply.
1067 * 0 on success, a negative errno value otherwise and rte_errno is set.
1070 mlx5_ctrl_flow(struct rte_eth_dev *dev,
1071 struct rte_flow_item_eth *eth_spec,
1072 struct rte_flow_item_eth *eth_mask)
1074 return mlx5_ctrl_flow_vlan(dev, eth_spec, eth_mask, NULL, NULL);
1080 * @see rte_flow_destroy()
1084 mlx5_flow_destroy(struct rte_eth_dev *dev,
1085 struct rte_flow *flow,
1086 struct rte_flow_error *error __rte_unused)
1088 struct priv *priv = dev->data->dev_private;
1090 mlx5_flow_list_destroy(dev, &priv->flows, flow);
1095 * Destroy all flows.
1097 * @see rte_flow_flush()
1101 mlx5_flow_flush(struct rte_eth_dev *dev,
1102 struct rte_flow_error *error __rte_unused)
1104 struct priv *priv = dev->data->dev_private;
1106 mlx5_flow_list_flush(dev, &priv->flows);
1113 * @see rte_flow_isolate()
1117 mlx5_flow_isolate(struct rte_eth_dev *dev,
1119 struct rte_flow_error *error)
1121 struct priv *priv = dev->data->dev_private;
1123 if (dev->data->dev_started) {
1124 rte_flow_error_set(error, EBUSY,
1125 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1127 "port must be stopped first");
1130 priv->isolated = !!enable;
1132 dev->dev_ops = &mlx5_dev_ops_isolate;
1134 dev->dev_ops = &mlx5_dev_ops;
1139 * Convert a flow director filter to a generic flow.
1142 * Pointer to Ethernet device.
1143 * @param fdir_filter
1144 * Flow director filter to add.
1146 * Generic flow parameters structure.
1149 * 0 on success, a negative errno value otherwise and rte_errno is set.
1152 mlx5_fdir_filter_convert(struct rte_eth_dev *dev,
1153 const struct rte_eth_fdir_filter *fdir_filter,
1154 struct mlx5_fdir *attributes)
1156 struct priv *priv = dev->data->dev_private;
1157 const struct rte_eth_fdir_input *input = &fdir_filter->input;
1158 const struct rte_eth_fdir_masks *mask =
1159 &dev->data->dev_conf.fdir_conf.mask;
1161 /* Validate queue number. */
1162 if (fdir_filter->action.rx_queue >= priv->rxqs_n) {
1163 DRV_LOG(ERR, "port %u invalid queue number %d",
1164 dev->data->port_id, fdir_filter->action.rx_queue);
1168 attributes->attr.ingress = 1;
1169 attributes->items[0] = (struct rte_flow_item) {
1170 .type = RTE_FLOW_ITEM_TYPE_ETH,
1171 .spec = &attributes->l2,
1172 .mask = &attributes->l2_mask,
1174 switch (fdir_filter->action.behavior) {
1175 case RTE_ETH_FDIR_ACCEPT:
1176 attributes->actions[0] = (struct rte_flow_action){
1177 .type = RTE_FLOW_ACTION_TYPE_QUEUE,
1178 .conf = &attributes->queue,
1181 case RTE_ETH_FDIR_REJECT:
1182 attributes->actions[0] = (struct rte_flow_action){
1183 .type = RTE_FLOW_ACTION_TYPE_DROP,
1187 DRV_LOG(ERR, "port %u invalid behavior %d",
1189 fdir_filter->action.behavior);
1190 rte_errno = ENOTSUP;
1193 attributes->queue.index = fdir_filter->action.rx_queue;
1195 switch (fdir_filter->input.flow_type) {
1196 case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
1197 case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
1198 case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
1199 attributes->l3.ipv4.hdr = (struct ipv4_hdr){
1200 .src_addr = input->flow.ip4_flow.src_ip,
1201 .dst_addr = input->flow.ip4_flow.dst_ip,
1202 .time_to_live = input->flow.ip4_flow.ttl,
1203 .type_of_service = input->flow.ip4_flow.tos,
1204 .next_proto_id = input->flow.ip4_flow.proto,
1206 attributes->l3_mask.ipv4.hdr = (struct ipv4_hdr){
1207 .src_addr = mask->ipv4_mask.src_ip,
1208 .dst_addr = mask->ipv4_mask.dst_ip,
1209 .time_to_live = mask->ipv4_mask.ttl,
1210 .type_of_service = mask->ipv4_mask.tos,
1211 .next_proto_id = mask->ipv4_mask.proto,
1213 attributes->items[1] = (struct rte_flow_item){
1214 .type = RTE_FLOW_ITEM_TYPE_IPV4,
1215 .spec = &attributes->l3,
1216 .mask = &attributes->l3_mask,
1219 case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
1220 case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
1221 case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
1222 attributes->l3.ipv6.hdr = (struct ipv6_hdr){
1223 .hop_limits = input->flow.ipv6_flow.hop_limits,
1224 .proto = input->flow.ipv6_flow.proto,
1227 memcpy(attributes->l3.ipv6.hdr.src_addr,
1228 input->flow.ipv6_flow.src_ip,
1229 RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
1230 memcpy(attributes->l3.ipv6.hdr.dst_addr,
1231 input->flow.ipv6_flow.dst_ip,
1232 RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
1233 memcpy(attributes->l3_mask.ipv6.hdr.src_addr,
1234 mask->ipv6_mask.src_ip,
1235 RTE_DIM(attributes->l3_mask.ipv6.hdr.src_addr));
1236 memcpy(attributes->l3_mask.ipv6.hdr.dst_addr,
1237 mask->ipv6_mask.dst_ip,
1238 RTE_DIM(attributes->l3_mask.ipv6.hdr.src_addr));
1239 attributes->items[1] = (struct rte_flow_item){
1240 .type = RTE_FLOW_ITEM_TYPE_IPV6,
1241 .spec = &attributes->l3,
1242 .mask = &attributes->l3_mask,
1246 DRV_LOG(ERR, "port %u invalid flow type%d",
1247 dev->data->port_id, fdir_filter->input.flow_type);
1248 rte_errno = ENOTSUP;
1252 switch (fdir_filter->input.flow_type) {
1253 case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
1254 attributes->l4.udp.hdr = (struct udp_hdr){
1255 .src_port = input->flow.udp4_flow.src_port,
1256 .dst_port = input->flow.udp4_flow.dst_port,
1258 attributes->l4_mask.udp.hdr = (struct udp_hdr){
1259 .src_port = mask->src_port_mask,
1260 .dst_port = mask->dst_port_mask,
1262 attributes->items[2] = (struct rte_flow_item){
1263 .type = RTE_FLOW_ITEM_TYPE_UDP,
1264 .spec = &attributes->l4,
1265 .mask = &attributes->l4_mask,
1268 case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
1269 attributes->l4.tcp.hdr = (struct tcp_hdr){
1270 .src_port = input->flow.tcp4_flow.src_port,
1271 .dst_port = input->flow.tcp4_flow.dst_port,
1273 attributes->l4_mask.tcp.hdr = (struct tcp_hdr){
1274 .src_port = mask->src_port_mask,
1275 .dst_port = mask->dst_port_mask,
1277 attributes->items[2] = (struct rte_flow_item){
1278 .type = RTE_FLOW_ITEM_TYPE_TCP,
1279 .spec = &attributes->l4,
1280 .mask = &attributes->l4_mask,
1283 case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
1284 attributes->l4.udp.hdr = (struct udp_hdr){
1285 .src_port = input->flow.udp6_flow.src_port,
1286 .dst_port = input->flow.udp6_flow.dst_port,
1288 attributes->l4_mask.udp.hdr = (struct udp_hdr){
1289 .src_port = mask->src_port_mask,
1290 .dst_port = mask->dst_port_mask,
1292 attributes->items[2] = (struct rte_flow_item){
1293 .type = RTE_FLOW_ITEM_TYPE_UDP,
1294 .spec = &attributes->l4,
1295 .mask = &attributes->l4_mask,
1298 case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
1299 attributes->l4.tcp.hdr = (struct tcp_hdr){
1300 .src_port = input->flow.tcp6_flow.src_port,
1301 .dst_port = input->flow.tcp6_flow.dst_port,
1303 attributes->l4_mask.tcp.hdr = (struct tcp_hdr){
1304 .src_port = mask->src_port_mask,
1305 .dst_port = mask->dst_port_mask,
1307 attributes->items[2] = (struct rte_flow_item){
1308 .type = RTE_FLOW_ITEM_TYPE_TCP,
1309 .spec = &attributes->l4,
1310 .mask = &attributes->l4_mask,
1313 case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
1314 case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
1317 DRV_LOG(ERR, "port %u invalid flow type%d",
1318 dev->data->port_id, fdir_filter->input.flow_type);
1319 rte_errno = ENOTSUP;
1326 * Add new flow director filter and store it in list.
1329 * Pointer to Ethernet device.
1330 * @param fdir_filter
1331 * Flow director filter to add.
1334 * 0 on success, a negative errno value otherwise and rte_errno is set.
1337 mlx5_fdir_filter_add(struct rte_eth_dev *dev,
1338 const struct rte_eth_fdir_filter *fdir_filter)
1340 struct priv *priv = dev->data->dev_private;
1341 struct mlx5_fdir attributes = {
1344 .dst.addr_bytes = "\x00\x00\x00\x00\x00\x00",
1345 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
1349 struct rte_flow_error error;
1350 struct rte_flow *flow;
1353 ret = mlx5_fdir_filter_convert(dev, fdir_filter, &attributes);
1356 flow = mlx5_flow_list_create(dev, &priv->flows, &attributes.attr,
1357 attributes.items, attributes.actions,
1360 DRV_LOG(DEBUG, "port %u FDIR created %p", dev->data->port_id,
1368 * Delete specific filter.
1371 * Pointer to Ethernet device.
1372 * @param fdir_filter
1373 * Filter to be deleted.
1376 * 0 on success, a negative errno value otherwise and rte_errno is set.
1379 mlx5_fdir_filter_delete(struct rte_eth_dev *dev __rte_unused,
1380 const struct rte_eth_fdir_filter *fdir_filter
1383 rte_errno = ENOTSUP;
1388 * Update queue for specific filter.
1391 * Pointer to Ethernet device.
1392 * @param fdir_filter
1393 * Filter to be updated.
1396 * 0 on success, a negative errno value otherwise and rte_errno is set.
1399 mlx5_fdir_filter_update(struct rte_eth_dev *dev,
1400 const struct rte_eth_fdir_filter *fdir_filter)
1404 ret = mlx5_fdir_filter_delete(dev, fdir_filter);
1407 return mlx5_fdir_filter_add(dev, fdir_filter);
1411 * Flush all filters.
1414 * Pointer to Ethernet device.
1417 mlx5_fdir_filter_flush(struct rte_eth_dev *dev)
1419 struct priv *priv = dev->data->dev_private;
1421 mlx5_flow_list_flush(dev, &priv->flows);
1425 * Get flow director information.
1428 * Pointer to Ethernet device.
1429 * @param[out] fdir_info
1430 * Resulting flow director information.
1433 mlx5_fdir_info_get(struct rte_eth_dev *dev, struct rte_eth_fdir_info *fdir_info)
1435 struct rte_eth_fdir_masks *mask =
1436 &dev->data->dev_conf.fdir_conf.mask;
1438 fdir_info->mode = dev->data->dev_conf.fdir_conf.mode;
1439 fdir_info->guarant_spc = 0;
1440 rte_memcpy(&fdir_info->mask, mask, sizeof(fdir_info->mask));
1441 fdir_info->max_flexpayload = 0;
1442 fdir_info->flow_types_mask[0] = 0;
1443 fdir_info->flex_payload_unit = 0;
1444 fdir_info->max_flex_payload_segment_num = 0;
1445 fdir_info->flex_payload_limit = 0;
1446 memset(&fdir_info->flex_conf, 0, sizeof(fdir_info->flex_conf));
1450 * Deal with flow director operations.
1453 * Pointer to Ethernet device.
1455 * Operation to perform.
1457 * Pointer to operation-specific structure.
1460 * 0 on success, a negative errno value otherwise and rte_errno is set.
1463 mlx5_fdir_ctrl_func(struct rte_eth_dev *dev, enum rte_filter_op filter_op,
1466 enum rte_fdir_mode fdir_mode =
1467 dev->data->dev_conf.fdir_conf.mode;
1469 if (filter_op == RTE_ETH_FILTER_NOP)
1471 if (fdir_mode != RTE_FDIR_MODE_PERFECT &&
1472 fdir_mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
1473 DRV_LOG(ERR, "port %u flow director mode %d not supported",
1474 dev->data->port_id, fdir_mode);
1478 switch (filter_op) {
1479 case RTE_ETH_FILTER_ADD:
1480 return mlx5_fdir_filter_add(dev, arg);
1481 case RTE_ETH_FILTER_UPDATE:
1482 return mlx5_fdir_filter_update(dev, arg);
1483 case RTE_ETH_FILTER_DELETE:
1484 return mlx5_fdir_filter_delete(dev, arg);
1485 case RTE_ETH_FILTER_FLUSH:
1486 mlx5_fdir_filter_flush(dev);
1488 case RTE_ETH_FILTER_INFO:
1489 mlx5_fdir_info_get(dev, arg);
1492 DRV_LOG(DEBUG, "port %u unknown operation %u",
1493 dev->data->port_id, filter_op);
1501 * Manage filter operations.
1504 * Pointer to Ethernet device structure.
1505 * @param filter_type
1508 * Operation to perform.
1510 * Pointer to operation-specific structure.
1513 * 0 on success, a negative errno value otherwise and rte_errno is set.
1516 mlx5_dev_filter_ctrl(struct rte_eth_dev *dev,
1517 enum rte_filter_type filter_type,
1518 enum rte_filter_op filter_op,
1521 switch (filter_type) {
1522 case RTE_ETH_FILTER_GENERIC:
1523 if (filter_op != RTE_ETH_FILTER_GET) {
1527 *(const void **)arg = &mlx5_flow_ops;
1529 case RTE_ETH_FILTER_FDIR:
1530 return mlx5_fdir_ctrl_func(dev, filter_op, arg);
1532 DRV_LOG(ERR, "port %u filter type (%d) not supported",
1533 dev->data->port_id, filter_type);
1534 rte_errno = ENOTSUP;