File: cpfl_flow.c

package info (click to toggle)
dpdk 25.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 127,892 kB
  • sloc: ansic: 2,358,479; python: 16,426; sh: 4,474; makefile: 1,713; awk: 70
file content (339 lines) | stat: -rw-r--r-- 7,417 bytes parent folder | download | duplicates (2)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2023 Intel Corporation
 */
#include <rte_flow_driver.h>
#include <rte_tailq.h>

#include "cpfl_flow.h"
#include "cpfl_flow_parser.h"

TAILQ_HEAD(cpfl_flow_engine_list, cpfl_flow_engine);

static struct cpfl_flow_engine_list engine_list = TAILQ_HEAD_INITIALIZER(engine_list);

void
cpfl_flow_engine_register(struct cpfl_flow_engine *engine)
{
	TAILQ_INSERT_TAIL(&engine_list, engine, node);
}

struct cpfl_flow_engine *
cpfl_flow_engine_match(struct rte_eth_dev *dev,
		       const struct rte_flow_attr *attr,
		       const struct rte_flow_item pattern[],
		       const struct rte_flow_action actions[],
		       void **meta)
{
	struct cpfl_flow_engine *engine = NULL;
	void *temp;

	RTE_TAILQ_FOREACH_SAFE(engine, &engine_list, node, temp) {
		if (!engine->parse_pattern_action)
			continue;

		if (engine->parse_pattern_action(dev, attr, pattern, actions, meta) < 0)
			continue;
		return engine;
	}

	return NULL;
}

int
cpfl_flow_engine_init(struct cpfl_adapter_ext *adapter)
{
	struct cpfl_flow_engine *engine = NULL;
	void *temp;
	int ret;

	RTE_TAILQ_FOREACH_SAFE(engine, &engine_list, node, temp) {
		if (!engine->init) {
			PMD_INIT_LOG(ERR, "Invalid engine type (%d)",
				     engine->type);
			return -ENOTSUP;
		}

		ret = engine->init(adapter);
		if (ret) {
			PMD_INIT_LOG(ERR, "Failed to initialize engine %d",
				     engine->type);
			return ret;
		}
	}

	return 0;
}

void
cpfl_flow_engine_uninit(struct cpfl_adapter_ext *adapter)
{
	struct cpfl_flow_engine *engine = NULL;
	void *temp;

	RTE_TAILQ_FOREACH_SAFE(engine, &engine_list, node, temp) {
		if (engine->uninit)
			engine->uninit(adapter);
	}
}

static int
cpfl_flow_attr_valid(const struct rte_flow_attr *attr,
		     struct rte_flow_error *error)
{
	if (attr->priority > CPFL_PREC_MAX) {
		rte_flow_error_set(error, EINVAL,
				   RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
				   attr, "Only support priority 0-7.");
		return -rte_errno;
	}

	return 0;
}

static int
cpfl_flow_param_valid(const struct rte_flow_attr *attr,
		      const struct rte_flow_item pattern[],
		      const struct rte_flow_action actions[],
		      struct rte_flow_error *error)
{
	int ret;

	if (!pattern) {
		rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
				   NULL, "NULL pattern.");
		return -rte_errno;
	}

	if (!attr) {
		rte_flow_error_set(error, EINVAL,
				   RTE_FLOW_ERROR_TYPE_ATTR,
				   NULL, "NULL attribute.");
		return -rte_errno;
	}

	ret = cpfl_flow_attr_valid(attr, error);
	if (ret)
		return ret;

	if (!actions || actions->type == RTE_FLOW_ACTION_TYPE_END) {
		rte_flow_error_set(error, EINVAL,
				   RTE_FLOW_ERROR_TYPE_ACTION_NUM,
				   NULL, "NULL action.");
		return -rte_errno;
	}

	return 0;
}

static int
__cpfl_flow_validate(struct rte_eth_dev *dev,
		     const struct rte_flow_attr *attr,
		     const struct rte_flow_item pattern[],
		     const struct rte_flow_action actions[],
		     void **meta,
		     struct cpfl_flow_engine **engine,
		     struct rte_flow_error *error)
{
	int ret;

	ret = cpfl_flow_param_valid(attr, pattern, actions, error);
	if (ret)
		return ret;

	*engine = cpfl_flow_engine_match(dev, attr, pattern, actions, meta);
	if (!*engine) {
		rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
				   NULL, "No matched engine.");
		return -rte_errno;
	}

	return 0;
}

int
cpfl_flow_validate(struct rte_eth_dev *dev,
		   const struct rte_flow_attr *attr,
		   const struct rte_flow_item pattern[],
		   const struct rte_flow_action actions[],
		   struct rte_flow_error *error)
{
	struct cpfl_flow_engine *engine = NULL;
	int ret;

	ret = __cpfl_flow_validate(dev, attr, pattern, actions, NULL, &engine, error);

	return ret;
}

