xref: /dpdk/lib/ethdev/rte_flow.c (revision 09442498ef736d0a96632cf8b8c15d8ca78a6468)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016 6WIND S.A.
3  * Copyright 2016 Mellanox Technologies, Ltd
4  */
5 
6 #include <errno.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 
10 #include <rte_common.h>
11 #include <rte_errno.h>
12 #include <rte_branch_prediction.h>
13 #include <rte_string_fns.h>
14 #include <rte_mbuf_dyn.h>
15 #include "rte_ethdev.h"
16 #include "rte_flow_driver.h"
17 #include "rte_flow.h"
18 
19 /* Mbuf dynamic field name for metadata. */
20 int32_t rte_flow_dynf_metadata_offs = -1;
21 
22 /* Mbuf dynamic field flag bit number for metadata. */
23 uint64_t rte_flow_dynf_metadata_mask;
24 
25 /**
26  * Flow elements description tables.
27  */
28 struct rte_flow_desc_data {
29 	const char *name;
30 	size_t size;
31 	size_t (*desc_fn)(void *dst, const void *src);
32 };
33 
34 /**
35  *
36  * @param buf
37  * Destination memory.
38  * @param data
39  * Source memory
40  * @param size
41  * Requested copy size
42  * @param desc
43  * rte_flow_desc_item - for flow item conversion.
44  * rte_flow_desc_action - for flow action conversion.
45  * @param type
46  * Offset into the desc param or negative value for private flow elements.
47  */
48 static inline size_t
49 rte_flow_conv_copy(void *buf, const void *data, const size_t size,
50 		   const struct rte_flow_desc_data *desc, int type)
51 {
52 	/**
53 	 * Allow PMD private flow item
54 	 */
55 	bool rte_type = type >= 0;
56 
57 	size_t sz = rte_type ? desc[type].size : sizeof(void *);
58 	if (buf == NULL || data == NULL)
59 		return 0;
60 	rte_memcpy(buf, data, (size > sz ? sz : size));
61 	if (rte_type && desc[type].desc_fn)
62 		sz += desc[type].desc_fn(size > 0 ? buf : NULL, data);
63 	return sz;
64 }
65 
66 static size_t
67 rte_flow_item_flex_conv(void *buf, const void *data)
68 {
69 	struct rte_flow_item_flex *dst = buf;
70 	const struct rte_flow_item_flex *src = data;
71 	if (buf) {
72 		dst->pattern = rte_memcpy
73 			((void *)((uintptr_t)(dst + 1)), src->pattern,
74 			 src->length);
75 	}
76 	return src->length;
77 }
78 
79 /** Generate flow_item[] entry. */
80 #define MK_FLOW_ITEM(t, s) \
81 	[RTE_FLOW_ITEM_TYPE_ ## t] = { \
82 		.name = # t, \
83 		.size = s,               \
84 		.desc_fn = NULL,\
85 	}
86 
87 #define MK_FLOW_ITEM_FN(t, s, fn) \
88 	[RTE_FLOW_ITEM_TYPE_ ## t] = {\
89 		.name = # t,                 \
90 		.size = s,                   \
91 		.desc_fn = fn,               \
92 	}
93 
94 /** Information about known flow pattern items. */
95 static const struct rte_flow_desc_data rte_flow_desc_item[] = {
96 	MK_FLOW_ITEM(END, 0),
97 	MK_FLOW_ITEM(VOID, 0),
98 	MK_FLOW_ITEM(INVERT, 0),
99 	MK_FLOW_ITEM(ANY, sizeof(struct rte_flow_item_any)),
100 	MK_FLOW_ITEM(PORT_ID, sizeof(struct rte_flow_item_port_id)),
101 	MK_FLOW_ITEM(RAW, sizeof(struct rte_flow_item_raw)),
102 	MK_FLOW_ITEM(ETH, sizeof(struct rte_flow_item_eth)),
103 	MK_FLOW_ITEM(VLAN, sizeof(struct rte_flow_item_vlan)),
104 	MK_FLOW_ITEM(IPV4, sizeof(struct rte_flow_item_ipv4)),
105 	MK_FLOW_ITEM(IPV6, sizeof(struct rte_flow_item_ipv6)),
106 	MK_FLOW_ITEM(ICMP, sizeof(struct rte_flow_item_icmp)),
107 	MK_FLOW_ITEM(UDP, sizeof(struct rte_flow_item_udp)),
108 	MK_FLOW_ITEM(TCP, sizeof(struct rte_flow_item_tcp)),
109 	MK_FLOW_ITEM(SCTP, sizeof(struct rte_flow_item_sctp)),
110 	MK_FLOW_ITEM(VXLAN, sizeof(struct rte_flow_item_vxlan)),
111 	MK_FLOW_ITEM(E_TAG, sizeof(struct rte_flow_item_e_tag)),
112 	MK_FLOW_ITEM(NVGRE, sizeof(struct rte_flow_item_nvgre)),
113 	MK_FLOW_ITEM(MPLS, sizeof(struct rte_flow_item_mpls)),
114 	MK_FLOW_ITEM(GRE, sizeof(struct rte_flow_item_gre)),
115 	MK_FLOW_ITEM(FUZZY, sizeof(struct rte_flow_item_fuzzy)),
116 	MK_FLOW_ITEM(GTP, sizeof(struct rte_flow_item_gtp)),
117 	MK_FLOW_ITEM(GTPC, sizeof(struct rte_flow_item_gtp)),
118 	MK_FLOW_ITEM(GTPU, sizeof(struct rte_flow_item_gtp)),
119 	MK_FLOW_ITEM(ESP, sizeof(struct rte_flow_item_esp)),
120 	MK_FLOW_ITEM(GENEVE, sizeof(struct rte_flow_item_geneve)),
121 	MK_FLOW_ITEM(VXLAN_GPE, sizeof(struct rte_flow_item_vxlan_gpe)),
122 	MK_FLOW_ITEM(ARP_ETH_IPV4, sizeof(struct rte_flow_item_arp_eth_ipv4)),
123 	MK_FLOW_ITEM(IPV6_EXT, sizeof(struct rte_flow_item_ipv6_ext)),
124 	MK_FLOW_ITEM(IPV6_FRAG_EXT, sizeof(struct rte_flow_item_ipv6_frag_ext)),
125 	MK_FLOW_ITEM(ICMP6, sizeof(struct rte_flow_item_icmp6)),
126 	MK_FLOW_ITEM(ICMP6_ND_NS, sizeof(struct rte_flow_item_icmp6_nd_ns)),
127 	MK_FLOW_ITEM(ICMP6_ND_NA, sizeof(struct rte_flow_item_icmp6_nd_na)),
128 	MK_FLOW_ITEM(ICMP6_ND_OPT, sizeof(struct rte_flow_item_icmp6_nd_opt)),
129 	MK_FLOW_ITEM(ICMP6_ND_OPT_SLA_ETH,
130 		     sizeof(struct rte_flow_item_icmp6_nd_opt_sla_eth)),
131 	MK_FLOW_ITEM(ICMP6_ND_OPT_TLA_ETH,
132 		     sizeof(struct rte_flow_item_icmp6_nd_opt_tla_eth)),
133 	MK_FLOW_ITEM(MARK, sizeof(struct rte_flow_item_mark)),
134 	MK_FLOW_ITEM(META, sizeof(struct rte_flow_item_meta)),
135 	MK_FLOW_ITEM(TAG, sizeof(struct rte_flow_item_tag)),
136 	MK_FLOW_ITEM(GRE_KEY, sizeof(rte_be32_t)),
137 	MK_FLOW_ITEM(GRE_OPTION, sizeof(struct rte_flow_item_gre_opt)),
138 	MK_FLOW_ITEM(GTP_PSC, sizeof(struct rte_flow_item_gtp_psc)),
139 	MK_FLOW_ITEM(PPPOES, sizeof(struct rte_flow_item_pppoe)),
140 	MK_FLOW_ITEM(PPPOED, sizeof(struct rte_flow_item_pppoe)),
141 	MK_FLOW_ITEM(PPPOE_PROTO_ID,
142 			sizeof(struct rte_flow_item_pppoe_proto_id)),
143 	MK_FLOW_ITEM(NSH, sizeof(struct rte_flow_item_nsh)),
144 	MK_FLOW_ITEM(IGMP, sizeof(struct rte_flow_item_igmp)),
145 	MK_FLOW_ITEM(AH, sizeof(struct rte_flow_item_ah)),
146 	MK_FLOW_ITEM(HIGIG2, sizeof(struct rte_flow_item_higig2_hdr)),
147 	MK_FLOW_ITEM(L2TPV3OIP, sizeof(struct rte_flow_item_l2tpv3oip)),
148 	MK_FLOW_ITEM(PFCP, sizeof(struct rte_flow_item_pfcp)),
149 	MK_FLOW_ITEM(ECPRI, sizeof(struct rte_flow_item_ecpri)),
150 	MK_FLOW_ITEM(GENEVE_OPT, sizeof(struct rte_flow_item_geneve_opt)),
151 	MK_FLOW_ITEM(INTEGRITY, sizeof(struct rte_flow_item_integrity)),
152 	MK_FLOW_ITEM(CONNTRACK, sizeof(uint32_t)),
153 	MK_FLOW_ITEM(PORT_REPRESENTOR, sizeof(struct rte_flow_item_ethdev)),
154 	MK_FLOW_ITEM(REPRESENTED_PORT, sizeof(struct rte_flow_item_ethdev)),
155 	MK_FLOW_ITEM_FN(FLEX, sizeof(struct rte_flow_item_flex),
156 			rte_flow_item_flex_conv),
157 	MK_FLOW_ITEM(L2TPV2, sizeof(struct rte_flow_item_l2tpv2)),
158 	MK_FLOW_ITEM(PPP, sizeof(struct rte_flow_item_ppp)),
159 	MK_FLOW_ITEM(METER_COLOR, sizeof(struct rte_flow_item_meter_color)),
160 };
161 
162 /** Generate flow_action[] entry. */
163 #define MK_FLOW_ACTION(t, s) \
164 	[RTE_FLOW_ACTION_TYPE_ ## t] = { \
165 		.name = # t, \
166 		.size = s, \
167 		.desc_fn = NULL,\
168 	}
169 
170 #define MK_FLOW_ACTION_FN(t, fn) \
171 	[RTE_FLOW_ACTION_TYPE_ ## t] = { \
172 		.name = # t, \
173 		.size = 0, \
174 		.desc_fn = fn,\
175 	}
176 
177 
178 /** Information about known flow actions. */
179 static const struct rte_flow_desc_data rte_flow_desc_action[] = {
180 	MK_FLOW_ACTION(END, 0),
181 	MK_FLOW_ACTION(VOID, 0),
182 	MK_FLOW_ACTION(PASSTHRU, 0),
183 	MK_FLOW_ACTION(JUMP, sizeof(struct rte_flow_action_jump)),
184 	MK_FLOW_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
185 	MK_FLOW_ACTION(FLAG, 0),
186 	MK_FLOW_ACTION(QUEUE, sizeof(struct rte_flow_action_queue)),
187 	MK_FLOW_ACTION(DROP, 0),
188 	MK_FLOW_ACTION(COUNT, sizeof(struct rte_flow_action_count)),
189 	MK_FLOW_ACTION(RSS, sizeof(struct rte_flow_action_rss)),
190 	MK_FLOW_ACTION(PF, 0),
191 	MK_FLOW_ACTION(VF, sizeof(struct rte_flow_action_vf)),
192 	MK_FLOW_ACTION(PORT_ID, sizeof(struct rte_flow_action_port_id)),
193 	MK_FLOW_ACTION(METER, sizeof(struct rte_flow_action_meter)),
194 	MK_FLOW_ACTION(SECURITY, sizeof(struct rte_flow_action_security)),
195 	MK_FLOW_ACTION(OF_DEC_NW_TTL, 0),
196 	MK_FLOW_ACTION(OF_POP_VLAN, 0),
197 	MK_FLOW_ACTION(OF_PUSH_VLAN,
198 		       sizeof(struct rte_flow_action_of_push_vlan)),
199 	MK_FLOW_ACTION(OF_SET_VLAN_VID,
200 		       sizeof(struct rte_flow_action_of_set_vlan_vid)),
201 	MK_FLOW_ACTION(OF_SET_VLAN_PCP,
202 		       sizeof(struct rte_flow_action_of_set_vlan_pcp)),
203 	MK_FLOW_ACTION(OF_POP_MPLS,
204 		       sizeof(struct rte_flow_action_of_pop_mpls)),
205 	MK_FLOW_ACTION(OF_PUSH_MPLS,
206 		       sizeof(struct rte_flow_action_of_push_mpls)),
207 	MK_FLOW_ACTION(VXLAN_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
208 	MK_FLOW_ACTION(VXLAN_DECAP, 0),
209 	MK_FLOW_ACTION(NVGRE_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
210 	MK_FLOW_ACTION(NVGRE_DECAP, 0),
211 	MK_FLOW_ACTION(RAW_ENCAP, sizeof(struct rte_flow_action_raw_encap)),
212 	MK_FLOW_ACTION(RAW_DECAP, sizeof(struct rte_flow_action_raw_decap)),
213 	MK_FLOW_ACTION(SET_IPV4_SRC,
214 		       sizeof(struct rte_flow_action_set_ipv4)),
215 	MK_FLOW_ACTION(SET_IPV4_DST,
216 		       sizeof(struct rte_flow_action_set_ipv4)),
217 	MK_FLOW_ACTION(SET_IPV6_SRC,
218 		       sizeof(struct rte_flow_action_set_ipv6)),
219 	MK_FLOW_ACTION(SET_IPV6_DST,
220 		       sizeof(struct rte_flow_action_set_ipv6)),
221 	MK_FLOW_ACTION(SET_TP_SRC,
222 		       sizeof(struct rte_flow_action_set_tp)),
223 	MK_FLOW_ACTION(SET_TP_DST,
224 		       sizeof(struct rte_flow_action_set_tp)),
225 	MK_FLOW_ACTION(MAC_SWAP, 0),
226 	MK_FLOW_ACTION(DEC_TTL, 0),
227 	MK_FLOW_ACTION(SET_TTL, sizeof(struct rte_flow_action_set_ttl)),
228 	MK_FLOW_ACTION(SET_MAC_SRC, sizeof(struct rte_flow_action_set_mac)),
229 	MK_FLOW_ACTION(SET_MAC_DST, sizeof(struct rte_flow_action_set_mac)),
230 	MK_FLOW_ACTION(INC_TCP_SEQ, sizeof(rte_be32_t)),
231 	MK_FLOW_ACTION(DEC_TCP_SEQ, sizeof(rte_be32_t)),
232 	MK_FLOW_ACTION(INC_TCP_ACK, sizeof(rte_be32_t)),
233 	MK_FLOW_ACTION(DEC_TCP_ACK, sizeof(rte_be32_t)),
234 	MK_FLOW_ACTION(SET_TAG, sizeof(struct rte_flow_action_set_tag)),
235 	MK_FLOW_ACTION(SET_META, sizeof(struct rte_flow_action_set_meta)),
236 	MK_FLOW_ACTION(SET_IPV4_DSCP, sizeof(struct rte_flow_action_set_dscp)),
237 	MK_FLOW_ACTION(SET_IPV6_DSCP, sizeof(struct rte_flow_action_set_dscp)),
238 	MK_FLOW_ACTION(AGE, sizeof(struct rte_flow_action_age)),
239 	MK_FLOW_ACTION(SAMPLE, sizeof(struct rte_flow_action_sample)),
240 	MK_FLOW_ACTION(MODIFY_FIELD,
241 		       sizeof(struct rte_flow_action_modify_field)),
242 	/**
243 	 * Indirect action represented as handle of type
244 	 * (struct rte_flow_action_handle *) stored in conf field (see
245 	 * struct rte_flow_action); no need for additional structure to * store
246 	 * indirect action handle.
247 	 */
248 	MK_FLOW_ACTION(INDIRECT, 0),
249 	MK_FLOW_ACTION(CONNTRACK, sizeof(struct rte_flow_action_conntrack)),
250 	MK_FLOW_ACTION(PORT_REPRESENTOR, sizeof(struct rte_flow_action_ethdev)),
251 	MK_FLOW_ACTION(REPRESENTED_PORT, sizeof(struct rte_flow_action_ethdev)),
252 	MK_FLOW_ACTION(METER_MARK, sizeof(struct rte_flow_action_meter_mark)),
253 	MK_FLOW_ACTION(SEND_TO_KERNEL, 0),
254 };
255 
256 int
257 rte_flow_dynf_metadata_register(void)
258 {
259 	int offset;
260 	int flag;
261 
262 	static const struct rte_mbuf_dynfield desc_offs = {
263 		.name = RTE_MBUF_DYNFIELD_METADATA_NAME,
264 		.size = sizeof(uint32_t),
265 		.align = __alignof__(uint32_t),
266 	};
267 	static const struct rte_mbuf_dynflag desc_flag = {
268 		.name = RTE_MBUF_DYNFLAG_METADATA_NAME,
269 	};
270 
271 	offset = rte_mbuf_dynfield_register(&desc_offs);
272 	if (offset < 0)
273 		goto error;
274 	flag = rte_mbuf_dynflag_register(&desc_flag);
275 	if (flag < 0)
276 		goto error;
277 	rte_flow_dynf_metadata_offs = offset;
278 	rte_flow_dynf_metadata_mask = RTE_BIT64(flag);
279 	return 0;
280 
281 error:
282 	rte_flow_dynf_metadata_offs = -1;
283 	rte_flow_dynf_metadata_mask = UINT64_C(0);
284 	return -rte_errno;
285 }
286 
287 static inline void
288 fts_enter(struct rte_eth_dev *dev)
289 {
290 	if (!(dev->data->dev_flags & RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE))
291 		pthread_mutex_lock(&dev->data->flow_ops_mutex);
292 }
293 
294 static inline void
295 fts_exit(struct rte_eth_dev *dev)
296 {
297 	if (!(dev->data->dev_flags & RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE))
298 		pthread_mutex_unlock(&dev->data->flow_ops_mutex);
299 }
300 
301 static int
302 flow_err(uint16_t port_id, int ret, struct rte_flow_error *error)
303 {
304 	if (ret == 0)
305 		return 0;
306 	if (rte_eth_dev_is_removed(port_id))
307 		return rte_flow_error_set(error, EIO,
308 					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
309 					  NULL, rte_strerror(EIO));
310 	return ret;
311 }
312 
313 /* Get generic flow operations structure from a port. */
314 const struct rte_flow_ops *
315 rte_flow_ops_get(uint16_t port_id, struct rte_flow_error *error)
316 {
317 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
318 	const struct rte_flow_ops *ops;
319 	int code;
320 
321 	if (unlikely(!rte_eth_dev_is_valid_port(port_id)))
322 		code = ENODEV;
323 	else if (unlikely(dev->dev_ops->flow_ops_get == NULL))
324 		/* flow API not supported with this driver dev_ops */
325 		code = ENOSYS;
326 	else
327 		code = dev->dev_ops->flow_ops_get(dev, &ops);
328 	if (code == 0 && ops == NULL)
329 		/* flow API not supported with this device */
330 		code = ENOSYS;
331 
332 	if (code != 0) {
333 		rte_flow_error_set(error, code, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
334 				   NULL, rte_strerror(code));
335 		return NULL;
336 	}
337 	return ops;
338 }
339 
340 /* Check whether a flow rule can be created on a given port. */
341 int
342 rte_flow_validate(uint16_t port_id,
343 		  const struct rte_flow_attr *attr,
344 		  const struct rte_flow_item pattern[],
345 		  const struct rte_flow_action actions[],
346 		  struct rte_flow_error *error)
347 {
348 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
349 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
350 	int ret;
351 
352 	if (likely(!!attr) && attr->transfer &&
353 	    (attr->ingress || attr->egress)) {
354 		return rte_flow_error_set(error, EINVAL,
355 					  RTE_FLOW_ERROR_TYPE_ATTR,
356 					  attr, "cannot use attr ingress/egress with attr transfer");
357 	}
358 
359 	if (unlikely(!ops))
360 		return -rte_errno;
361 	if (likely(!!ops->validate)) {
362 		fts_enter(dev);
363 		ret = ops->validate(dev, attr, pattern, actions, error);
364 		fts_exit(dev);
365 		return flow_err(port_id, ret, error);
366 	}
367 	return rte_flow_error_set(error, ENOSYS,
368 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
369 				  NULL, rte_strerror(ENOSYS));
370 }
371 
372 /* Create a flow rule on a given port. */
373 struct rte_flow *
374 rte_flow_create(uint16_t port_id,
375 		const struct rte_flow_attr *attr,
376 		const struct rte_flow_item pattern[],
377 		const struct rte_flow_action actions[],
378 		struct rte_flow_error *error)
379 {
380 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
381 	struct rte_flow *flow;
382 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
383 
384 	if (unlikely(!ops))
385 		return NULL;
386 	if (likely(!!ops->create)) {
387 		fts_enter(dev);
388 		flow = ops->create(dev, attr, pattern, actions, error);
389 		fts_exit(dev);
390 		if (flow == NULL)
391 			flow_err(port_id, -rte_errno, error);
392 		return flow;
393 	}
394 	rte_flow_error_set(error, ENOSYS, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
395 			   NULL, rte_strerror(ENOSYS));
396 	return NULL;
397 }
398 
399 /* Destroy a flow rule on a given port. */
400 int
401 rte_flow_destroy(uint16_t port_id,
402 		 struct rte_flow *flow,
403 		 struct rte_flow_error *error)
404 {
405 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
406 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
407 	int ret;
408 
409 	if (unlikely(!ops))
410 		return -rte_errno;
411 	if (likely(!!ops->destroy)) {
412 		fts_enter(dev);
413 		ret = ops->destroy(dev, flow, error);
414 		fts_exit(dev);
415 		return flow_err(port_id, ret, error);
416 	}
417 	return rte_flow_error_set(error, ENOSYS,
418 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
419 				  NULL, rte_strerror(ENOSYS));
420 }
421 
422 /* Destroy all flow rules associated with a port. */
423 int
424 rte_flow_flush(uint16_t port_id,
425 	       struct rte_flow_error *error)
426 {
427 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
428 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
429 	int ret;
430 
431 	if (unlikely(!ops))
432 		return -rte_errno;
433 	if (likely(!!ops->flush)) {
434 		fts_enter(dev);
435 		ret = ops->flush(dev, error);
436 		fts_exit(dev);
437 		return flow_err(port_id, ret, error);
438 	}
439 	return rte_flow_error_set(error, ENOSYS,
440 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
441 				  NULL, rte_strerror(ENOSYS));
442 }
443 
444 /* Query an existing flow rule. */
445 int
446 rte_flow_query(uint16_t port_id,
447 	       struct rte_flow *flow,
448 	       const struct rte_flow_action *action,
449 	       void *data,
450 	       struct rte_flow_error *error)
451 {
452 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
453 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
454 	int ret;
455 
456 	if (!ops)
457 		return -rte_errno;
458 	if (likely(!!ops->query)) {
459 		fts_enter(dev);
460 		ret = ops->query(dev, flow, action, data, error);
461 		fts_exit(dev);
462 		return flow_err(port_id, ret, error);
463 	}
464 	return rte_flow_error_set(error, ENOSYS,
465 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
466 				  NULL, rte_strerror(ENOSYS));
467 }
468 
469 /* Restrict ingress traffic to the defined flow rules. */
470 int
471 rte_flow_isolate(uint16_t port_id,
472 		 int set,
473 		 struct rte_flow_error *error)
474 {
475 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
476 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
477 	int ret;
478 
479 	if (!ops)
480 		return -rte_errno;
481 	if (likely(!!ops->isolate)) {
482 		fts_enter(dev);
483 		ret = ops->isolate(dev, set, error);
484 		fts_exit(dev);
485 		return flow_err(port_id, ret, error);
486 	}
487 	return rte_flow_error_set(error, ENOSYS,
488 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
489 				  NULL, rte_strerror(ENOSYS));
490 }
491 
492 /* Initialize flow error structure. */
493 int
494 rte_flow_error_set(struct rte_flow_error *error,
495 		   int code,
496 		   enum rte_flow_error_type type,
497 		   const void *cause,
498 		   const char *message)
499 {
500 	if (error) {
501 		*error = (struct rte_flow_error){
502 			.type = type,
503 			.cause = cause,
504 			.message = message,
505 		};
506 	}
507 	rte_errno = code;
508 	return -code;
509 }
510 
511 /** Pattern item specification types. */
512 enum rte_flow_conv_item_spec_type {
513 	RTE_FLOW_CONV_ITEM_SPEC,
514 	RTE_FLOW_CONV_ITEM_LAST,
515 	RTE_FLOW_CONV_ITEM_MASK,
516 };
517 
518 /**
519  * Copy pattern item specification.
520  *
521  * @param[out] buf
522  *   Output buffer. Can be NULL if @p size is zero.
523  * @param size
524  *   Size of @p buf in bytes.
525  * @param[in] item
526  *   Pattern item to copy specification from.
527  * @param type
528  *   Specification selector for either @p spec, @p last or @p mask.
529  *
530  * @return
531  *   Number of bytes needed to store pattern item specification regardless
532  *   of @p size. @p buf contents are truncated to @p size if not large
533  *   enough.
534  */
535 static size_t
536 rte_flow_conv_item_spec(void *buf, const size_t size,
537 			const struct rte_flow_item *item,
538 			enum rte_flow_conv_item_spec_type type)
539 {
540 	size_t off;
541 	const void *data =
542 		type == RTE_FLOW_CONV_ITEM_SPEC ? item->spec :
543 		type == RTE_FLOW_CONV_ITEM_LAST ? item->last :
544 		type == RTE_FLOW_CONV_ITEM_MASK ? item->mask :
545 		NULL;
546 
547 	switch (item->type) {
548 		union {
549 			const struct rte_flow_item_raw *raw;
550 		} spec;
551 		union {
552 			const struct rte_flow_item_raw *raw;
553 		} last;
554 		union {
555 			const struct rte_flow_item_raw *raw;
556 		} mask;
557 		union {
558 			const struct rte_flow_item_raw *raw;
559 		} src;
560 		union {
561 			struct rte_flow_item_raw *raw;
562 		} dst;
563 		size_t tmp;
564 
565 	case RTE_FLOW_ITEM_TYPE_RAW:
566 		spec.raw = item->spec;
567 		last.raw = item->last ? item->last : item->spec;
568 		mask.raw = item->mask ? item->mask : &rte_flow_item_raw_mask;
569 		src.raw = data;
570 		dst.raw = buf;
571 		rte_memcpy(dst.raw,
572 			   (&(struct rte_flow_item_raw){
573 				.relative = src.raw->relative,
574 				.search = src.raw->search,
575 				.reserved = src.raw->reserved,
576 				.offset = src.raw->offset,
577 				.limit = src.raw->limit,
578 				.length = src.raw->length,
579 			   }),
580 			   size > sizeof(*dst.raw) ? sizeof(*dst.raw) : size);
581 		off = sizeof(*dst.raw);
582 		if (type == RTE_FLOW_CONV_ITEM_SPEC ||
583 		    (type == RTE_FLOW_CONV_ITEM_MASK &&
584 		     ((spec.raw->length & mask.raw->length) >=
585 		      (last.raw->length & mask.raw->length))))
586 			tmp = spec.raw->length & mask.raw->length;
587 		else
588 			tmp = last.raw->length & mask.raw->length;
589 		if (tmp) {
590 			off = RTE_ALIGN_CEIL(off, sizeof(*dst.raw->pattern));
591 			if (size >= off + tmp)
592 				dst.raw->pattern = rte_memcpy
593 					((void *)((uintptr_t)dst.raw + off),
594 					 src.raw->pattern, tmp);
595 			off += tmp;
596 		}
597 		break;
598 	default:
599 		off = rte_flow_conv_copy(buf, data, size,
600 					 rte_flow_desc_item, item->type);
601 		break;
602 	}
603 	return off;
604 }
605 
606 /**
607  * Copy action configuration.
608  *
609  * @param[out] buf
610  *   Output buffer. Can be NULL if @p size is zero.
611  * @param size
612  *   Size of @p buf in bytes.
613  * @param[in] action
614  *   Action to copy configuration from.
615  *
616  * @return
617  *   Number of bytes needed to store pattern item specification regardless
618  *   of @p size. @p buf contents are truncated to @p size if not large
619  *   enough.
620  */
621 static size_t
622 rte_flow_conv_action_conf(void *buf, const size_t size,
623 			  const struct rte_flow_action *action)
624 {
625 	size_t off;
626 
627 	switch (action->type) {
628 		union {
629 			const struct rte_flow_action_rss *rss;
630 			const struct rte_flow_action_vxlan_encap *vxlan_encap;
631 			const struct rte_flow_action_nvgre_encap *nvgre_encap;
632 		} src;
633 		union {
634 			struct rte_flow_action_rss *rss;
635 			struct rte_flow_action_vxlan_encap *vxlan_encap;
636 			struct rte_flow_action_nvgre_encap *nvgre_encap;
637 		} dst;
638 		size_t tmp;
639 		int ret;
640 
641 	case RTE_FLOW_ACTION_TYPE_RSS:
642 		src.rss = action->conf;
643 		dst.rss = buf;
644 		rte_memcpy(dst.rss,
645 			   (&(struct rte_flow_action_rss){
646 				.func = src.rss->func,
647 				.level = src.rss->level,
648 				.types = src.rss->types,
649 				.key_len = src.rss->key_len,
650 				.queue_num = src.rss->queue_num,
651 			   }),
652 			   size > sizeof(*dst.rss) ? sizeof(*dst.rss) : size);
653 		off = sizeof(*dst.rss);
654 		if (src.rss->key_len && src.rss->key) {
655 			off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->key));
656 			tmp = sizeof(*src.rss->key) * src.rss->key_len;
657 			if (size >= off + tmp)
658 				dst.rss->key = rte_memcpy
659 					((void *)((uintptr_t)dst.rss + off),
660 					 src.rss->key, tmp);
661 			off += tmp;
662 		}
663 		if (src.rss->queue_num) {
664 			off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->queue));
665 			tmp = sizeof(*src.rss->queue) * src.rss->queue_num;
666 			if (size >= off + tmp)
667 				dst.rss->queue = rte_memcpy
668 					((void *)((uintptr_t)dst.rss + off),
669 					 src.rss->queue, tmp);
670 			off += tmp;
671 		}
672 		break;
673 	case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
674 	case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
675 		src.vxlan_encap = action->conf;
676 		dst.vxlan_encap = buf;
677 		RTE_BUILD_BUG_ON(sizeof(*src.vxlan_encap) !=
678 				 sizeof(*src.nvgre_encap) ||
679 				 offsetof(struct rte_flow_action_vxlan_encap,
680 					  definition) !=
681 				 offsetof(struct rte_flow_action_nvgre_encap,
682 					  definition));
683 		off = sizeof(*dst.vxlan_encap);
684 		if (src.vxlan_encap->definition) {
685 			off = RTE_ALIGN_CEIL
686 				(off, sizeof(*dst.vxlan_encap->definition));
687 			ret = rte_flow_conv
688 				(RTE_FLOW_CONV_OP_PATTERN,
689 				 (void *)((uintptr_t)dst.vxlan_encap + off),
690 				 size > off ? size - off : 0,
691 				 src.vxlan_encap->definition, NULL);
692 			if (ret < 0)
693 				return 0;
694 			if (size >= off + ret)
695 				dst.vxlan_encap->definition =
696 					(void *)((uintptr_t)dst.vxlan_encap +
697 						 off);
698 			off += ret;
699 		}
700 		break;
701 	default:
702 		off = rte_flow_conv_copy(buf, action->conf, size,
703 					 rte_flow_desc_action, action->type);
704 		break;
705 	}
706 	return off;
707 }
708 
709 /**
710  * Copy a list of pattern items.
711  *
712  * @param[out] dst
713  *   Destination buffer. Can be NULL if @p size is zero.
714  * @param size
715  *   Size of @p dst in bytes.
716  * @param[in] src
717  *   Source pattern items.
718  * @param num
719  *   Maximum number of pattern items to process from @p src or 0 to process
720  *   the entire list. In both cases, processing stops after
721  *   RTE_FLOW_ITEM_TYPE_END is encountered.
722  * @param[out] error
723  *   Perform verbose error reporting if not NULL.
724  *
725  * @return
726  *   A positive value representing the number of bytes needed to store
727  *   pattern items regardless of @p size on success (@p buf contents are
728  *   truncated to @p size if not large enough), a negative errno value
729  *   otherwise and rte_errno is set.
730  */
731 static int
732 rte_flow_conv_pattern(struct rte_flow_item *dst,
733 		      const size_t size,
734 		      const struct rte_flow_item *src,
735 		      unsigned int num,
736 		      struct rte_flow_error *error)
737 {
738 	uintptr_t data = (uintptr_t)dst;
739 	size_t off;
740 	size_t ret;
741 	unsigned int i;
742 
743 	for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
744 		/**
745 		 * allow PMD private flow item
746 		 */
747 		if (((int)src->type >= 0) &&
748 			((size_t)src->type >= RTE_DIM(rte_flow_desc_item) ||
749 		    !rte_flow_desc_item[src->type].name))
750 			return rte_flow_error_set
751 				(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, src,
752 				 "cannot convert unknown item type");
753 		if (size >= off + sizeof(*dst))
754 			*dst = (struct rte_flow_item){
755 				.type = src->type,
756 			};
757 		off += sizeof(*dst);
758 		if (!src->type)
759 			num = i + 1;
760 	}
761 	num = i;
762 	src -= num;
763 	dst -= num;
764 	do {
765 		if (src->spec) {
766 			off = RTE_ALIGN_CEIL(off, sizeof(double));
767 			ret = rte_flow_conv_item_spec
768 				((void *)(data + off),
769 				 size > off ? size - off : 0, src,
770 				 RTE_FLOW_CONV_ITEM_SPEC);
771 			if (size && size >= off + ret)
772 				dst->spec = (void *)(data + off);
773 			off += ret;
774 
775 		}
776 		if (src->last) {
777 			off = RTE_ALIGN_CEIL(off, sizeof(double));
778 			ret = rte_flow_conv_item_spec
779 				((void *)(data + off),
780 				 size > off ? size - off : 0, src,
781 				 RTE_FLOW_CONV_ITEM_LAST);
782 			if (size && size >= off + ret)
783 				dst->last = (void *)(data + off);
784 			off += ret;
785 		}
786 		if (src->mask) {
787 			off = RTE_ALIGN_CEIL(off, sizeof(double));
788 			ret = rte_flow_conv_item_spec
789 				((void *)(data + off),
790 				 size > off ? size - off : 0, src,
791 				 RTE_FLOW_CONV_ITEM_MASK);
792 			if (size && size >= off + ret)
793 				dst->mask = (void *)(data + off);
794 			off += ret;
795 		}
796 		++src;
797 		++dst;
798 	} while (--num);
799 	return off;
800 }
801 
802 /**
803  * Copy a list of actions.
804  *
805  * @param[out] dst
806  *   Destination buffer. Can be NULL if @p size is zero.
807  * @param size
808  *   Size of @p dst in bytes.
809  * @param[in] src
810  *   Source actions.
811  * @param num
812  *   Maximum number of actions to process from @p src or 0 to process the
813  *   entire list. In both cases, processing stops after
814  *   RTE_FLOW_ACTION_TYPE_END is encountered.
815  * @param[out] error
816  *   Perform verbose error reporting if not NULL.
817  *
818  * @return
819  *   A positive value representing the number of bytes needed to store
820  *   actions regardless of @p size on success (@p buf contents are truncated
821  *   to @p size if not large enough), a negative errno value otherwise and
822  *   rte_errno is set.
823  */
824 static int
825 rte_flow_conv_actions(struct rte_flow_action *dst,
826 		      const size_t size,
827 		      const struct rte_flow_action *src,
828 		      unsigned int num,
829 		      struct rte_flow_error *error)
830 {
831 	uintptr_t data = (uintptr_t)dst;
832 	size_t off;
833 	size_t ret;
834 	unsigned int i;
835 
836 	for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
837 		/**
838 		 * allow PMD private flow action
839 		 */
840 		if (((int)src->type >= 0) &&
841 		    ((size_t)src->type >= RTE_DIM(rte_flow_desc_action) ||
842 		    !rte_flow_desc_action[src->type].name))
843 			return rte_flow_error_set
844 				(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
845 				 src, "cannot convert unknown action type");
846 		if (size >= off + sizeof(*dst))
847 			*dst = (struct rte_flow_action){
848 				.type = src->type,
849 			};
850 		off += sizeof(*dst);
851 		if (!src->type)
852 			num = i + 1;
853 	}
854 	num = i;
855 	src -= num;
856 	dst -= num;
857 	do {
858 		if (src->conf) {
859 			off = RTE_ALIGN_CEIL(off, sizeof(double));
860 			ret = rte_flow_conv_action_conf
861 				((void *)(data + off),
862 				 size > off ? size - off : 0, src);
863 			if (size && size >= off + ret)
864 				dst->conf = (void *)(data + off);
865 			off += ret;
866 		}
867 		++src;
868 		++dst;
869 	} while (--num);
870 	return off;
871 }
872 
873 /**
874  * Copy flow rule components.
875  *
876  * This comprises the flow rule descriptor itself, attributes, pattern and
877  * actions list. NULL components in @p src are skipped.
878  *
879  * @param[out] dst
880  *   Destination buffer. Can be NULL if @p size is zero.
881  * @param size
882  *   Size of @p dst in bytes.
883  * @param[in] src
884  *   Source flow rule descriptor.
885  * @param[out] error
886  *   Perform verbose error reporting if not NULL.
887  *
888  * @return
889  *   A positive value representing the number of bytes needed to store all
890  *   components including the descriptor regardless of @p size on success
891  *   (@p buf contents are truncated to @p size if not large enough), a
892  *   negative errno value otherwise and rte_errno is set.
893  */
894 static int
895 rte_flow_conv_rule(struct rte_flow_conv_rule *dst,
896 		   const size_t size,
897 		   const struct rte_flow_conv_rule *src,
898 		   struct rte_flow_error *error)
899 {
900 	size_t off;
901 	int ret;
902 
903 	rte_memcpy(dst,
904 		   (&(struct rte_flow_conv_rule){
905 			.attr = NULL,
906 			.pattern = NULL,
907 			.actions = NULL,
908 		   }),
909 		   size > sizeof(*dst) ? sizeof(*dst) : size);
910 	off = sizeof(*dst);
911 	if (src->attr_ro) {
912 		off = RTE_ALIGN_CEIL(off, sizeof(double));
913 		if (size && size >= off + sizeof(*dst->attr))
914 			dst->attr = rte_memcpy
915 				((void *)((uintptr_t)dst + off),
916 				 src->attr_ro, sizeof(*dst->attr));
917 		off += sizeof(*dst->attr);
918 	}
919 	if (src->pattern_ro) {
920 		off = RTE_ALIGN_CEIL(off, sizeof(double));
921 		ret = rte_flow_conv_pattern((void *)((uintptr_t)dst + off),
922 					    size > off ? size - off : 0,
923 					    src->pattern_ro, 0, error);
924 		if (ret < 0)
925 			return ret;
926 		if (size && size >= off + (size_t)ret)
927 			dst->pattern = (void *)((uintptr_t)dst + off);
928 		off += ret;
929 	}
930 	if (src->actions_ro) {
931 		off = RTE_ALIGN_CEIL(off, sizeof(double));
932 		ret = rte_flow_conv_actions((void *)((uintptr_t)dst + off),
933 					    size > off ? size - off : 0,
934 					    src->actions_ro, 0, error);
935 		if (ret < 0)
936 			return ret;
937 		if (size >= off + (size_t)ret)
938 			dst->actions = (void *)((uintptr_t)dst + off);
939 		off += ret;
940 	}
941 	return off;
942 }
943 
944 /**
945  * Retrieve the name of a pattern item/action type.
946  *
947  * @param is_action
948  *   Nonzero when @p src represents an action type instead of a pattern item
949  *   type.
950  * @param is_ptr
951  *   Nonzero to write string address instead of contents into @p dst.
952  * @param[out] dst
953  *   Destination buffer. Can be NULL if @p size is zero.
954  * @param size
955  *   Size of @p dst in bytes.
956  * @param[in] src
957  *   Depending on @p is_action, source pattern item or action type cast as a
958  *   pointer.
959  * @param[out] error
960  *   Perform verbose error reporting if not NULL.
961  *
962  * @return
963  *   A positive value representing the number of bytes needed to store the
964  *   name or its address regardless of @p size on success (@p buf contents
965  *   are truncated to @p size if not large enough), a negative errno value
966  *   otherwise and rte_errno is set.
967  */
968 static int
969 rte_flow_conv_name(int is_action,
970 		   int is_ptr,
971 		   char *dst,
972 		   const size_t size,
973 		   const void *src,
974 		   struct rte_flow_error *error)
975 {
976 	struct desc_info {
977 		const struct rte_flow_desc_data *data;
978 		size_t num;
979 	};
980 	static const struct desc_info info_rep[2] = {
981 		{ rte_flow_desc_item, RTE_DIM(rte_flow_desc_item), },
982 		{ rte_flow_desc_action, RTE_DIM(rte_flow_desc_action), },
983 	};
984 	const struct desc_info *const info = &info_rep[!!is_action];
985 	unsigned int type = (uintptr_t)src;
986 
987 	if (type >= info->num)
988 		return rte_flow_error_set
989 			(error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
990 			 "unknown object type to retrieve the name of");
991 	if (!is_ptr)
992 		return strlcpy(dst, info->data[type].name, size);
993 	if (size >= sizeof(const char **))
994 		*((const char **)dst) = info->data[type].name;
995 	return sizeof(const char **);
996 }
997 
998 /** Helper function to convert flow API objects. */
999 int
1000 rte_flow_conv(enum rte_flow_conv_op op,
1001 	      void *dst,
1002 	      size_t size,
1003 	      const void *src,
1004 	      struct rte_flow_error *error)
1005 {
1006 	switch (op) {
1007 		const struct rte_flow_attr *attr;
1008 
1009 	case RTE_FLOW_CONV_OP_NONE:
1010 		return 0;
1011 	case RTE_FLOW_CONV_OP_ATTR:
1012 		attr = src;
1013 		if (size > sizeof(*attr))
1014 			size = sizeof(*attr);
1015 		rte_memcpy(dst, attr, size);
1016 		return sizeof(*attr);
1017 	case RTE_FLOW_CONV_OP_ITEM:
1018 		return rte_flow_conv_pattern(dst, size, src, 1, error);
1019 	case RTE_FLOW_CONV_OP_ACTION:
1020 		return rte_flow_conv_actions(dst, size, src, 1, error);
1021 	case RTE_FLOW_CONV_OP_PATTERN:
1022 		return rte_flow_conv_pattern(dst, size, src, 0, error);
1023 	case RTE_FLOW_CONV_OP_ACTIONS:
1024 		return rte_flow_conv_actions(dst, size, src, 0, error);
1025 	case RTE_FLOW_CONV_OP_RULE:
1026 		return rte_flow_conv_rule(dst, size, src, error);
1027 	case RTE_FLOW_CONV_OP_ITEM_NAME:
1028 		return rte_flow_conv_name(0, 0, dst, size, src, error);
1029 	case RTE_FLOW_CONV_OP_ACTION_NAME:
1030 		return rte_flow_conv_name(1, 0, dst, size, src, error);
1031 	case RTE_FLOW_CONV_OP_ITEM_NAME_PTR:
1032 		return rte_flow_conv_name(0, 1, dst, size, src, error);
1033 	case RTE_FLOW_CONV_OP_ACTION_NAME_PTR:
1034 		return rte_flow_conv_name(1, 1, dst, size, src, error);
1035 	}
1036 	return rte_flow_error_set
1037 		(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1038 		 "unknown object conversion operation");
1039 }
1040 
1041 /** Store a full rte_flow description. */
1042 size_t
1043 rte_flow_copy(struct rte_flow_desc *desc, size_t len,
1044 	      const struct rte_flow_attr *attr,
1045 	      const struct rte_flow_item *items,
1046 	      const struct rte_flow_action *actions)
1047 {
1048 	/*
1049 	 * Overlap struct rte_flow_conv with struct rte_flow_desc in order
1050 	 * to convert the former to the latter without wasting space.
1051 	 */
1052 	struct rte_flow_conv_rule *dst =
1053 		len ?
1054 		(void *)((uintptr_t)desc +
1055 			 (offsetof(struct rte_flow_desc, actions) -
1056 			  offsetof(struct rte_flow_conv_rule, actions))) :
1057 		NULL;
1058 	size_t dst_size =
1059 		len > sizeof(*desc) - sizeof(*dst) ?
1060 		len - (sizeof(*desc) - sizeof(*dst)) :
1061 		0;
1062 	struct rte_flow_conv_rule src = {
1063 		.attr_ro = NULL,
1064 		.pattern_ro = items,
1065 		.actions_ro = actions,
1066 	};
1067 	int ret;
1068 
1069 	RTE_BUILD_BUG_ON(sizeof(struct rte_flow_desc) <
1070 			 sizeof(struct rte_flow_conv_rule));
1071 	if (dst_size &&
1072 	    (&dst->pattern != &desc->items ||
1073 	     &dst->actions != &desc->actions ||
1074 	     (uintptr_t)(dst + 1) != (uintptr_t)(desc + 1))) {
1075 		rte_errno = EINVAL;
1076 		return 0;
1077 	}
1078 	ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, dst, dst_size, &src, NULL);
1079 	if (ret < 0)
1080 		return 0;
1081 	ret += sizeof(*desc) - sizeof(*dst);
1082 	rte_memcpy(desc,
1083 		   (&(struct rte_flow_desc){
1084 			.size = ret,
1085 			.attr = *attr,
1086 			.items = dst_size ? dst->pattern : NULL,
1087 			.actions = dst_size ? dst->actions : NULL,
1088 		   }),
1089 		   len > sizeof(*desc) ? sizeof(*desc) : len);
1090 	return ret;
1091 }
1092 
1093 int
1094 rte_flow_dev_dump(uint16_t port_id, struct rte_flow *flow,
1095 			FILE *file, struct rte_flow_error *error)
1096 {
1097 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1098 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1099 	int ret;
1100 
1101 	if (unlikely(!ops))
1102 		return -rte_errno;
1103 	if (likely(!!ops->dev_dump)) {
1104 		fts_enter(dev);
1105 		ret = ops->dev_dump(dev, flow, file, error);
1106 		fts_exit(dev);
1107 		return flow_err(port_id, ret, error);
1108 	}
1109 	return rte_flow_error_set(error, ENOSYS,
1110 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1111 				  NULL, rte_strerror(ENOSYS));
1112 }
1113 
1114 int
1115 rte_flow_get_aged_flows(uint16_t port_id, void **contexts,
1116 		    uint32_t nb_contexts, struct rte_flow_error *error)
1117 {
1118 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1119 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1120 	int ret;
1121 
1122 	if (unlikely(!ops))
1123 		return -rte_errno;
1124 	if (likely(!!ops->get_aged_flows)) {
1125 		fts_enter(dev);
1126 		ret = ops->get_aged_flows(dev, contexts, nb_contexts, error);
1127 		fts_exit(dev);
1128 		return flow_err(port_id, ret, error);
1129 	}
1130 	return rte_flow_error_set(error, ENOTSUP,
1131 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1132 				  NULL, rte_strerror(ENOTSUP));
1133 }
1134 
1135 int
1136 rte_flow_get_q_aged_flows(uint16_t port_id, uint32_t queue_id, void **contexts,
1137 			  uint32_t nb_contexts, struct rte_flow_error *error)
1138 {
1139 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1140 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1141 	int ret;
1142 
1143 	if (unlikely(!ops))
1144 		return -rte_errno;
1145 	if (likely(!!ops->get_q_aged_flows)) {
1146 		fts_enter(dev);
1147 		ret = ops->get_q_aged_flows(dev, queue_id, contexts,
1148 					    nb_contexts, error);
1149 		fts_exit(dev);
1150 		return flow_err(port_id, ret, error);
1151 	}
1152 	return rte_flow_error_set(error, ENOTSUP,
1153 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1154 				  NULL, rte_strerror(ENOTSUP));
1155 }
1156 
1157 struct rte_flow_action_handle *
1158 rte_flow_action_handle_create(uint16_t port_id,
1159 			      const struct rte_flow_indir_action_conf *conf,
1160 			      const struct rte_flow_action *action,
1161 			      struct rte_flow_error *error)
1162 {
1163 	struct rte_flow_action_handle *handle;
1164 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1165 
1166 	if (unlikely(!ops))
1167 		return NULL;
1168 	if (unlikely(!ops->action_handle_create)) {
1169 		rte_flow_error_set(error, ENOSYS,
1170 				   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1171 				   rte_strerror(ENOSYS));
1172 		return NULL;
1173 	}
1174 	handle = ops->action_handle_create(&rte_eth_devices[port_id],
1175 					   conf, action, error);
1176 	if (handle == NULL)
1177 		flow_err(port_id, -rte_errno, error);
1178 	return handle;
1179 }
1180 
1181 int
1182 rte_flow_action_handle_destroy(uint16_t port_id,
1183 			       struct rte_flow_action_handle *handle,
1184 			       struct rte_flow_error *error)
1185 {
1186 	int ret;
1187 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1188 
1189 	if (unlikely(!ops))
1190 		return -rte_errno;
1191 	if (unlikely(!ops->action_handle_destroy))
1192 		return rte_flow_error_set(error, ENOSYS,
1193 					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1194 					  NULL, rte_strerror(ENOSYS));
1195 	ret = ops->action_handle_destroy(&rte_eth_devices[port_id],
1196 					 handle, error);
1197 	return flow_err(port_id, ret, error);
1198 }
1199 
1200 int
1201 rte_flow_action_handle_update(uint16_t port_id,
1202 			      struct rte_flow_action_handle *handle,
1203 			      const void *update,
1204 			      struct rte_flow_error *error)
1205 {
1206 	int ret;
1207 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1208 
1209 	if (unlikely(!ops))
1210 		return -rte_errno;
1211 	if (unlikely(!ops->action_handle_update))
1212 		return rte_flow_error_set(error, ENOSYS,
1213 					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1214 					  NULL, rte_strerror(ENOSYS));
1215 	ret = ops->action_handle_update(&rte_eth_devices[port_id], handle,
1216 					update, error);
1217 	return flow_err(port_id, ret, error);
1218 }
1219 
1220 int
1221 rte_flow_action_handle_query(uint16_t port_id,
1222 			     const struct rte_flow_action_handle *handle,
1223 			     void *data,
1224 			     struct rte_flow_error *error)
1225 {
1226 	int ret;
1227 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1228 
1229 	if (unlikely(!ops))
1230 		return -rte_errno;
1231 	if (unlikely(!ops->action_handle_query))
1232 		return rte_flow_error_set(error, ENOSYS,
1233 					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1234 					  NULL, rte_strerror(ENOSYS));
1235 	ret = ops->action_handle_query(&rte_eth_devices[port_id], handle,
1236 				       data, error);
1237 	return flow_err(port_id, ret, error);
1238 }
1239 
1240 int
1241 rte_flow_tunnel_decap_set(uint16_t port_id,
1242 			  struct rte_flow_tunnel *tunnel,
1243 			  struct rte_flow_action **actions,
1244 			  uint32_t *num_of_actions,
1245 			  struct rte_flow_error *error)
1246 {
1247 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1248 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1249 
1250 	if (unlikely(!ops))
1251 		return -rte_errno;
1252 	if (likely(!!ops->tunnel_decap_set)) {
1253 		return flow_err(port_id,
1254 				ops->tunnel_decap_set(dev, tunnel, actions,
1255 						      num_of_actions, error),
1256 				error);
1257 	}
1258 	return rte_flow_error_set(error, ENOTSUP,
1259 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1260 				  NULL, rte_strerror(ENOTSUP));
1261 }
1262 
1263 int
1264 rte_flow_tunnel_match(uint16_t port_id,
1265 		      struct rte_flow_tunnel *tunnel,
1266 		      struct rte_flow_item **items,
1267 		      uint32_t *num_of_items,
1268 		      struct rte_flow_error *error)
1269 {
1270 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1271 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1272 
1273 	if (unlikely(!ops))
1274 		return -rte_errno;
1275 	if (likely(!!ops->tunnel_match)) {
1276 		return flow_err(port_id,
1277 				ops->tunnel_match(dev, tunnel, items,
1278 						  num_of_items, error),
1279 				error);
1280 	}
1281 	return rte_flow_error_set(error, ENOTSUP,
1282 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1283 				  NULL, rte_strerror(ENOTSUP));
1284 }
1285 
1286 int
1287 rte_flow_get_restore_info(uint16_t port_id,
1288 			  struct rte_mbuf *m,
1289 			  struct rte_flow_restore_info *restore_info,
1290 			  struct rte_flow_error *error)
1291 {
1292 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1293 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1294 
1295 	if (unlikely(!ops))
1296 		return -rte_errno;
1297 	if (likely(!!ops->get_restore_info)) {
1298 		return flow_err(port_id,
1299 				ops->get_restore_info(dev, m, restore_info,
1300 						      error),
1301 				error);
1302 	}
1303 	return rte_flow_error_set(error, ENOTSUP,
1304 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1305 				  NULL, rte_strerror(ENOTSUP));
1306 }
1307 
1308 int
1309 rte_flow_tunnel_action_decap_release(uint16_t port_id,
1310 				     struct rte_flow_action *actions,
1311 				     uint32_t num_of_actions,
1312 				     struct rte_flow_error *error)
1313 {
1314 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1315 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1316 
1317 	if (unlikely(!ops))
1318 		return -rte_errno;
1319 	if (likely(!!ops->tunnel_action_decap_release)) {
1320 		return flow_err(port_id,
1321 				ops->tunnel_action_decap_release(dev, actions,
1322 								 num_of_actions,
1323 								 error),
1324 				error);
1325 	}
1326 	return rte_flow_error_set(error, ENOTSUP,
1327 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1328 				  NULL, rte_strerror(ENOTSUP));
1329 }
1330 
1331 int
1332 rte_flow_tunnel_item_release(uint16_t port_id,
1333 			     struct rte_flow_item *items,
1334 			     uint32_t num_of_items,
1335 			     struct rte_flow_error *error)
1336 {
1337 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1338 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1339 
1340 	if (unlikely(!ops))
1341 		return -rte_errno;
1342 	if (likely(!!ops->tunnel_item_release)) {
1343 		return flow_err(port_id,
1344 				ops->tunnel_item_release(dev, items,
1345 							 num_of_items, error),
1346 				error);
1347 	}
1348 	return rte_flow_error_set(error, ENOTSUP,
1349 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1350 				  NULL, rte_strerror(ENOTSUP));
1351 }
1352 
1353 int
1354 rte_flow_pick_transfer_proxy(uint16_t port_id, uint16_t *proxy_port_id,
1355 			     struct rte_flow_error *error)
1356 {
1357 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1358 	struct rte_eth_dev *dev;
1359 
1360 	if (unlikely(ops == NULL))
1361 		return -rte_errno;
1362 
1363 	if (ops->pick_transfer_proxy == NULL) {
1364 		*proxy_port_id = port_id;
1365 		return 0;
1366 	}
1367 
1368 	dev = &rte_eth_devices[port_id];
1369 
1370 	return flow_err(port_id,
1371 			ops->pick_transfer_proxy(dev, proxy_port_id, error),
1372 			error);
1373 }
1374 
1375 struct rte_flow_item_flex_handle *
1376 rte_flow_flex_item_create(uint16_t port_id,
1377 			  const struct rte_flow_item_flex_conf *conf,
1378 			  struct rte_flow_error *error)
1379 {
1380 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1381 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1382 	struct rte_flow_item_flex_handle *handle;
1383 
1384 	if (unlikely(!ops))
1385 		return NULL;
1386 	if (unlikely(!ops->flex_item_create)) {
1387 		rte_flow_error_set(error, ENOTSUP,
1388 				   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1389 				   NULL, rte_strerror(ENOTSUP));
1390 		return NULL;
1391 	}
1392 	handle = ops->flex_item_create(dev, conf, error);
1393 	if (handle == NULL)
1394 		flow_err(port_id, -rte_errno, error);
1395 	return handle;
1396 }
1397 
1398 int
1399 rte_flow_flex_item_release(uint16_t port_id,
1400 			   const struct rte_flow_item_flex_handle *handle,
1401 			   struct rte_flow_error *error)
1402 {
1403 	int ret;
1404 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1405 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1406 
1407 	if (unlikely(!ops || !ops->flex_item_release))
1408 		return rte_flow_error_set(error, ENOTSUP,
1409 					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1410 					  NULL, rte_strerror(ENOTSUP));
1411 	ret = ops->flex_item_release(dev, handle, error);
1412 	return flow_err(port_id, ret, error);
1413 }
1414 
1415 int
1416 rte_flow_info_get(uint16_t port_id,
1417 		  struct rte_flow_port_info *port_info,
1418 		  struct rte_flow_queue_info *queue_info,
1419 		  struct rte_flow_error *error)
1420 {
1421 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1422 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1423 
1424 	if (unlikely(!ops))
1425 		return -rte_errno;
1426 	if (dev->data->dev_configured == 0) {
1427 		RTE_FLOW_LOG(INFO,
1428 			"Device with port_id=%"PRIu16" is not configured.\n",
1429 			port_id);
1430 		return -EINVAL;
1431 	}
1432 	if (port_info == NULL) {
1433 		RTE_FLOW_LOG(ERR, "Port %"PRIu16" info is NULL.\n", port_id);
1434 		return -EINVAL;
1435 	}
1436 	if (likely(!!ops->info_get)) {
1437 		return flow_err(port_id,
1438 				ops->info_get(dev, port_info, queue_info, error),
1439 				error);
1440 	}
1441 	return rte_flow_error_set(error, ENOTSUP,
1442 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1443 				  NULL, rte_strerror(ENOTSUP));
1444 }
1445 
1446 int
1447 rte_flow_configure(uint16_t port_id,
1448 		   const struct rte_flow_port_attr *port_attr,
1449 		   uint16_t nb_queue,
1450 		   const struct rte_flow_queue_attr *queue_attr[],
1451 		   struct rte_flow_error *error)
1452 {
1453 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1454 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1455 	int ret;
1456 
1457 	if (unlikely(!ops))
1458 		return -rte_errno;
1459 	if (dev->data->dev_configured == 0) {
1460 		RTE_FLOW_LOG(INFO,
1461 			"Device with port_id=%"PRIu16" is not configured.\n",
1462 			port_id);
1463 		return -EINVAL;
1464 	}
1465 	if (dev->data->dev_started != 0) {
1466 		RTE_FLOW_LOG(INFO,
1467 			"Device with port_id=%"PRIu16" already started.\n",
1468 			port_id);
1469 		return -EINVAL;
1470 	}
1471 	if (port_attr == NULL) {
1472 		RTE_FLOW_LOG(ERR, "Port %"PRIu16" info is NULL.\n", port_id);
1473 		return -EINVAL;
1474 	}
1475 	if (queue_attr == NULL) {
1476 		RTE_FLOW_LOG(ERR, "Port %"PRIu16" queue info is NULL.\n", port_id);
1477 		return -EINVAL;
1478 	}
1479 	if (likely(!!ops->configure)) {
1480 		ret = ops->configure(dev, port_attr, nb_queue, queue_attr, error);
1481 		if (ret == 0)
1482 			dev->data->flow_configured = 1;
1483 		return flow_err(port_id, ret, error);
1484 	}
1485 	return rte_flow_error_set(error, ENOTSUP,
1486 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1487 				  NULL, rte_strerror(ENOTSUP));
1488 }
1489 
1490 struct rte_flow_pattern_template *
1491 rte_flow_pattern_template_create(uint16_t port_id,
1492 		const struct rte_flow_pattern_template_attr *template_attr,
1493 		const struct rte_flow_item pattern[],
1494 		struct rte_flow_error *error)
1495 {
1496 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1497 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1498 	struct rte_flow_pattern_template *template;
1499 
1500 	if (unlikely(!ops))
1501 		return NULL;
1502 	if (dev->data->flow_configured == 0) {
1503 		RTE_FLOW_LOG(INFO,
1504 			"Flow engine on port_id=%"PRIu16" is not configured.\n",
1505 			port_id);
1506 		rte_flow_error_set(error, EINVAL,
1507 				RTE_FLOW_ERROR_TYPE_STATE,
1508 				NULL, rte_strerror(EINVAL));
1509 		return NULL;
1510 	}
1511 	if (template_attr == NULL) {
1512 		RTE_FLOW_LOG(ERR,
1513 			     "Port %"PRIu16" template attr is NULL.\n",
1514 			     port_id);
1515 		rte_flow_error_set(error, EINVAL,
1516 				   RTE_FLOW_ERROR_TYPE_ATTR,
1517 				   NULL, rte_strerror(EINVAL));
1518 		return NULL;
1519 	}
1520 	if (pattern == NULL) {
1521 		RTE_FLOW_LOG(ERR,
1522 			     "Port %"PRIu16" pattern is NULL.\n",
1523 			     port_id);
1524 		rte_flow_error_set(error, EINVAL,
1525 				   RTE_FLOW_ERROR_TYPE_ATTR,
1526 				   NULL, rte_strerror(EINVAL));
1527 		return NULL;
1528 	}
1529 	if (likely(!!ops->pattern_template_create)) {
1530 		template = ops->pattern_template_create(dev, template_attr,
1531 							pattern, error);
1532 		if (template == NULL)
1533 			flow_err(port_id, -rte_errno, error);
1534 		return template;
1535 	}
1536 	rte_flow_error_set(error, ENOTSUP,
1537 			   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1538 			   NULL, rte_strerror(ENOTSUP));
1539 	return NULL;
1540 }
1541 
1542 int
1543 rte_flow_pattern_template_destroy(uint16_t port_id,
1544 		struct rte_flow_pattern_template *pattern_template,
1545 		struct rte_flow_error *error)
1546 {
1547 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1548 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1549 
1550 	if (unlikely(!ops))
1551 		return -rte_errno;
1552 	if (unlikely(pattern_template == NULL))
1553 		return 0;
1554 	if (likely(!!ops->pattern_template_destroy)) {
1555 		return flow_err(port_id,
1556 				ops->pattern_template_destroy(dev,
1557 							      pattern_template,
1558 							      error),
1559 				error);
1560 	}
1561 	return rte_flow_error_set(error, ENOTSUP,
1562 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1563 				  NULL, rte_strerror(ENOTSUP));
1564 }
1565 
1566 struct rte_flow_actions_template *
1567 rte_flow_actions_template_create(uint16_t port_id,
1568 			const struct rte_flow_actions_template_attr *template_attr,
1569 			const struct rte_flow_action actions[],
1570 			const struct rte_flow_action masks[],
1571 			struct rte_flow_error *error)
1572 {
1573 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1574 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1575 	struct rte_flow_actions_template *template;
1576 
1577 	if (unlikely(!ops))
1578 		return NULL;
1579 	if (dev->data->flow_configured == 0) {
1580 		RTE_FLOW_LOG(INFO,
1581 			"Flow engine on port_id=%"PRIu16" is not configured.\n",
1582 			port_id);
1583 		rte_flow_error_set(error, EINVAL,
1584 				   RTE_FLOW_ERROR_TYPE_STATE,
1585 				   NULL, rte_strerror(EINVAL));
1586 		return NULL;
1587 	}
1588 	if (template_attr == NULL) {
1589 		RTE_FLOW_LOG(ERR,
1590 			     "Port %"PRIu16" template attr is NULL.\n",
1591 			     port_id);
1592 		rte_flow_error_set(error, EINVAL,
1593 				   RTE_FLOW_ERROR_TYPE_ATTR,
1594 				   NULL, rte_strerror(EINVAL));
1595 		return NULL;
1596 	}
1597 	if (actions == NULL) {
1598 		RTE_FLOW_LOG(ERR,
1599 			     "Port %"PRIu16" actions is NULL.\n",
1600 			     port_id);
1601 		rte_flow_error_set(error, EINVAL,
1602 				   RTE_FLOW_ERROR_TYPE_ATTR,
1603 				   NULL, rte_strerror(EINVAL));
1604 		return NULL;
1605 	}
1606 	if (masks == NULL) {
1607 		RTE_FLOW_LOG(ERR,
1608 			     "Port %"PRIu16" masks is NULL.\n",
1609 			     port_id);
1610 		rte_flow_error_set(error, EINVAL,
1611 				   RTE_FLOW_ERROR_TYPE_ATTR,
1612 				   NULL, rte_strerror(EINVAL));
1613 
1614 	}
1615 	if (likely(!!ops->actions_template_create)) {
1616 		template = ops->actions_template_create(dev, template_attr,
1617 							actions, masks, error);
1618 		if (template == NULL)
1619 			flow_err(port_id, -rte_errno, error);
1620 		return template;
1621 	}
1622 	rte_flow_error_set(error, ENOTSUP,
1623 			   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1624 			   NULL, rte_strerror(ENOTSUP));
1625 	return NULL;
1626 }
1627 
1628 int
1629 rte_flow_actions_template_destroy(uint16_t port_id,
1630 			struct rte_flow_actions_template *actions_template,
1631 			struct rte_flow_error *error)
1632 {
1633 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1634 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1635 
1636 	if (unlikely(!ops))
1637 		return -rte_errno;
1638 	if (unlikely(actions_template == NULL))
1639 		return 0;
1640 	if (likely(!!ops->actions_template_destroy)) {
1641 		return flow_err(port_id,
1642 				ops->actions_template_destroy(dev,
1643 							      actions_template,
1644 							      error),
1645 				error);
1646 	}
1647 	return rte_flow_error_set(error, ENOTSUP,
1648 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1649 				  NULL, rte_strerror(ENOTSUP));
1650 }
1651 
1652 struct rte_flow_template_table *
1653 rte_flow_template_table_create(uint16_t port_id,
1654 			const struct rte_flow_template_table_attr *table_attr,
1655 			struct rte_flow_pattern_template *pattern_templates[],
1656 			uint8_t nb_pattern_templates,
1657 			struct rte_flow_actions_template *actions_templates[],
1658 			uint8_t nb_actions_templates,
1659 			struct rte_flow_error *error)
1660 {
1661 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1662 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1663 	struct rte_flow_template_table *table;
1664 
1665 	if (unlikely(!ops))
1666 		return NULL;
1667 	if (dev->data->flow_configured == 0) {
1668 		RTE_FLOW_LOG(INFO,
1669 			"Flow engine on port_id=%"PRIu16" is not configured.\n",
1670 			port_id);
1671 		rte_flow_error_set(error, EINVAL,
1672 				   RTE_FLOW_ERROR_TYPE_STATE,
1673 				   NULL, rte_strerror(EINVAL));
1674 		return NULL;
1675 	}
1676 	if (table_attr == NULL) {
1677 		RTE_FLOW_LOG(ERR,
1678 			     "Port %"PRIu16" table attr is NULL.\n",
1679 			     port_id);
1680 		rte_flow_error_set(error, EINVAL,
1681 				   RTE_FLOW_ERROR_TYPE_ATTR,
1682 				   NULL, rte_strerror(EINVAL));
1683 		return NULL;
1684 	}
1685 	if (pattern_templates == NULL) {
1686 		RTE_FLOW_LOG(ERR,
1687 			     "Port %"PRIu16" pattern templates is NULL.\n",
1688 			     port_id);
1689 		rte_flow_error_set(error, EINVAL,
1690 				   RTE_FLOW_ERROR_TYPE_ATTR,
1691 				   NULL, rte_strerror(EINVAL));
1692 		return NULL;
1693 	}
1694 	if (actions_templates == NULL) {
1695 		RTE_FLOW_LOG(ERR,
1696 			     "Port %"PRIu16" actions templates is NULL.\n",
1697 			     port_id);
1698 		rte_flow_error_set(error, EINVAL,
1699 				   RTE_FLOW_ERROR_TYPE_ATTR,
1700 				   NULL, rte_strerror(EINVAL));
1701 		return NULL;
1702 	}
1703 	if (likely(!!ops->template_table_create)) {
1704 		table = ops->template_table_create(dev, table_attr,
1705 					pattern_templates, nb_pattern_templates,
1706 					actions_templates, nb_actions_templates,
1707 					error);
1708 		if (table == NULL)
1709 			flow_err(port_id, -rte_errno, error);
1710 		return table;
1711 	}
1712 	rte_flow_error_set(error, ENOTSUP,
1713 			   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1714 			   NULL, rte_strerror(ENOTSUP));
1715 	return NULL;
1716 }
1717 
1718 int
1719 rte_flow_template_table_destroy(uint16_t port_id,
1720 				struct rte_flow_template_table *template_table,
1721 				struct rte_flow_error *error)
1722 {
1723 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1724 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1725 
1726 	if (unlikely(!ops))
1727 		return -rte_errno;
1728 	if (unlikely(template_table == NULL))
1729 		return 0;
1730 	if (likely(!!ops->template_table_destroy)) {
1731 		return flow_err(port_id,
1732 				ops->template_table_destroy(dev,
1733 							    template_table,
1734 							    error),
1735 				error);
1736 	}
1737 	return rte_flow_error_set(error, ENOTSUP,
1738 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1739 				  NULL, rte_strerror(ENOTSUP));
1740 }
1741 
1742 struct rte_flow *
1743 rte_flow_async_create(uint16_t port_id,
1744 		      uint32_t queue_id,
1745 		      const struct rte_flow_op_attr *op_attr,
1746 		      struct rte_flow_template_table *template_table,
1747 		      const struct rte_flow_item pattern[],
1748 		      uint8_t pattern_template_index,
1749 		      const struct rte_flow_action actions[],
1750 		      uint8_t actions_template_index,
1751 		      void *user_data,
1752 		      struct rte_flow_error *error)
1753 {
1754 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1755 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1756 	struct rte_flow *flow;
1757 
1758 	flow = ops->async_create(dev, queue_id,
1759 				 op_attr, template_table,
1760 				 pattern, pattern_template_index,
1761 				 actions, actions_template_index,
1762 				 user_data, error);
1763 	if (flow == NULL)
1764 		flow_err(port_id, -rte_errno, error);
1765 	return flow;
1766 }
1767 
1768 int
1769 rte_flow_async_destroy(uint16_t port_id,
1770 		       uint32_t queue_id,
1771 		       const struct rte_flow_op_attr *op_attr,
1772 		       struct rte_flow *flow,
1773 		       void *user_data,
1774 		       struct rte_flow_error *error)
1775 {
1776 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1777 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1778 
1779 	return flow_err(port_id,
1780 			ops->async_destroy(dev, queue_id,
1781 					   op_attr, flow,
1782 					   user_data, error),
1783 			error);
1784 }
1785 
1786 int
1787 rte_flow_push(uint16_t port_id,
1788 	      uint32_t queue_id,
1789 	      struct rte_flow_error *error)
1790 {
1791 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1792 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1793 
1794 	return flow_err(port_id,
1795 			ops->push(dev, queue_id, error),
1796 			error);
1797 }
1798 
1799 int
1800 rte_flow_pull(uint16_t port_id,
1801 	      uint32_t queue_id,
1802 	      struct rte_flow_op_result res[],
1803 	      uint16_t n_res,
1804 	      struct rte_flow_error *error)
1805 {
1806 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1807 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1808 	int ret;
1809 
1810 	ret = ops->pull(dev, queue_id, res, n_res, error);
1811 	return ret ? ret : flow_err(port_id, ret, error);
1812 }
1813 
1814 struct rte_flow_action_handle *
1815 rte_flow_async_action_handle_create(uint16_t port_id,
1816 		uint32_t queue_id,
1817 		const struct rte_flow_op_attr *op_attr,
1818 		const struct rte_flow_indir_action_conf *indir_action_conf,
1819 		const struct rte_flow_action *action,
1820 		void *user_data,
1821 		struct rte_flow_error *error)
1822 {
1823 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1824 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1825 	struct rte_flow_action_handle *handle;
1826 
1827 	handle = ops->async_action_handle_create(dev, queue_id, op_attr,
1828 					     indir_action_conf, action, user_data, error);
1829 	if (handle == NULL)
1830 		flow_err(port_id, -rte_errno, error);
1831 	return handle;
1832 }
1833 
1834 int
1835 rte_flow_async_action_handle_destroy(uint16_t port_id,
1836 		uint32_t queue_id,
1837 		const struct rte_flow_op_attr *op_attr,
1838 		struct rte_flow_action_handle *action_handle,
1839 		void *user_data,
1840 		struct rte_flow_error *error)
1841 {
1842 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1843 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1844 	int ret;
1845 
1846 	ret = ops->async_action_handle_destroy(dev, queue_id, op_attr,
1847 					   action_handle, user_data, error);
1848 	return flow_err(port_id, ret, error);
1849 }
1850 
1851 int
1852 rte_flow_async_action_handle_update(uint16_t port_id,
1853 		uint32_t queue_id,
1854 		const struct rte_flow_op_attr *op_attr,
1855 		struct rte_flow_action_handle *action_handle,
1856 		const void *update,
1857 		void *user_data,
1858 		struct rte_flow_error *error)
1859 {
1860 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1861 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1862 	int ret;
1863 
1864 	ret = ops->async_action_handle_update(dev, queue_id, op_attr,
1865 					  action_handle, update, user_data, error);
1866 	return flow_err(port_id, ret, error);
1867 }
1868 
1869 int
1870 rte_flow_async_action_handle_query(uint16_t port_id,
1871 		uint32_t queue_id,
1872 		const struct rte_flow_op_attr *op_attr,
1873 		const struct rte_flow_action_handle *action_handle,
1874 		void *data,
1875 		void *user_data,
1876 		struct rte_flow_error *error)
1877 {
1878 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1879 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1880 	int ret;
1881 
1882 	ret = ops->async_action_handle_query(dev, queue_id, op_attr,
1883 					  action_handle, data, user_data, error);
1884 	return flow_err(port_id, ret, error);
1885 }
1886