summaryrefslogtreecommitdiff
path: root/drivers/net/failsafe/failsafe_flow.c
blob: 5e2b5f7c67575abf75d11132bda7537b091f67a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright 2017 6WIND S.A.
 * Copyright 2017 Mellanox Technologies, Ltd
 */

#include <stddef.h>
#include <string.h>
#include <sys/queue.h>

#include <rte_errno.h>
#include <rte_malloc.h>
#include <rte_tailq.h>
#include <rte_flow.h>
#include <rte_flow_driver.h>

#include "failsafe_private.h"

static struct rte_flow *
fs_flow_allocate(const struct rte_flow_attr *attr,
		 const struct rte_flow_item *items,
		 const struct rte_flow_action *actions)
{
	struct rte_flow *flow;
	const struct rte_flow_conv_rule rule = {
		.attr_ro = attr,
		.pattern_ro = items,
		.actions_ro = actions,
	};
	struct rte_flow_error error;
	int ret;

	ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, NULL, 0, &rule, &error);
	if (ret < 0) {
		ERROR("Unable to process flow rule (%s): %s",
		      error.message ? error.message : "unspecified",
		      strerror(rte_errno));
		return NULL;
	}
	flow = rte_zmalloc(NULL, offsetof(struct rte_flow, rule) + ret,
			   RTE_CACHE_LINE_SIZE);
	if (flow == NULL) {
		ERROR("Could not allocate new flow");
		return NULL;
	}
	ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, &flow->rule, ret, &rule,
			    &error);
	if (ret < 0) {
		ERROR("Failed to copy flow rule (%s): %s",
		      error.message ? error.message : "unspecified",
		      strerror(rte_errno));
		rte_free(flow);
		return NULL;
	}
	return flow;
}

static void
fs_flow_release(struct rte_flow **flow)
{
	rte_free(*flow);
	*flow = NULL;
}

static int
fs_flow_validate(struct rte_eth_dev *dev,
		 const struct rte_flow_attr *attr,
		 const struct rte_flow_item patterns[],
		 const struct rte_flow_action actions[],
		 struct rte_flow_error *error)
{
	struct sub_device *sdev;
	uint8_t i;
	int ret;

	fs_lock(dev, 0);
	FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
		DEBUG("Calling rte_flow_validate on sub_device %d", i);
		ret = rte_flow_validate(PORT_ID(sdev),
				attr, patterns, actions, error);
		if ((ret = fs_err(sdev, ret))) {
			ERROR("Operation rte_flow_validate failed for sub_device %d"
			      " with error %d", i, ret);
			fs_unlock(dev, 0);
			return ret;
		}
	}
	fs_unlock(dev, 0);
	return 0;
}

static struct rte_flow *
fs_flow_create(struct rte_eth_dev *dev,
	       const struct rte_flow_attr *attr,
	       const struct rte_flow_item patterns[],
	       const struct rte_flow_action actions[],
	       struct rte_flow_error *error)
{
	struct sub_device *sdev;
	struct rte_flow *flow;
	uint8_t i;

	fs_lock(dev, 0);
	flow = fs_flow_allocate(attr, patterns, actions);
	FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
		flow->flows[i] = rte_flow_create(PORT_ID(sdev),
				attr, patterns, actions, error);
		if (flow->flows[i] == NULL && fs_err(sdev, -rte_errno)) {
			ERROR("Failed to create flow on sub_device %d",
				i);
			goto err;
		}
	}
	TAILQ_INSERT_TAIL(&PRIV(dev)->flow_list, flow, next);
	fs_unlock(dev, 0);
	return flow;
err:
	FOREACH_SUBDEV(sdev, i, dev) {
		if (flow->flows[i] != NULL)
			rte_flow_destroy(PORT_ID(sdev),
				flow->flows[i], error);
	}
	fs_flow_release(&flow);
	fs_unlock(dev, 0);
	return NULL;
}