struct rte_flow *
cpfl_flow_create(struct rte_eth_dev *dev,
		 const struct rte_flow_attr *attr,
		 const struct rte_flow_item pattern[],
		 const struct rte_flow_action actions[],
		 struct rte_flow_error *error)
{
	struct cpfl_itf *itf = CPFL_DEV_TO_ITF(dev);
	struct cpfl_flow_engine *engine = NULL;
	struct rte_flow *flow;
	void *meta;
	int ret;

	flow = rte_malloc(NULL, sizeof(struct rte_flow), 0);
	if (!flow) {
		rte_flow_error_set(error, ENOMEM,
				   RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
				   "Failed to allocate memory");
		return NULL;
	}

	ret = __cpfl_flow_validate(dev, attr, pattern, actions, &meta, &engine, error);
	if (ret) {
		rte_free(flow);
		return NULL;
	}

	if (!engine->create) {
		rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
				   NULL, "No matched flow creation function");
		rte_free(flow);
		return NULL;
	}

	ret = engine->create(dev, flow, meta, error);
	if (ret) {
		rte_free(flow);
		return NULL;
	}

	flow->engine = engine;
	TAILQ_INSERT_TAIL(&itf->flow_list, flow, next);

	return flow;
}

int
cpfl_flow_destroy(struct rte_eth_dev *dev,
		  struct rte_flow *flow,
		  struct rte_flow_error *error)
{
	struct cpfl_itf *itf = CPFL_DEV_TO_ITF(dev);
	int ret = 0;

	if (!flow || !flow->engine || !flow->engine->destroy) {
		rte_flow_error_set(error, EINVAL,
				   RTE_FLOW_ERROR_TYPE_HANDLE,
				   NULL, "Invalid flow");
		return -rte_errno;
	}

	ret = flow->engine->destroy(dev, flow, error);
	if (!ret)
		TAILQ_REMOVE(&itf->flow_list, flow, next);
	else
		PMD_DRV_LOG(ERR, "Failed to destroy flow");

	return ret;
}

int
cpfl_flow_flush(struct rte_eth_dev *dev,
		struct rte_flow_error *error)
{
	struct cpfl_itf *itf = CPFL_DEV_TO_ITF(dev);
	struct rte_flow *p_flow;
	void *temp;
	int ret = 0;

	RTE_TAILQ_FOREACH_SAFE(p_flow, &itf->flow_list, next, temp) {
		ret = cpfl_flow_destroy(dev, p_flow, error);
		if (ret) {
			PMD_DRV_LOG(ERR, "Failed to flush flows");
			return -EINVAL;
		}
	}

	return ret;
}

int
cpfl_flow_query(struct rte_eth_dev *dev,
		struct rte_flow *flow,
		const struct rte_flow_action *actions,
		void *data,
		struct rte_flow_error *error)
{
	struct rte_flow_query_count *count = data;
	int ret = -EINVAL;

	if (!flow || !flow->engine || !flow->engine->query_count) {
		rte_flow_error_set(error, EINVAL,
				   RTE_FLOW_ERROR_TYPE_HANDLE,
				   NULL, "Invalid flow");
		return -rte_errno;
	}

	for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
		switch (actions->type) {
		case RTE_FLOW_ACTION_TYPE_VOID:
			break;
		case RTE_FLOW_ACTION_TYPE_COUNT:
			ret = flow->engine->query_count(dev, flow, count, error);
			break;
		default:
			ret = rte_flow_error_set(error, ENOTSUP,
						 RTE_FLOW_ERROR_TYPE_ACTION,
						 actions,
						 "action not supported");
			break;
		}
	}

	return ret;
}

const struct rte_flow_ops cpfl_flow_ops = {
	.validate = cpfl_flow_validate,
	.create = cpfl_flow_create,
	.destroy = cpfl_flow_destroy,
	.flush = cpfl_flow_flush,
	.query = cpfl_flow_query,
};

int
cpfl_flow_init(struct cpfl_adapter_ext *ad, struct cpfl_devargs *devargs)
{
	int ret;

	if (devargs->flow_parser[0] == '\0') {
		PMD_INIT_LOG(WARNING, "flow module is not initialized");
		return 0;
	}

	ret = cpfl_flow_engine_init(ad);
	if (ret) {
		PMD_DRV_LOG(ERR, "Failed to init flow engines");
		goto err;
	}

	ret = cpfl_parser_create(&ad->flow_parser, devargs->flow_parser);
	if (ret) {
		PMD_DRV_LOG(ERR, "Failed to create flow parser");
		goto err;
	}

	return ret;

err:
	cpfl_flow_engine_uninit(ad);
	return ret;
}

void
cpfl_flow_uninit(struct cpfl_adapter_ext *ad)
{
	if (ad->flow_parser == NULL)
		return;

	cpfl_parser_destroy(ad->flow_parser);
	cpfl_flow_engine_uninit(ad);
}