1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 6WIND S.A.
3 * Copyright 2017 Mellanox Technologies, Ltd
10 #include <rte_errno.h>
11 #include <rte_malloc.h>
12 #include <rte_tailq.h>
14 #include <rte_flow_driver.h>
16 #include "failsafe_private.h"
18 static struct rte_flow *
19 fs_flow_allocate(const struct rte_flow_attr *attr,
20 const struct rte_flow_item *items,
21 const struct rte_flow_action *actions)
23 struct rte_flow *flow;
24 const struct rte_flow_conv_rule rule = {
27 .actions_ro = actions,
29 struct rte_flow_error error;
32 ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, NULL, 0, &rule, &error);
34 ERROR("Unable to process flow rule (%s): %s",
35 error.message ? error.message : "unspecified",
39 flow = rte_zmalloc(NULL, offsetof(struct rte_flow, rule) + ret,
42 ERROR("Could not allocate new flow");
45 ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, &flow->rule, ret, &rule,
48 ERROR("Failed to copy flow rule (%s): %s",
49 error.message ? error.message : "unspecified",
58 fs_flow_release(struct rte_flow **flow)
65 fs_flow_validate(struct rte_eth_dev *dev,
66 const struct rte_flow_attr *attr,
67 const struct rte_flow_item patterns[],
68 const struct rte_flow_action actions[],
69 struct rte_flow_error *error)
71 struct sub_device *sdev;
76 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
77 DEBUG("Calling rte_flow_validate on sub_device %d", i);
78 ret = rte_flow_validate(PORT_ID(sdev),
79 attr, patterns, actions, error);
80 if ((ret = fs_err(sdev, ret))) {
81 ERROR("Operation rte_flow_validate failed for sub_device %d"
82 " with error %d", i, ret);
91 static struct rte_flow *
92 fs_flow_create(struct rte_eth_dev *dev,
93 const struct rte_flow_attr *attr,
94 const struct rte_flow_item patterns[],
95 const struct rte_flow_action actions[],
96 struct rte_flow_error *error)
98 struct sub_device *sdev;
99 struct rte_flow *flow;
103 flow = fs_flow_allocate(attr, patterns, actions);
104 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
105 flow->flows[i] = rte_flow_create(PORT_ID(sdev),
106 attr, patterns, actions, error);
107 if (flow->flows[i] == NULL && fs_err(sdev, -rte_errno)) {
108 ERROR("Failed to create flow on sub_device %d",
113 TAILQ_INSERT_TAIL(&PRIV(dev)->flow_list, flow, next);
117 FOREACH_SUBDEV(sdev, i, dev) {
118 if (flow->flows[i] != NULL)
119 rte_flow_destroy(PORT_ID(sdev),
120 flow->flows[i], error);
122 fs_flow_release(&flow);
128 fs_flow_destroy(struct rte_eth_dev *dev,
129 struct rte_flow *flow,
130 struct rte_flow_error *error)
132 struct sub_device *sdev;
137 ERROR("Invalid flow");
142 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
145 if (flow->flows[i] == NULL)
147 local_ret = rte_flow_destroy(PORT_ID(sdev),
148 flow->flows[i], error);
149 if ((local_ret = fs_err(sdev, local_ret))) {
150 ERROR("Failed to destroy flow on sub_device %d: %d",
156 TAILQ_REMOVE(&PRIV(dev)->flow_list, flow, next);
157 fs_flow_release(&flow);
163 fs_flow_flush(struct rte_eth_dev *dev,
164 struct rte_flow_error *error)
166 struct sub_device *sdev;
167 struct rte_flow *flow;
173 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
174 DEBUG("Calling rte_flow_flush on sub_device %d", i);
175 ret = rte_flow_flush(PORT_ID(sdev), error);
176 if ((ret = fs_err(sdev, ret))) {
177 ERROR("Operation rte_flow_flush failed for sub_device %d"
178 " with error %d", i, ret);
183 TAILQ_FOREACH_SAFE(flow, &PRIV(dev)->flow_list, next, tmp) {
184 TAILQ_REMOVE(&PRIV(dev)->flow_list, flow, next);
185 fs_flow_release(&flow);
192 fs_flow_query(struct rte_eth_dev *dev,
193 struct rte_flow *flow,
194 const struct rte_flow_action *action,
196 struct rte_flow_error *error)
198 struct sub_device *sdev;
201 sdev = TX_SUBDEV(dev);
203 int ret = rte_flow_query(PORT_ID(sdev),
204 flow->flows[SUB_ID(sdev)],
207 if ((ret = fs_err(sdev, ret))) {
213 WARN("No active sub_device to query about its flow");
218 fs_flow_isolate(struct rte_eth_dev *dev,
220 struct rte_flow_error *error)
222 struct sub_device *sdev;
227 FOREACH_SUBDEV(sdev, i, dev) {
228 if (sdev->state < DEV_PROBED)
230 DEBUG("Calling rte_flow_isolate on sub_device %d", i);
231 if (PRIV(dev)->flow_isolated != sdev->flow_isolated)
232 WARN("flow isolation mode of sub_device %d in incoherent state.",
234 ret = rte_flow_isolate(PORT_ID(sdev), set, error);
235 if ((ret = fs_err(sdev, ret))) {
236 ERROR("Operation rte_flow_isolate failed for sub_device %d"
237 " with error %d", i, ret);
241 sdev->flow_isolated = set;
243 PRIV(dev)->flow_isolated = set;
248 const struct rte_flow_ops fs_flow_ops = {
249 .validate = fs_flow_validate,
250 .create = fs_flow_create,
251 .destroy = fs_flow_destroy,
252 .flush = fs_flow_flush,
253 .query = fs_flow_query,
254 .isolate = fs_flow_isolate,