static int
fs_flow_destroy(struct rte_eth_dev *dev,
		struct rte_flow *flow,
		struct rte_flow_error *error)
{
	struct sub_device *sdev;
	uint8_t i;
	int ret;

	if (flow == NULL) {
		ERROR("Invalid flow");
		return -EINVAL;
	}
	ret = 0;
	fs_lock(dev, 0);
	FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
		int local_ret;

		if (flow->flows[i] == NULL)
			continue;
		local_ret = rte_flow_destroy(PORT_ID(sdev),
				flow->flows[i], error);
		if ((local_ret = fs_err(sdev, local_ret))) {
			ERROR("Failed to destroy flow on sub_device %d: %d",
					i, local_ret);
			if (ret == 0)
				ret = local_ret;
		}
	}
	TAILQ_REMOVE(&PRIV(dev)->flow_list, flow, next);
	fs_flow_release(&flow);
	fs_unlock(dev, 0);
	return ret;
}

static int
fs_flow_flush(struct rte_eth_dev *dev,
	      struct rte_flow_error *error)
{
	struct sub_device *sdev;
	struct rte_flow *flow;
	void *tmp;
	uint8_t i;
	int ret;

	fs_lock(dev, 0);
	FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) {
		DEBUG("Calling rte_flow_flush on sub_device %d", i);
		ret = rte_flow_flush(PORT_ID(sdev), error);
		if ((ret = fs_err(sdev, ret))) {
			ERROR("Operation rte_flow_flush failed for sub_device %d"
			      " with error %d", i, ret);
			fs_unlock(dev, 0);
			return ret;
		}
	}
	TAILQ_FOREACH_SAFE(flow, &PRIV(dev)->flow_list, next, tmp) {
		TAILQ_REMOVE(&PRIV(dev)->flow_list, flow, next);
		fs_flow_release(&flow);
	}
	fs_unlock(dev, 0);
	return 0;
}

static int
fs_flow_query(struct rte_eth_dev *dev,
	      struct rte_flow *flow,
	      const struct rte_flow_action *action,
	      void *arg,
	      struct rte_flow_error *error)
{
	struct sub_device *sdev;

	fs_lock(dev, 0);
	sdev = TX_SUBDEV(dev);
	if (sdev != NULL) {
		int ret = rte_flow_query(PORT_ID(sdev),
					 flow->flows[SUB_ID(sdev)],
					 action, arg, error);

		if ((ret = fs_err(sdev, ret))) {
			fs_unlock(dev, 0);
			return ret;
		}
	}
	fs_unlock(dev, 0);
	WARN("No active sub_device to query about its flow");
	return -1;
}

static int
fs_flow_isolate(struct rte_eth_dev *dev,
		int set,
		struct rte_flow_error *error)
{
	struct sub_device *sdev;
	uint8_t i;
	int ret;

	fs_lock(dev, 0);
	FOREACH_SUBDEV(sdev, i, dev) {
		if (sdev->state < DEV_PROBED)
			continue;
		DEBUG("Calling rte_flow_isolate on sub_device %d", i);
		if (PRIV(dev)->flow_isolated != sdev->flow_isolated)
			WARN("flow isolation mode of sub_device %d in incoherent state.",
				i);
		ret = rte_flow_isolate(PORT_ID(sdev), set, error);
		if ((ret = fs_err(sdev, ret))) {
			ERROR("Operation rte_flow_isolate failed for sub_device %d"
			      " with error %d", i, ret);
			fs_unlock(dev, 0);
			return ret;
		}
		sdev->flow_isolated = set;
	}
	PRIV(dev)->flow_isolated = set;
	fs_unlock(dev, 0);
	return 0;
}

const struct rte_flow_ops fs_flow_ops = {
	.validate = fs_flow_validate,
	.create = fs_flow_create,
	.destroy = fs_flow_destroy,
	.flush = fs_flow_flush,
	.query = fs_flow_query,
	.isolate = fs_flow_isolate,
};