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 __rte_unused,
912 struct mlx5_flows *list __rte_unused)
920 * Pointer to Ethernet device.
922 * Pointer to a TAILQ flow list.
925 * 0 on success, a negative errno value otherwise and rte_errno is set.
928 mlx5_flow_start(struct rte_eth_dev *dev __rte_unused,
929 struct mlx5_flows *list __rte_unused)
935 * Verify the flow list is empty
938 * Pointer to Ethernet device.
940 * @return the number of flows not released.
943 mlx5_flow_verify(struct rte_eth_dev *dev)
945 struct priv *priv = dev->data->dev_private;
946 struct rte_flow *flow;
949 TAILQ_FOREACH(flow, &priv->flows, next) {
950 DRV_LOG(DEBUG, "port %u flow %p still referenced",
951 dev->data->port_id, (void *)flow);
958 * Enable a control flow configured from the control plane.
961 * Pointer to Ethernet device.
963 * An Ethernet flow spec to apply.
965 * An Ethernet flow mask to apply.
967 * A VLAN flow spec to apply.
969 * A VLAN flow mask to apply.
972 * 0 on success, a negative errno value otherwise and rte_errno is set.
975 mlx5_ctrl_flow_vlan(struct rte_eth_dev *dev,
976 struct rte_flow_item_eth *eth_spec,
977 struct rte_flow_item_eth *eth_mask,
978 struct rte_flow_item_vlan *vlan_spec,
979 struct rte_flow_item_vlan *vlan_mask)
981 struct priv *priv = dev->data->dev_private;
982 const struct rte_flow_attr attr = {
984 .priority = priv->config.flow_prio - 1,
986 struct rte_flow_item items[] = {
988 .type = RTE_FLOW_ITEM_TYPE_ETH,
994 .type = (vlan_spec) ? RTE_FLOW_ITEM_TYPE_VLAN :
995 RTE_FLOW_ITEM_TYPE_END,
1001 .type = RTE_FLOW_ITEM_TYPE_END,
1004 uint16_t queue[priv->reta_idx_n];
1005 struct rte_flow_action_rss action_rss = {
1006 .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
1008 .types = priv->rss_conf.rss_hf,
1009 .key_len = priv->rss_conf.rss_key_len,
1010 .queue_num = priv->reta_idx_n,
1011 .key = priv->rss_conf.rss_key,
1014 struct rte_flow_action actions[] = {
1016 .type = RTE_FLOW_ACTION_TYPE_RSS,
1017 .conf = &action_rss,
1020 .type = RTE_FLOW_ACTION_TYPE_END,
1023 struct rte_flow *flow;
1024 struct rte_flow_error error;
1027 if (!priv->reta_idx_n) {
1031 for (i = 0; i != priv->reta_idx_n; ++i)
1032 queue[i] = (*priv->reta_idx)[i];
1033 flow = mlx5_flow_list_create(dev, &priv->ctrl_flows, &attr, items,
1041 * Enable a flow control configured from the control plane.
1044 * Pointer to Ethernet device.
1046 * An Ethernet flow spec to apply.
1048 * An Ethernet flow mask to apply.
1051 * 0 on success, a negative errno value otherwise and rte_errno is set.
1054 mlx5_ctrl_flow(struct rte_eth_dev *dev,
1055 struct rte_flow_item_eth *eth_spec,
1056 struct rte_flow_item_eth *eth_mask)
1058 return mlx5_ctrl_flow_vlan(dev, eth_spec, eth_mask, NULL, NULL);
1064 * @see rte_flow_destroy()
1068 mlx5_flow_destroy(struct rte_eth_dev *dev,
1069 struct rte_flow *flow,
1070 struct rte_flow_error *error __rte_unused)
1072 struct priv *priv = dev->data->dev_private;
1074 mlx5_flow_list_destroy(dev, &priv->flows, flow);
1079 * Destroy all flows.
1081 * @see rte_flow_flush()
1085 mlx5_flow_flush(struct rte_eth_dev *dev,
1086 struct rte_flow_error *error __rte_unused)
1088 struct priv *priv = dev->data->dev_private;
1090 mlx5_flow_list_flush(dev, &priv->flows);
1097 * @see rte_flow_isolate()
1101 mlx5_flow_isolate(struct rte_eth_dev *dev,
1103 struct rte_flow_error *error)
1105 struct priv *priv = dev->data->dev_private;
1107 if (dev->data->dev_started) {
1108 rte_flow_error_set(error, EBUSY,
1109 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1111 "port must be stopped first");
1114 priv->isolated = !!enable;
1116 dev->dev_ops = &mlx5_dev_ops_isolate;
1118 dev->dev_ops = &mlx5_dev_ops;
1123 * Convert a flow director filter to a generic flow.
1126 * Pointer to Ethernet device.
1127 * @param fdir_filter
1128 * Flow director filter to add.
1130 * Generic flow parameters structure.
1133 * 0 on success, a negative errno value otherwise and rte_errno is set.
1136 mlx5_fdir_filter_convert(struct rte_eth_dev *dev,
1137 const struct rte_eth_fdir_filter *fdir_filter,
1138 struct mlx5_fdir *attributes)
1140 struct priv *priv = dev->data->dev_private;
1141 const struct rte_eth_fdir_input *input = &fdir_filter->input;
1142 const struct rte_eth_fdir_masks *mask =
1143 &dev->data->dev_conf.fdir_conf.mask;
1145 /* Validate queue number. */
1146 if (fdir_filter->action.rx_queue >= priv->rxqs_n) {
1147 DRV_LOG(ERR, "port %u invalid queue number %d",
1148 dev->data->port_id, fdir_filter->action.rx_queue);
1152 attributes->attr.ingress = 1;
1153 attributes->items[0] = (struct rte_flow_item) {
1154 .type = RTE_FLOW_ITEM_TYPE_ETH,
1155 .spec = &attributes->l2,
1156 .mask = &attributes->l2_mask,
1158 switch (fdir_filter->action.behavior) {
1159 case RTE_ETH_FDIR_ACCEPT:
1160 attributes->actions[0] = (struct rte_flow_action){
1161 .type = RTE_FLOW_ACTION_TYPE_QUEUE,
1162 .conf = &attributes->queue,
1165 case RTE_ETH_FDIR_REJECT:
1166 attributes->actions[0] = (struct rte_flow_action){
1167 .type = RTE_FLOW_ACTION_TYPE_DROP,
1171 DRV_LOG(ERR, "port %u invalid behavior %d",
1173 fdir_filter->action.behavior);
1174 rte_errno = ENOTSUP;
1177 attributes->queue.index = fdir_filter->action.rx_queue;
1179 switch (fdir_filter->input.flow_type) {
1180 case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
1181 case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
1182 case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
1183 attributes->l3.ipv4.hdr = (struct ipv4_hdr){
1184 .src_addr = input->flow.ip4_flow.src_ip,
1185 .dst_addr = input->flow.ip4_flow.dst_ip,
1186 .time_to_live = input->flow.ip4_flow.ttl,
1187 .type_of_service = input->flow.ip4_flow.tos,
1188 .next_proto_id = input->flow.ip4_flow.proto,
1190 attributes->l3_mask.ipv4.hdr = (struct ipv4_hdr){
1191 .src_addr = mask->ipv4_mask.src_ip,
1192 .dst_addr = mask->ipv4_mask.dst_ip,
1193 .time_to_live = mask->ipv4_mask.ttl,
1194 .type_of_service = mask->ipv4_mask.tos,
1195 .next_proto_id = mask->ipv4_mask.proto,
1197 attributes->items[1] = (struct rte_flow_item){
1198 .type = RTE_FLOW_ITEM_TYPE_IPV4,
1199 .spec = &attributes->l3,
1200 .mask = &attributes->l3_mask,
1203 case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
1204 case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
1205 case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
1206 attributes->l3.ipv6.hdr = (struct ipv6_hdr){
1207 .hop_limits = input->flow.ipv6_flow.hop_limits,
1208 .proto = input->flow.ipv6_flow.proto,
1211 memcpy(attributes->l3.ipv6.hdr.src_addr,
1212 input->flow.ipv6_flow.src_ip,
1213 RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
1214 memcpy(attributes->l3.ipv6.hdr.dst_addr,
1215 input->flow.ipv6_flow.dst_ip,
1216 RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
1217 memcpy(attributes->l3_mask.ipv6.hdr.src_addr,
1218 mask->ipv6_mask.src_ip,
1219 RTE_DIM(attributes->l3_mask.ipv6.hdr.src_addr));
1220 memcpy(attributes->l3_mask.ipv6.hdr.dst_addr,
1221 mask->ipv6_mask.dst_ip,
1222 RTE_DIM(attributes->l3_mask.ipv6.hdr.src_addr));
1223 attributes->items[1] = (struct rte_flow_item){
1224 .type = RTE_FLOW_ITEM_TYPE_IPV6,
1225 .spec = &attributes->l3,
1226 .mask = &attributes->l3_mask,
1230 DRV_LOG(ERR, "port %u invalid flow type%d",
1231 dev->data->port_id, fdir_filter->input.flow_type);
1232 rte_errno = ENOTSUP;
1236 switch (fdir_filter->input.flow_type) {
1237 case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
1238 attributes->l4.udp.hdr = (struct udp_hdr){
1239 .src_port = input->flow.udp4_flow.src_port,
1240 .dst_port = input->flow.udp4_flow.dst_port,
1242 attributes->l4_mask.udp.hdr = (struct udp_hdr){
1243 .src_port = mask->src_port_mask,
1244 .dst_port = mask->dst_port_mask,
1246 attributes->items[2] = (struct rte_flow_item){
1247 .type = RTE_FLOW_ITEM_TYPE_UDP,
1248 .spec = &attributes->l4,
1249 .mask = &attributes->l4_mask,
1252 case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
1253 attributes->l4.tcp.hdr = (struct tcp_hdr){
1254 .src_port = input->flow.tcp4_flow.src_port,
1255 .dst_port = input->flow.tcp4_flow.dst_port,
1257 attributes->l4_mask.tcp.hdr = (struct tcp_hdr){
1258 .src_port = mask->src_port_mask,
1259 .dst_port = mask->dst_port_mask,
1261 attributes->items[2] = (struct rte_flow_item){
1262 .type = RTE_FLOW_ITEM_TYPE_TCP,
1263 .spec = &attributes->l4,
1264 .mask = &attributes->l4_mask,
1267 case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
1268 attributes->l4.udp.hdr = (struct udp_hdr){
1269 .src_port = input->flow.udp6_flow.src_port,
1270 .dst_port = input->flow.udp6_flow.dst_port,
1272 attributes->l4_mask.udp.hdr = (struct udp_hdr){
1273 .src_port = mask->src_port_mask,
1274 .dst_port = mask->dst_port_mask,
1276 attributes->items[2] = (struct rte_flow_item){
1277 .type = RTE_FLOW_ITEM_TYPE_UDP,
1278 .spec = &attributes->l4,
1279 .mask = &attributes->l4_mask,
1282 case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
1283 attributes->l4.tcp.hdr = (struct tcp_hdr){
1284 .src_port = input->flow.tcp6_flow.src_port,
1285 .dst_port = input->flow.tcp6_flow.dst_port,
1287 attributes->l4_mask.tcp.hdr = (struct tcp_hdr){
1288 .src_port = mask->src_port_mask,
1289 .dst_port = mask->dst_port_mask,
1291 attributes->items[2] = (struct rte_flow_item){
1292 .type = RTE_FLOW_ITEM_TYPE_TCP,
1293 .spec = &attributes->l4,
1294 .mask = &attributes->l4_mask,
1297 case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
1298 case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
1301 DRV_LOG(ERR, "port %u invalid flow type%d",
1302 dev->data->port_id, fdir_filter->input.flow_type);
1303 rte_errno = ENOTSUP;
1310 * Add new flow director filter and store it in list.
1313 * Pointer to Ethernet device.
1314 * @param fdir_filter
1315 * Flow director filter to add.
1318 * 0 on success, a negative errno value otherwise and rte_errno is set.
1321 mlx5_fdir_filter_add(struct rte_eth_dev *dev,
1322 const struct rte_eth_fdir_filter *fdir_filter)
1324 struct priv *priv = dev->data->dev_private;
1325 struct mlx5_fdir attributes = {
1328 .dst.addr_bytes = "\x00\x00\x00\x00\x00\x00",
1329 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
1333 struct rte_flow_error error;
1334 struct rte_flow *flow;
1337 ret = mlx5_fdir_filter_convert(dev, fdir_filter, &attributes);
1340 flow = mlx5_flow_list_create(dev, &priv->flows, &attributes.attr,
1341 attributes.items, attributes.actions,
1344 DRV_LOG(DEBUG, "port %u FDIR created %p", dev->data->port_id,
1352 * Delete specific filter.
1355 * Pointer to Ethernet device.
1356 * @param fdir_filter
1357 * Filter to be deleted.
1360 * 0 on success, a negative errno value otherwise and rte_errno is set.
1363 mlx5_fdir_filter_delete(struct rte_eth_dev *dev __rte_unused,
1364 const struct rte_eth_fdir_filter *fdir_filter
1367 rte_errno = ENOTSUP;
1372 * Update queue for specific filter.
1375 * Pointer to Ethernet device.
1376 * @param fdir_filter
1377 * Filter to be updated.
1380 * 0 on success, a negative errno value otherwise and rte_errno is set.
1383 mlx5_fdir_filter_update(struct rte_eth_dev *dev,
1384 const struct rte_eth_fdir_filter *fdir_filter)
1388 ret = mlx5_fdir_filter_delete(dev, fdir_filter);
1391 return mlx5_fdir_filter_add(dev, fdir_filter);
1395 * Flush all filters.
1398 * Pointer to Ethernet device.
1401 mlx5_fdir_filter_flush(struct rte_eth_dev *dev)
1403 struct priv *priv = dev->data->dev_private;
1405 mlx5_flow_list_flush(dev, &priv->flows);
1409 * Get flow director information.
1412 * Pointer to Ethernet device.
1413 * @param[out] fdir_info
1414 * Resulting flow director information.
1417 mlx5_fdir_info_get(struct rte_eth_dev *dev, struct rte_eth_fdir_info *fdir_info)
1419 struct rte_eth_fdir_masks *mask =
1420 &dev->data->dev_conf.fdir_conf.mask;
1422 fdir_info->mode = dev->data->dev_conf.fdir_conf.mode;
1423 fdir_info->guarant_spc = 0;
1424 rte_memcpy(&fdir_info->mask, mask, sizeof(fdir_info->mask));
1425 fdir_info->max_flexpayload = 0;
1426 fdir_info->flow_types_mask[0] = 0;
1427 fdir_info->flex_payload_unit = 0;
1428 fdir_info->max_flex_payload_segment_num = 0;
1429 fdir_info->flex_payload_limit = 0;
1430 memset(&fdir_info->flex_conf, 0, sizeof(fdir_info->flex_conf));
1434 * Deal with flow director operations.
1437 * Pointer to Ethernet device.
1439 * Operation to perform.
1441 * Pointer to operation-specific structure.
1444 * 0 on success, a negative errno value otherwise and rte_errno is set.
1447 mlx5_fdir_ctrl_func(struct rte_eth_dev *dev, enum rte_filter_op filter_op,
1450 enum rte_fdir_mode fdir_mode =
1451 dev->data->dev_conf.fdir_conf.mode;
1453 if (filter_op == RTE_ETH_FILTER_NOP)
1455 if (fdir_mode != RTE_FDIR_MODE_PERFECT &&
1456 fdir_mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
1457 DRV_LOG(ERR, "port %u flow director mode %d not supported",
1458 dev->data->port_id, fdir_mode);
1462 switch (filter_op) {
1463 case RTE_ETH_FILTER_ADD:
1464 return mlx5_fdir_filter_add(dev, arg);
1465 case RTE_ETH_FILTER_UPDATE:
1466 return mlx5_fdir_filter_update(dev, arg);
1467 case RTE_ETH_FILTER_DELETE:
1468 return mlx5_fdir_filter_delete(dev, arg);
1469 case RTE_ETH_FILTER_FLUSH:
1470 mlx5_fdir_filter_flush(dev);
1472 case RTE_ETH_FILTER_INFO:
1473 mlx5_fdir_info_get(dev, arg);
1476 DRV_LOG(DEBUG, "port %u unknown operation %u",
1477 dev->data->port_id, filter_op);
1485 * Manage filter operations.
1488 * Pointer to Ethernet device structure.
1489 * @param filter_type
1492 * Operation to perform.
1494 * Pointer to operation-specific structure.
1497 * 0 on success, a negative errno value otherwise and rte_errno is set.
1500 mlx5_dev_filter_ctrl(struct rte_eth_dev *dev,
1501 enum rte_filter_type filter_type,
1502 enum rte_filter_op filter_op,
1505 switch (filter_type) {
1506 case RTE_ETH_FILTER_GENERIC:
1507 if (filter_op != RTE_ETH_FILTER_GET) {
1511 *(const void **)arg = &mlx5_flow_ops;
1513 case RTE_ETH_FILTER_FDIR:
1514 return mlx5_fdir_ctrl_func(dev, filter_op, arg);
1516 DRV_LOG(ERR, "port %u filter type (%d) not supported",
1517 dev->data->port_id, filter_type);
1518 rte_errno = ENOTSUP;