xref: /dpdk/lib/ethdev/rte_flow.c (revision 6cf72047)
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 #include <string.h>
10 
11 #include <rte_common.h>
12 #include <rte_errno.h>
13 #include <rte_branch_prediction.h>
14 #include <rte_string_fns.h>
15 #include <rte_mbuf.h>
16 #include <rte_mbuf_dyn.h>
17 #include "rte_ethdev.h"
18 #include "rte_flow_driver.h"
19 #include "rte_flow.h"
20 
21 /* Mbuf dynamic field name for metadata. */
22 int32_t rte_flow_dynf_metadata_offs = -1;
23 
24 /* Mbuf dynamic field flag bit number for metadata. */
25 uint64_t rte_flow_dynf_metadata_mask;
26 
27 /**
28  * Flow elements description tables.
29  */
30 struct rte_flow_desc_data {
31 	const char *name;
32 	size_t size;
33 	size_t (*desc_fn)(void *dst, const void *src);
34 };
35 
36 /**
37  *
38  * @param buf
39  * Destination memory.
40  * @param data
41  * Source memory
42  * @param size
43  * Requested copy size
44  * @param desc
45  * rte_flow_desc_item - for flow item conversion.
46  * rte_flow_desc_action - for flow action conversion.
47  * @param type
48  * Offset into the desc param or negative value for private flow elements.
49  */
50 static inline size_t
51 rte_flow_conv_copy(void *buf, const void *data, const size_t size,
52 		   const struct rte_flow_desc_data *desc, int type)
53 {
54 	/**
55 	 * Allow PMD private flow item
56 	 */
57 	size_t sz = type >= 0 ? 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 (desc[type].desc_fn)
62 		sz += desc[type].desc_fn(size > 0 ? buf : NULL, data);
63 	return sz;
64 }
65 
66 /** Generate flow_item[] entry. */
67 #define MK_FLOW_ITEM(t, s) \
68 	[RTE_FLOW_ITEM_TYPE_ ## t] = { \
69 		.name = # t, \
70 		.size = s,               \
71 		.desc_fn = NULL,\
72 	}
73 
74 #define MK_FLOW_ITEM_FN(t, s, fn) \
75 	[RTE_FLOW_ITEM_TYPE_ ## t] = {\
76 		.name = # t,                 \
77 		.size = s,                   \
78 		.desc_fn = fn,               \
79 	}
80 
81 /** Information about known flow pattern items. */
82 static const struct rte_flow_desc_data rte_flow_desc_item[] = {
83 	MK_FLOW_ITEM(END, 0),
84 	MK_FLOW_ITEM(VOID, 0),
85 	MK_FLOW_ITEM(INVERT, 0),
86 	MK_FLOW_ITEM(ANY, sizeof(struct rte_flow_item_any)),
87 	MK_FLOW_ITEM(PF, 0),
88 	MK_FLOW_ITEM(VF, sizeof(struct rte_flow_item_vf)),
89 	MK_FLOW_ITEM(PHY_PORT, sizeof(struct rte_flow_item_phy_port)),
90 	MK_FLOW_ITEM(PORT_ID, sizeof(struct rte_flow_item_port_id)),
91 	MK_FLOW_ITEM(RAW, sizeof(struct rte_flow_item_raw)),
92 	MK_FLOW_ITEM(ETH, sizeof(struct rte_flow_item_eth)),
93 	MK_FLOW_ITEM(VLAN, sizeof(struct rte_flow_item_vlan)),
94 	MK_FLOW_ITEM(IPV4, sizeof(struct rte_flow_item_ipv4)),
95 	MK_FLOW_ITEM(IPV6, sizeof(struct rte_flow_item_ipv6)),
96 	MK_FLOW_ITEM(ICMP, sizeof(struct rte_flow_item_icmp)),
97 	MK_FLOW_ITEM(UDP, sizeof(struct rte_flow_item_udp)),
98 	MK_FLOW_ITEM(TCP, sizeof(struct rte_flow_item_tcp)),
99 	MK_FLOW_ITEM(SCTP, sizeof(struct rte_flow_item_sctp)),
100 	MK_FLOW_ITEM(VXLAN, sizeof(struct rte_flow_item_vxlan)),
101 	MK_FLOW_ITEM(E_TAG, sizeof(struct rte_flow_item_e_tag)),
102 	MK_FLOW_ITEM(NVGRE, sizeof(struct rte_flow_item_nvgre)),
103 	MK_FLOW_ITEM(MPLS, sizeof(struct rte_flow_item_mpls)),
104 	MK_FLOW_ITEM(GRE, sizeof(struct rte_flow_item_gre)),
105 	MK_FLOW_ITEM(FUZZY, sizeof(struct rte_flow_item_fuzzy)),
106 	MK_FLOW_ITEM(GTP, sizeof(struct rte_flow_item_gtp)),
107 	MK_FLOW_ITEM(GTPC, sizeof(struct rte_flow_item_gtp)),
108 	MK_FLOW_ITEM(GTPU, sizeof(struct rte_flow_item_gtp)),
109 	MK_FLOW_ITEM(ESP, sizeof(struct rte_flow_item_esp)),
110 	MK_FLOW_ITEM(GENEVE, sizeof(struct rte_flow_item_geneve)),
111 	MK_FLOW_ITEM(VXLAN_GPE, sizeof(struct rte_flow_item_vxlan_gpe)),
112 	MK_FLOW_ITEM(ARP_ETH_IPV4, sizeof(struct rte_flow_item_arp_eth_ipv4)),
113 	MK_FLOW_ITEM(IPV6_EXT, sizeof(struct rte_flow_item_ipv6_ext)),
114 	MK_FLOW_ITEM(IPV6_FRAG_EXT, sizeof(struct rte_flow_item_ipv6_frag_ext)),
115 	MK_FLOW_ITEM(ICMP6, sizeof(struct rte_flow_item_icmp6)),
116 	MK_FLOW_ITEM(ICMP6_ND_NS, sizeof(struct rte_flow_item_icmp6_nd_ns)),
117 	MK_FLOW_ITEM(ICMP6_ND_NA, sizeof(struct rte_flow_item_icmp6_nd_na)),
118 	MK_FLOW_ITEM(ICMP6_ND_OPT, sizeof(struct rte_flow_item_icmp6_nd_opt)),
119 	MK_FLOW_ITEM(ICMP6_ND_OPT_SLA_ETH,
120 		     sizeof(struct rte_flow_item_icmp6_nd_opt_sla_eth)),
121 	MK_FLOW_ITEM(ICMP6_ND_OPT_TLA_ETH,
122 		     sizeof(struct rte_flow_item_icmp6_nd_opt_tla_eth)),
123 	MK_FLOW_ITEM(MARK, sizeof(struct rte_flow_item_mark)),
124 	MK_FLOW_ITEM(META, sizeof(struct rte_flow_item_meta)),
125 	MK_FLOW_ITEM(TAG, sizeof(struct rte_flow_item_tag)),
126 	MK_FLOW_ITEM(GRE_KEY, sizeof(rte_be32_t)),
127 	MK_FLOW_ITEM(GTP_PSC, sizeof(struct rte_flow_item_gtp_psc)),
128 	MK_FLOW_ITEM(PPPOES, sizeof(struct rte_flow_item_pppoe)),
129 	MK_FLOW_ITEM(PPPOED, sizeof(struct rte_flow_item_pppoe)),
130 	MK_FLOW_ITEM(PPPOE_PROTO_ID,
131 			sizeof(struct rte_flow_item_pppoe_proto_id)),
132 	MK_FLOW_ITEM(NSH, sizeof(struct rte_flow_item_nsh)),
133 	MK_FLOW_ITEM(IGMP, sizeof(struct rte_flow_item_igmp)),
134 	MK_FLOW_ITEM(AH, sizeof(struct rte_flow_item_ah)),
135 	MK_FLOW_ITEM(HIGIG2, sizeof(struct rte_flow_item_higig2_hdr)),
136 	MK_FLOW_ITEM(L2TPV3OIP, sizeof(struct rte_flow_item_l2tpv3oip)),
137 	MK_FLOW_ITEM(PFCP, sizeof(struct rte_flow_item_pfcp)),
138 	MK_FLOW_ITEM(ECPRI, sizeof(struct rte_flow_item_ecpri)),
139 	MK_FLOW_ITEM(GENEVE_OPT, sizeof(struct rte_flow_item_geneve_opt)),
140 	MK_FLOW_ITEM(INTEGRITY, sizeof(struct rte_flow_item_integrity)),
141 	MK_FLOW_ITEM(CONNTRACK, sizeof(uint32_t)),
142 	MK_FLOW_ITEM(PORT_REPRESENTOR, sizeof(struct rte_flow_item_ethdev)),
143 	MK_FLOW_ITEM(REPRESENTED_PORT, sizeof(struct rte_flow_item_ethdev)),
144 };
145 
146 /** Generate flow_action[] entry. */
147 #define MK_FLOW_ACTION(t, s) \
148 	[RTE_FLOW_ACTION_TYPE_ ## t] = { \
149 		.name = # t, \
150 		.size = s, \
151 		.desc_fn = NULL,\
152 	}
153 
154 #define MK_FLOW_ACTION_FN(t, fn) \
155 	[RTE_FLOW_ACTION_TYPE_ ## t] = { \
156 		.name = # t, \
157 		.size = 0, \
158 		.desc_fn = fn,\
159 	}
160 
161 
162 /** Information about known flow actions. */
163 static const struct rte_flow_desc_data rte_flow_desc_action[] = {
164 	MK_FLOW_ACTION(END, 0),
165 	MK_FLOW_ACTION(VOID, 0),
166 	MK_FLOW_ACTION(PASSTHRU, 0),
167 	MK_FLOW_ACTION(JUMP, sizeof(struct rte_flow_action_jump)),
168 	MK_FLOW_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
169 	MK_FLOW_ACTION(FLAG, 0),
170 	MK_FLOW_ACTION(QUEUE, sizeof(struct rte_flow_action_queue)),
171 	MK_FLOW_ACTION(DROP, 0),
172 	MK_FLOW_ACTION(COUNT, sizeof(struct rte_flow_action_count)),
173 	MK_FLOW_ACTION(RSS, sizeof(struct rte_flow_action_rss)),
174 	MK_FLOW_ACTION(PF, 0),
175 	MK_FLOW_ACTION(VF, sizeof(struct rte_flow_action_vf)),
176 	MK_FLOW_ACTION(PHY_PORT, sizeof(struct rte_flow_action_phy_port)),
177 	MK_FLOW_ACTION(PORT_ID, sizeof(struct rte_flow_action_port_id)),
178 	MK_FLOW_ACTION(METER, sizeof(struct rte_flow_action_meter)),
179 	MK_FLOW_ACTION(SECURITY, sizeof(struct rte_flow_action_security)),
180 	MK_FLOW_ACTION(OF_SET_MPLS_TTL,
181 		       sizeof(struct rte_flow_action_of_set_mpls_ttl)),
182 	MK_FLOW_ACTION(OF_DEC_MPLS_TTL, 0),
183 	MK_FLOW_ACTION(OF_SET_NW_TTL,
184 		       sizeof(struct rte_flow_action_of_set_nw_ttl)),
185 	MK_FLOW_ACTION(OF_DEC_NW_TTL, 0),
186 	MK_FLOW_ACTION(OF_COPY_TTL_OUT, 0),
187 	MK_FLOW_ACTION(OF_COPY_TTL_IN, 0),
188 	MK_FLOW_ACTION(OF_POP_VLAN, 0),
189 	MK_FLOW_ACTION(OF_PUSH_VLAN,
190 		       sizeof(struct rte_flow_action_of_push_vlan)),
191 	MK_FLOW_ACTION(OF_SET_VLAN_VID,
192 		       sizeof(struct rte_flow_action_of_set_vlan_vid)),
193 	MK_FLOW_ACTION(OF_SET_VLAN_PCP,
194 		       sizeof(struct rte_flow_action_of_set_vlan_pcp)),
195 	MK_FLOW_ACTION(OF_POP_MPLS,
196 		       sizeof(struct rte_flow_action_of_pop_mpls)),
197 	MK_FLOW_ACTION(OF_PUSH_MPLS,
198 		       sizeof(struct rte_flow_action_of_push_mpls)),
199 	MK_FLOW_ACTION(VXLAN_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
200 	MK_FLOW_ACTION(VXLAN_DECAP, 0),
201 	MK_FLOW_ACTION(NVGRE_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
202 	MK_FLOW_ACTION(NVGRE_DECAP, 0),
203 	MK_FLOW_ACTION(RAW_ENCAP, sizeof(struct rte_flow_action_raw_encap)),
204 	MK_FLOW_ACTION(RAW_DECAP, sizeof(struct rte_flow_action_raw_decap)),
205 	MK_FLOW_ACTION(SET_IPV4_SRC,
206 		       sizeof(struct rte_flow_action_set_ipv4)),
207 	MK_FLOW_ACTION(SET_IPV4_DST,
208 		       sizeof(struct rte_flow_action_set_ipv4)),
209 	MK_FLOW_ACTION(SET_IPV6_SRC,
210 		       sizeof(struct rte_flow_action_set_ipv6)),
211 	MK_FLOW_ACTION(SET_IPV6_DST,
212 		       sizeof(struct rte_flow_action_set_ipv6)),
213 	MK_FLOW_ACTION(SET_TP_SRC,
214 		       sizeof(struct rte_flow_action_set_tp)),
215 	MK_FLOW_ACTION(SET_TP_DST,
216 		       sizeof(struct rte_flow_action_set_tp)),
217 	MK_FLOW_ACTION(MAC_SWAP, 0),
218 	MK_FLOW_ACTION(DEC_TTL, 0),
219 	MK_FLOW_ACTION(SET_TTL, sizeof(struct rte_flow_action_set_ttl)),
220 	MK_FLOW_ACTION(SET_MAC_SRC, sizeof(struct rte_flow_action_set_mac)),
221 	MK_FLOW_ACTION(SET_MAC_DST, sizeof(struct rte_flow_action_set_mac)),
222 	MK_FLOW_ACTION(INC_TCP_SEQ, sizeof(rte_be32_t)),
223 	MK_FLOW_ACTION(DEC_TCP_SEQ, sizeof(rte_be32_t)),
224 	MK_FLOW_ACTION(INC_TCP_ACK, sizeof(rte_be32_t)),
225 	MK_FLOW_ACTION(DEC_TCP_ACK, sizeof(rte_be32_t)),
226 	MK_FLOW_ACTION(SET_TAG, sizeof(struct rte_flow_action_set_tag)),
227 	MK_FLOW_ACTION(SET_META, sizeof(struct rte_flow_action_set_meta)),
228 	MK_FLOW_ACTION(SET_IPV4_DSCP, sizeof(struct rte_flow_action_set_dscp)),
229 	MK_FLOW_ACTION(SET_IPV6_DSCP, sizeof(struct rte_flow_action_set_dscp)),
230 	MK_FLOW_ACTION(AGE, sizeof(struct rte_flow_action_age)),
231 	MK_FLOW_ACTION(SAMPLE, sizeof(struct rte_flow_action_sample)),
232 	MK_FLOW_ACTION(MODIFY_FIELD,
233 		       sizeof(struct rte_flow_action_modify_field)),
234 	/**
235 	 * Indirect action represented as handle of type
236 	 * (struct rte_flow_action_handle *) stored in conf field (see
237 	 * struct rte_flow_action); no need for additional structure to * store
238 	 * indirect action handle.
239 	 */
240 	MK_FLOW_ACTION(INDIRECT, 0),
241 	MK_FLOW_ACTION(CONNTRACK, sizeof(struct rte_flow_action_conntrack)),
242 	MK_FLOW_ACTION(PORT_REPRESENTOR, sizeof(struct rte_flow_action_ethdev)),
243 	MK_FLOW_ACTION(REPRESENTED_PORT, sizeof(struct rte_flow_action_ethdev)),
244 };
245 
246 int
247 rte_flow_dynf_metadata_register(void)
248 {
249 	int offset;
250 	int flag;
251 
252 	static const struct rte_mbuf_dynfield desc_offs = {
253 		.name = RTE_MBUF_DYNFIELD_METADATA_NAME,
254 		.size = sizeof(uint32_t),
255 		.align = __alignof__(uint32_t),
256 	};
257 	static const struct rte_mbuf_dynflag desc_flag = {
258 		.name = RTE_MBUF_DYNFLAG_METADATA_NAME,
259 	};
260 
261 	offset = rte_mbuf_dynfield_register(&desc_offs);
262 	if (offset < 0)
263 		goto error;
264 	flag = rte_mbuf_dynflag_register(&desc_flag);
265 	if (flag < 0)
266 		goto error;
267 	rte_flow_dynf_metadata_offs = offset;
268 	rte_flow_dynf_metadata_mask = (1ULL << flag);
269 	return 0;
270 
271 error:
272 	rte_flow_dynf_metadata_offs = -1;
273 	rte_flow_dynf_metadata_mask = 0ULL;
274 	return -rte_errno;
275 }
276 
277 static inline void
278 fts_enter(struct rte_eth_dev *dev)
279 {
280 	if (!(dev->data->dev_flags & RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE))
281 		pthread_mutex_lock(&dev->data->flow_ops_mutex);
282 }
283 
284 static inline void
285 fts_exit(struct rte_eth_dev *dev)
286 {
287 	if (!(dev->data->dev_flags & RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE))
288 		pthread_mutex_unlock(&dev->data->flow_ops_mutex);
289 }
290 
291 static int
292 flow_err(uint16_t port_id, int ret, struct rte_flow_error *error)
293 {
294 	if (ret == 0)
295 		return 0;
296 	if (rte_eth_dev_is_removed(port_id))
297 		return rte_flow_error_set(error, EIO,
298 					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
299 					  NULL, rte_strerror(EIO));
300 	return ret;
301 }
302 
303 /* Get generic flow operations structure from a port. */
304 const struct rte_flow_ops *
305 rte_flow_ops_get(uint16_t port_id, struct rte_flow_error *error)
306 {
307 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
308 	const struct rte_flow_ops *ops;
309 	int code;
310 
311 	if (unlikely(!rte_eth_dev_is_valid_port(port_id)))
312 		code = ENODEV;
313 	else if (unlikely(dev->dev_ops->flow_ops_get == NULL))
314 		/* flow API not supported with this driver dev_ops */
315 		code = ENOSYS;
316 	else
317 		code = dev->dev_ops->flow_ops_get(dev, &ops);
318 	if (code == 0 && ops == NULL)
319 		/* flow API not supported with this device */
320 		code = ENOSYS;
321 
322 	if (code != 0) {
323 		rte_flow_error_set(error, code, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
324 				   NULL, rte_strerror(code));
325 		return NULL;
326 	}
327 	return ops;
328 }
329 
330 /* Check whether a flow rule can be created on a given port. */
331 int
332 rte_flow_validate(uint16_t port_id,
333 		  const struct rte_flow_attr *attr,
334 		  const struct rte_flow_item pattern[],
335 		  const struct rte_flow_action actions[],
336 		  struct rte_flow_error *error)
337 {
338 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
339 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
340 	int ret;
341 
342 	if (unlikely(!ops))
343 		return -rte_errno;
344 	if (likely(!!ops->validate)) {
345 		fts_enter(dev);
346 		ret = ops->validate(dev, attr, pattern, actions, error);
347 		fts_exit(dev);
348 		return flow_err(port_id, ret, error);
349 	}
350 	return rte_flow_error_set(error, ENOSYS,
351 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
352 				  NULL, rte_strerror(ENOSYS));
353 }
354 
355 /* Create a flow rule on a given port. */
356 struct rte_flow *
357 rte_flow_create(uint16_t port_id,
358 		const struct rte_flow_attr *attr,
359 		const struct rte_flow_item pattern[],
360 		const struct rte_flow_action actions[],
361 		struct rte_flow_error *error)
362 {
363 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
364 	struct rte_flow *flow;
365 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
366 
367 	if (unlikely(!ops))
368 		return NULL;
369 	if (likely(!!ops->create)) {
370 		fts_enter(dev);
371 		flow = ops->create(dev, attr, pattern, actions, error);
372 		fts_exit(dev);
373 		if (flow == NULL)
374 			flow_err(port_id, -rte_errno, error);
375 		return flow;
376 	}
377 	rte_flow_error_set(error, ENOSYS, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
378 			   NULL, rte_strerror(ENOSYS));
379 	return NULL;
380 }
381 
382 /* Destroy a flow rule on a given port. */
383 int
384 rte_flow_destroy(uint16_t port_id,
385 		 struct rte_flow *flow,
386 		 struct rte_flow_error *error)
387 {
388 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
389 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
390 	int ret;
391 
392 	if (unlikely(!ops))
393 		return -rte_errno;
394 	if (likely(!!ops->destroy)) {
395 		fts_enter(dev);
396 		ret = ops->destroy(dev, flow, error);
397 		fts_exit(dev);
398 		return flow_err(port_id, ret, error);
399 	}
400 	return rte_flow_error_set(error, ENOSYS,
401 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
402 				  NULL, rte_strerror(ENOSYS));
403 }
404 
405 /* Destroy all flow rules associated with a port. */
406 int
407 rte_flow_flush(uint16_t port_id,
408 	       struct rte_flow_error *error)
409 {
410 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
411 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
412 	int ret;
413 
414 	if (unlikely(!ops))
415 		return -rte_errno;
416 	if (likely(!!ops->flush)) {
417 		fts_enter(dev);
418 		ret = ops->flush(dev, error);
419 		fts_exit(dev);
420 		return flow_err(port_id, ret, error);
421 	}
422 	return rte_flow_error_set(error, ENOSYS,
423 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
424 				  NULL, rte_strerror(ENOSYS));
425 }
426 
427 /* Query an existing flow rule. */
428 int
429 rte_flow_query(uint16_t port_id,
430 	       struct rte_flow *flow,
431 	       const struct rte_flow_action *action,
432 	       void *data,
433 	       struct rte_flow_error *error)
434 {
435 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
436 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
437 	int ret;
438 
439 	if (!ops)
440 		return -rte_errno;
441 	if (likely(!!ops->query)) {
442 		fts_enter(dev);
443 		ret = ops->query(dev, flow, action, data, error);
444 		fts_exit(dev);
445 		return flow_err(port_id, ret, error);
446 	}
447 	return rte_flow_error_set(error, ENOSYS,
448 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
449 				  NULL, rte_strerror(ENOSYS));
450 }
451 
452 /* Restrict ingress traffic to the defined flow rules. */
453 int
454 rte_flow_isolate(uint16_t port_id,
455 		 int set,
456 		 struct rte_flow_error *error)
457 {
458 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
459 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
460 	int ret;
461 
462 	if (!ops)
463 		return -rte_errno;
464 	if (likely(!!ops->isolate)) {
465 		fts_enter(dev);
466 		ret = ops->isolate(dev, set, error);
467 		fts_exit(dev);
468 		return flow_err(port_id, ret, error);
469 	}
470 	return rte_flow_error_set(error, ENOSYS,
471 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
472 				  NULL, rte_strerror(ENOSYS));
473 }
474 
475 /* Initialize flow error structure. */
476 int
477 rte_flow_error_set(struct rte_flow_error *error,
478 		   int code,
479 		   enum rte_flow_error_type type,
480 		   const void *cause,
481 		   const char *message)
482 {
483 	if (error) {
484 		*error = (struct rte_flow_error){
485 			.type = type,
486 			.cause = cause,
487 			.message = message,
488 		};
489 	}
490 	rte_errno = code;
491 	return -code;
492 }
493 
494 /** Pattern item specification types. */
495 enum rte_flow_conv_item_spec_type {
496 	RTE_FLOW_CONV_ITEM_SPEC,
497 	RTE_FLOW_CONV_ITEM_LAST,
498 	RTE_FLOW_CONV_ITEM_MASK,
499 };
500 
501 /**
502  * Copy pattern item specification.
503  *
504  * @param[out] buf
505  *   Output buffer. Can be NULL if @p size is zero.
506  * @param size
507  *   Size of @p buf in bytes.
508  * @param[in] item
509  *   Pattern item to copy specification from.
510  * @param type
511  *   Specification selector for either @p spec, @p last or @p mask.
512  *
513  * @return
514  *   Number of bytes needed to store pattern item specification regardless
515  *   of @p size. @p buf contents are truncated to @p size if not large
516  *   enough.
517  */
518 static size_t
519 rte_flow_conv_item_spec(void *buf, const size_t size,
520 			const struct rte_flow_item *item,
521 			enum rte_flow_conv_item_spec_type type)
522 {
523 	size_t off;
524 	const void *data =
525 		type == RTE_FLOW_CONV_ITEM_SPEC ? item->spec :
526 		type == RTE_FLOW_CONV_ITEM_LAST ? item->last :
527 		type == RTE_FLOW_CONV_ITEM_MASK ? item->mask :
528 		NULL;
529 
530 	switch (item->type) {
531 		union {
532 			const struct rte_flow_item_raw *raw;
533 		} spec;
534 		union {
535 			const struct rte_flow_item_raw *raw;
536 		} last;
537 		union {
538 			const struct rte_flow_item_raw *raw;
539 		} mask;
540 		union {
541 			const struct rte_flow_item_raw *raw;
542 		} src;
543 		union {
544 			struct rte_flow_item_raw *raw;
545 		} dst;
546 		size_t tmp;
547 
548 	case RTE_FLOW_ITEM_TYPE_RAW:
549 		spec.raw = item->spec;
550 		last.raw = item->last ? item->last : item->spec;
551 		mask.raw = item->mask ? item->mask : &rte_flow_item_raw_mask;
552 		src.raw = data;
553 		dst.raw = buf;
554 		rte_memcpy(dst.raw,
555 			   (&(struct rte_flow_item_raw){
556 				.relative = src.raw->relative,
557 				.search = src.raw->search,
558 				.reserved = src.raw->reserved,
559 				.offset = src.raw->offset,
560 				.limit = src.raw->limit,
561 				.length = src.raw->length,
562 			   }),
563 			   size > sizeof(*dst.raw) ? sizeof(*dst.raw) : size);
564 		off = sizeof(*dst.raw);
565 		if (type == RTE_FLOW_CONV_ITEM_SPEC ||
566 		    (type == RTE_FLOW_CONV_ITEM_MASK &&
567 		     ((spec.raw->length & mask.raw->length) >=
568 		      (last.raw->length & mask.raw->length))))
569 			tmp = spec.raw->length & mask.raw->length;
570 		else
571 			tmp = last.raw->length & mask.raw->length;
572 		if (tmp) {
573 			off = RTE_ALIGN_CEIL(off, sizeof(*dst.raw->pattern));
574 			if (size >= off + tmp)
575 				dst.raw->pattern = rte_memcpy
576 					((void *)((uintptr_t)dst.raw + off),
577 					 src.raw->pattern, tmp);
578 			off += tmp;
579 		}
580 		break;
581 	default:
582 		off = rte_flow_conv_copy(buf, data, size,
583 					 rte_flow_desc_item, item->type);
584 		break;
585 	}
586 	return off;
587 }
588 
589 /**
590  * Copy action configuration.
591  *
592  * @param[out] buf
593  *   Output buffer. Can be NULL if @p size is zero.
594  * @param size
595  *   Size of @p buf in bytes.
596  * @param[in] action
597  *   Action to copy configuration from.
598  *
599  * @return
600  *   Number of bytes needed to store pattern item specification regardless
601  *   of @p size. @p buf contents are truncated to @p size if not large
602  *   enough.
603  */
604 static size_t
605 rte_flow_conv_action_conf(void *buf, const size_t size,
606 			  const struct rte_flow_action *action)
607 {
608 	size_t off;
609 
610 	switch (action->type) {
611 		union {
612 			const struct rte_flow_action_rss *rss;
613 			const struct rte_flow_action_vxlan_encap *vxlan_encap;
614 			const struct rte_flow_action_nvgre_encap *nvgre_encap;
615 		} src;
616 		union {
617 			struct rte_flow_action_rss *rss;
618 			struct rte_flow_action_vxlan_encap *vxlan_encap;
619 			struct rte_flow_action_nvgre_encap *nvgre_encap;
620 		} dst;
621 		size_t tmp;
622 		int ret;
623 
624 	case RTE_FLOW_ACTION_TYPE_RSS:
625 		src.rss = action->conf;
626 		dst.rss = buf;
627 		rte_memcpy(dst.rss,
628 			   (&(struct rte_flow_action_rss){
629 				.func = src.rss->func,
630 				.level = src.rss->level,
631 				.types = src.rss->types,
632 				.key_len = src.rss->key_len,
633 				.queue_num = src.rss->queue_num,
634 			   }),
635 			   size > sizeof(*dst.rss) ? sizeof(*dst.rss) : size);
636 		off = sizeof(*dst.rss);
637 		if (src.rss->key_len && src.rss->key) {
638 			off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->key));
639 			tmp = sizeof(*src.rss->key) * src.rss->key_len;
640 			if (size >= off + tmp)
641 				dst.rss->key = rte_memcpy
642 					((void *)((uintptr_t)dst.rss + off),
643 					 src.rss->key, tmp);
644 			off += tmp;
645 		}
646 		if (src.rss->queue_num) {
647 			off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->queue));
648 			tmp = sizeof(*src.rss->queue) * src.rss->queue_num;
649 			if (size >= off + tmp)
650 				dst.rss->queue = rte_memcpy
651 					((void *)((uintptr_t)dst.rss + off),
652 					 src.rss->queue, tmp);
653 			off += tmp;
654 		}
655 		break;
656 	case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
657 	case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
658 		src.vxlan_encap = action->conf;
659 		dst.vxlan_encap = buf;
660 		RTE_BUILD_BUG_ON(sizeof(*src.vxlan_encap) !=
661 				 sizeof(*src.nvgre_encap) ||
662 				 offsetof(struct rte_flow_action_vxlan_encap,
663 					  definition) !=
664 				 offsetof(struct rte_flow_action_nvgre_encap,
665 					  definition));
666 		off = sizeof(*dst.vxlan_encap);
667 		if (src.vxlan_encap->definition) {
668 			off = RTE_ALIGN_CEIL
669 				(off, sizeof(*dst.vxlan_encap->definition));
670 			ret = rte_flow_conv
671 				(RTE_FLOW_CONV_OP_PATTERN,
672 				 (void *)((uintptr_t)dst.vxlan_encap + off),
673 				 size > off ? size - off : 0,
674 				 src.vxlan_encap->definition, NULL);
675 			if (ret < 0)
676 				return 0;
677 			if (size >= off + ret)
678 				dst.vxlan_encap->definition =
679 					(void *)((uintptr_t)dst.vxlan_encap +
680 						 off);
681 			off += ret;
682 		}
683 		break;
684 	default:
685 		off = rte_flow_conv_copy(buf, action->conf, size,
686 					 rte_flow_desc_action, action->type);
687 		break;
688 	}
689 	return off;
690 }
691 
692 /**
693  * Copy a list of pattern items.
694  *
695  * @param[out] dst
696  *   Destination buffer. Can be NULL if @p size is zero.
697  * @param size
698  *   Size of @p dst in bytes.
699  * @param[in] src
700  *   Source pattern items.
701  * @param num
702  *   Maximum number of pattern items to process from @p src or 0 to process
703  *   the entire list. In both cases, processing stops after
704  *   RTE_FLOW_ITEM_TYPE_END is encountered.
705  * @param[out] error
706  *   Perform verbose error reporting if not NULL.
707  *
708  * @return
709  *   A positive value representing the number of bytes needed to store
710  *   pattern items regardless of @p size on success (@p buf contents are
711  *   truncated to @p size if not large enough), a negative errno value
712  *   otherwise and rte_errno is set.
713  */
714 static int
715 rte_flow_conv_pattern(struct rte_flow_item *dst,
716 		      const size_t size,
717 		      const struct rte_flow_item *src,
718 		      unsigned int num,
719 		      struct rte_flow_error *error)
720 {
721 	uintptr_t data = (uintptr_t)dst;
722 	size_t off;
723 	size_t ret;
724 	unsigned int i;
725 
726 	for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
727 		/**
728 		 * allow PMD private flow item
729 		 */
730 		if (((int)src->type >= 0) &&
731 			((size_t)src->type >= RTE_DIM(rte_flow_desc_item) ||
732 		    !rte_flow_desc_item[src->type].name))
733 			return rte_flow_error_set
734 				(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, src,
735 				 "cannot convert unknown item type");
736 		if (size >= off + sizeof(*dst))
737 			*dst = (struct rte_flow_item){
738 				.type = src->type,
739 			};
740 		off += sizeof(*dst);
741 		if (!src->type)
742 			num = i + 1;
743 	}
744 	num = i;
745 	src -= num;
746 	dst -= num;
747 	do {
748 		if (src->spec) {
749 			off = RTE_ALIGN_CEIL(off, sizeof(double));
750 			ret = rte_flow_conv_item_spec
751 				((void *)(data + off),
752 				 size > off ? size - off : 0, src,
753 				 RTE_FLOW_CONV_ITEM_SPEC);
754 			if (size && size >= off + ret)
755 				dst->spec = (void *)(data + off);
756 			off += ret;
757 
758 		}
759 		if (src->last) {
760 			off = RTE_ALIGN_CEIL(off, sizeof(double));
761 			ret = rte_flow_conv_item_spec
762 				((void *)(data + off),
763 				 size > off ? size - off : 0, src,
764 				 RTE_FLOW_CONV_ITEM_LAST);
765 			if (size && size >= off + ret)
766 				dst->last = (void *)(data + off);
767 			off += ret;
768 		}
769 		if (src->mask) {
770 			off = RTE_ALIGN_CEIL(off, sizeof(double));
771 			ret = rte_flow_conv_item_spec
772 				((void *)(data + off),
773 				 size > off ? size - off : 0, src,
774 				 RTE_FLOW_CONV_ITEM_MASK);
775 			if (size && size >= off + ret)
776 				dst->mask = (void *)(data + off);
777 			off += ret;
778 		}
779 		++src;
780 		++dst;
781 	} while (--num);
782 	return off;
783 }
784 
785 /**
786  * Copy a list of actions.
787  *
788  * @param[out] dst
789  *   Destination buffer. Can be NULL if @p size is zero.
790  * @param size
791  *   Size of @p dst in bytes.
792  * @param[in] src
793  *   Source actions.
794  * @param num
795  *   Maximum number of actions to process from @p src or 0 to process the
796  *   entire list. In both cases, processing stops after
797  *   RTE_FLOW_ACTION_TYPE_END is encountered.
798  * @param[out] error
799  *   Perform verbose error reporting if not NULL.
800  *
801  * @return
802  *   A positive value representing the number of bytes needed to store
803  *   actions regardless of @p size on success (@p buf contents are truncated
804  *   to @p size if not large enough), a negative errno value otherwise and
805  *   rte_errno is set.
806  */
807 static int
808 rte_flow_conv_actions(struct rte_flow_action *dst,
809 		      const size_t size,
810 		      const struct rte_flow_action *src,
811 		      unsigned int num,
812 		      struct rte_flow_error *error)
813 {
814 	uintptr_t data = (uintptr_t)dst;
815 	size_t off;
816 	size_t ret;
817 	unsigned int i;
818 
819 	for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
820 		/**
821 		 * allow PMD private flow action
822 		 */
823 		if (((int)src->type >= 0) &&
824 		    ((size_t)src->type >= RTE_DIM(rte_flow_desc_action) ||
825 		    !rte_flow_desc_action[src->type].name))
826 			return rte_flow_error_set
827 				(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
828 				 src, "cannot convert unknown action type");
829 		if (size >= off + sizeof(*dst))
830 			*dst = (struct rte_flow_action){
831 				.type = src->type,
832 			};
833 		off += sizeof(*dst);
834 		if (!src->type)
835 			num = i + 1;
836 	}
837 	num = i;
838 	src -= num;
839 	dst -= num;
840 	do {
841 		if (src->conf) {
842 			off = RTE_ALIGN_CEIL(off, sizeof(double));
843 			ret = rte_flow_conv_action_conf
844 				((void *)(data + off),
845 				 size > off ? size - off : 0, src);
846 			if (size && size >= off + ret)
847 				dst->conf = (void *)(data + off);
848 			off += ret;
849 		}
850 		++src;
851 		++dst;
852 	} while (--num);
853 	return off;
854 }
855 
856 /**
857  * Copy flow rule components.
858  *
859  * This comprises the flow rule descriptor itself, attributes, pattern and
860  * actions list. NULL components in @p src are skipped.
861  *
862  * @param[out] dst
863  *   Destination buffer. Can be NULL if @p size is zero.
864  * @param size
865  *   Size of @p dst in bytes.
866  * @param[in] src
867  *   Source flow rule descriptor.
868  * @param[out] error
869  *   Perform verbose error reporting if not NULL.
870  *
871  * @return
872  *   A positive value representing the number of bytes needed to store all
873  *   components including the descriptor regardless of @p size on success
874  *   (@p buf contents are truncated to @p size if not large enough), a
875  *   negative errno value otherwise and rte_errno is set.
876  */
877 static int
878 rte_flow_conv_rule(struct rte_flow_conv_rule *dst,
879 		   const size_t size,
880 		   const struct rte_flow_conv_rule *src,
881 		   struct rte_flow_error *error)
882 {
883 	size_t off;
884 	int ret;
885 
886 	rte_memcpy(dst,
887 		   (&(struct rte_flow_conv_rule){
888 			.attr = NULL,
889 			.pattern = NULL,
890 			.actions = NULL,
891 		   }),
892 		   size > sizeof(*dst) ? sizeof(*dst) : size);
893 	off = sizeof(*dst);
894 	if (src->attr_ro) {
895 		off = RTE_ALIGN_CEIL(off, sizeof(double));
896 		if (size && size >= off + sizeof(*dst->attr))
897 			dst->attr = rte_memcpy
898 				((void *)((uintptr_t)dst + off),
899 				 src->attr_ro, sizeof(*dst->attr));
900 		off += sizeof(*dst->attr);
901 	}
902 	if (src->pattern_ro) {
903 		off = RTE_ALIGN_CEIL(off, sizeof(double));
904 		ret = rte_flow_conv_pattern((void *)((uintptr_t)dst + off),
905 					    size > off ? size - off : 0,
906 					    src->pattern_ro, 0, error);
907 		if (ret < 0)
908 			return ret;
909 		if (size && size >= off + (size_t)ret)
910 			dst->pattern = (void *)((uintptr_t)dst + off);
911 		off += ret;
912 	}
913 	if (src->actions_ro) {
914 		off = RTE_ALIGN_CEIL(off, sizeof(double));
915 		ret = rte_flow_conv_actions((void *)((uintptr_t)dst + off),
916 					    size > off ? size - off : 0,
917 					    src->actions_ro, 0, error);
918 		if (ret < 0)
919 			return ret;
920 		if (size >= off + (size_t)ret)
921 			dst->actions = (void *)((uintptr_t)dst + off);
922 		off += ret;
923 	}
924 	return off;
925 }
926 
927 /**
928  * Retrieve the name of a pattern item/action type.
929  *
930  * @param is_action
931  *   Nonzero when @p src represents an action type instead of a pattern item
932  *   type.
933  * @param is_ptr
934  *   Nonzero to write string address instead of contents into @p dst.
935  * @param[out] dst
936  *   Destination buffer. Can be NULL if @p size is zero.
937  * @param size
938  *   Size of @p dst in bytes.
939  * @param[in] src
940  *   Depending on @p is_action, source pattern item or action type cast as a
941  *   pointer.
942  * @param[out] error
943  *   Perform verbose error reporting if not NULL.
944  *
945  * @return
946  *   A positive value representing the number of bytes needed to store the
947  *   name or its address regardless of @p size on success (@p buf contents
948  *   are truncated to @p size if not large enough), a negative errno value
949  *   otherwise and rte_errno is set.
950  */
951 static int
952 rte_flow_conv_name(int is_action,
953 		   int is_ptr,
954 		   char *dst,
955 		   const size_t size,
956 		   const void *src,
957 		   struct rte_flow_error *error)
958 {
959 	struct desc_info {
960 		const struct rte_flow_desc_data *data;
961 		size_t num;
962 	};
963 	static const struct desc_info info_rep[2] = {
964 		{ rte_flow_desc_item, RTE_DIM(rte_flow_desc_item), },
965 		{ rte_flow_desc_action, RTE_DIM(rte_flow_desc_action), },
966 	};
967 	const struct desc_info *const info = &info_rep[!!is_action];
968 	unsigned int type = (uintptr_t)src;
969 
970 	if (type >= info->num)
971 		return rte_flow_error_set
972 			(error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
973 			 "unknown object type to retrieve the name of");
974 	if (!is_ptr)
975 		return strlcpy(dst, info->data[type].name, size);
976 	if (size >= sizeof(const char **))
977 		*((const char **)dst) = info->data[type].name;
978 	return sizeof(const char **);
979 }
980 
981 /** Helper function to convert flow API objects. */
982 int
983 rte_flow_conv(enum rte_flow_conv_op op,
984 	      void *dst,
985 	      size_t size,
986 	      const void *src,
987 	      struct rte_flow_error *error)
988 {
989 	switch (op) {
990 		const struct rte_flow_attr *attr;
991 
992 	case RTE_FLOW_CONV_OP_NONE:
993 		return 0;
994 	case RTE_FLOW_CONV_OP_ATTR:
995 		attr = src;
996 		if (size > sizeof(*attr))
997 			size = sizeof(*attr);
998 		rte_memcpy(dst, attr, size);
999 		return sizeof(*attr);
1000 	case RTE_FLOW_CONV_OP_ITEM:
1001 		return rte_flow_conv_pattern(dst, size, src, 1, error);
1002 	case RTE_FLOW_CONV_OP_ACTION:
1003 		return rte_flow_conv_actions(dst, size, src, 1, error);
1004 	case RTE_FLOW_CONV_OP_PATTERN:
1005 		return rte_flow_conv_pattern(dst, size, src, 0, error);
1006 	case RTE_FLOW_CONV_OP_ACTIONS:
1007 		return rte_flow_conv_actions(dst, size, src, 0, error);
1008 	case RTE_FLOW_CONV_OP_RULE:
1009 		return rte_flow_conv_rule(dst, size, src, error);
1010 	case RTE_FLOW_CONV_OP_ITEM_NAME:
1011 		return rte_flow_conv_name(0, 0, dst, size, src, error);
1012 	case RTE_FLOW_CONV_OP_ACTION_NAME:
1013 		return rte_flow_conv_name(1, 0, dst, size, src, error);
1014 	case RTE_FLOW_CONV_OP_ITEM_NAME_PTR:
1015 		return rte_flow_conv_name(0, 1, dst, size, src, error);
1016 	case RTE_FLOW_CONV_OP_ACTION_NAME_PTR:
1017 		return rte_flow_conv_name(1, 1, dst, size, src, error);
1018 	}
1019 	return rte_flow_error_set
1020 		(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1021 		 "unknown object conversion operation");
1022 }
1023 
1024 /** Store a full rte_flow description. */
1025 size_t
1026 rte_flow_copy(struct rte_flow_desc *desc, size_t len,
1027 	      const struct rte_flow_attr *attr,
1028 	      const struct rte_flow_item *items,
1029 	      const struct rte_flow_action *actions)
1030 {
1031 	/*
1032 	 * Overlap struct rte_flow_conv with struct rte_flow_desc in order
1033 	 * to convert the former to the latter without wasting space.
1034 	 */
1035 	struct rte_flow_conv_rule *dst =
1036 		len ?
1037 		(void *)((uintptr_t)desc +
1038 			 (offsetof(struct rte_flow_desc, actions) -
1039 			  offsetof(struct rte_flow_conv_rule, actions))) :
1040 		NULL;
1041 	size_t dst_size =
1042 		len > sizeof(*desc) - sizeof(*dst) ?
1043 		len - (sizeof(*desc) - sizeof(*dst)) :
1044 		0;
1045 	struct rte_flow_conv_rule src = {
1046 		.attr_ro = NULL,
1047 		.pattern_ro = items,
1048 		.actions_ro = actions,
1049 	};
1050 	int ret;
1051 
1052 	RTE_BUILD_BUG_ON(sizeof(struct rte_flow_desc) <
1053 			 sizeof(struct rte_flow_conv_rule));
1054 	if (dst_size &&
1055 	    (&dst->pattern != &desc->items ||
1056 	     &dst->actions != &desc->actions ||
1057 	     (uintptr_t)(dst + 1) != (uintptr_t)(desc + 1))) {
1058 		rte_errno = EINVAL;
1059 		return 0;
1060 	}
1061 	ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, dst, dst_size, &src, NULL);
1062 	if (ret < 0)
1063 		return 0;
1064 	ret += sizeof(*desc) - sizeof(*dst);
1065 	rte_memcpy(desc,
1066 		   (&(struct rte_flow_desc){
1067 			.size = ret,
1068 			.attr = *attr,
1069 			.items = dst_size ? dst->pattern : NULL,
1070 			.actions = dst_size ? dst->actions : NULL,
1071 		   }),
1072 		   len > sizeof(*desc) ? sizeof(*desc) : len);
1073 	return ret;
1074 }
1075 
1076 int
1077 rte_flow_dev_dump(uint16_t port_id, struct rte_flow *flow,
1078 			FILE *file, struct rte_flow_error *error)
1079 {
1080 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1081 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1082 	int ret;
1083 
1084 	if (unlikely(!ops))
1085 		return -rte_errno;
1086 	if (likely(!!ops->dev_dump)) {
1087 		fts_enter(dev);
1088 		ret = ops->dev_dump(dev, flow, file, error);
1089 		fts_exit(dev);
1090 		return flow_err(port_id, ret, error);
1091 	}
1092 	return rte_flow_error_set(error, ENOSYS,
1093 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1094 				  NULL, rte_strerror(ENOSYS));
1095 }
1096 
1097 int
1098 rte_flow_get_aged_flows(uint16_t port_id, void **contexts,
1099 		    uint32_t nb_contexts, struct rte_flow_error *error)
1100 {
1101 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1102 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1103 	int ret;
1104 
1105 	if (unlikely(!ops))
1106 		return -rte_errno;
1107 	if (likely(!!ops->get_aged_flows)) {
1108 		fts_enter(dev);
1109 		ret = ops->get_aged_flows(dev, contexts, nb_contexts, error);
1110 		fts_exit(dev);
1111 		return flow_err(port_id, ret, error);
1112 	}
1113 	return rte_flow_error_set(error, ENOTSUP,
1114 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1115 				  NULL, rte_strerror(ENOTSUP));
1116 }
1117 
1118 struct rte_flow_action_handle *
1119 rte_flow_action_handle_create(uint16_t port_id,
1120 			      const struct rte_flow_indir_action_conf *conf,
1121 			      const struct rte_flow_action *action,
1122 			      struct rte_flow_error *error)
1123 {
1124 	struct rte_flow_action_handle *handle;
1125 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1126 
1127 	if (unlikely(!ops))
1128 		return NULL;
1129 	if (unlikely(!ops->action_handle_create)) {
1130 		rte_flow_error_set(error, ENOSYS,
1131 				   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1132 				   rte_strerror(ENOSYS));
1133 		return NULL;
1134 	}
1135 	handle = ops->action_handle_create(&rte_eth_devices[port_id],
1136 					   conf, action, error);
1137 	if (handle == NULL)
1138 		flow_err(port_id, -rte_errno, error);
1139 	return handle;
1140 }
1141 
1142 int
1143 rte_flow_action_handle_destroy(uint16_t port_id,
1144 			       struct rte_flow_action_handle *handle,
1145 			       struct rte_flow_error *error)
1146 {
1147 	int ret;
1148 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1149 
1150 	if (unlikely(!ops))
1151 		return -rte_errno;
1152 	if (unlikely(!ops->action_handle_destroy))
1153 		return rte_flow_error_set(error, ENOSYS,
1154 					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1155 					  NULL, rte_strerror(ENOSYS));
1156 	ret = ops->action_handle_destroy(&rte_eth_devices[port_id],
1157 					 handle, error);
1158 	return flow_err(port_id, ret, error);
1159 }
1160 
1161 int
1162 rte_flow_action_handle_update(uint16_t port_id,
1163 			      struct rte_flow_action_handle *handle,
1164 			      const void *update,
1165 			      struct rte_flow_error *error)
1166 {
1167 	int ret;
1168 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1169 
1170 	if (unlikely(!ops))
1171 		return -rte_errno;
1172 	if (unlikely(!ops->action_handle_update))
1173 		return rte_flow_error_set(error, ENOSYS,
1174 					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1175 					  NULL, rte_strerror(ENOSYS));
1176 	ret = ops->action_handle_update(&rte_eth_devices[port_id], handle,
1177 					update, error);
1178 	return flow_err(port_id, ret, error);
1179 }
1180 
1181 int
1182 rte_flow_action_handle_query(uint16_t port_id,
1183 			     const struct rte_flow_action_handle *handle,
1184 			     void *data,
1185 			     struct rte_flow_error *error)
1186 {
1187 	int ret;
1188 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1189 
1190 	if (unlikely(!ops))
1191 		return -rte_errno;
1192 	if (unlikely(!ops->action_handle_query))
1193 		return rte_flow_error_set(error, ENOSYS,
1194 					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1195 					  NULL, rte_strerror(ENOSYS));
1196 	ret = ops->action_handle_query(&rte_eth_devices[port_id], handle,
1197 				       data, error);
1198 	return flow_err(port_id, ret, error);
1199 }
1200 
1201 int
1202 rte_flow_tunnel_decap_set(uint16_t port_id,
1203 			  struct rte_flow_tunnel *tunnel,
1204 			  struct rte_flow_action **actions,
1205 			  uint32_t *num_of_actions,
1206 			  struct rte_flow_error *error)
1207 {
1208 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1209 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1210 
1211 	if (unlikely(!ops))
1212 		return -rte_errno;
1213 	if (likely(!!ops->tunnel_decap_set)) {
1214 		return flow_err(port_id,
1215 				ops->tunnel_decap_set(dev, tunnel, actions,
1216 						      num_of_actions, error),
1217 				error);
1218 	}
1219 	return rte_flow_error_set(error, ENOTSUP,
1220 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1221 				  NULL, rte_strerror(ENOTSUP));
1222 }
1223 
1224 int
1225 rte_flow_tunnel_match(uint16_t port_id,
1226 		      struct rte_flow_tunnel *tunnel,
1227 		      struct rte_flow_item **items,
1228 		      uint32_t *num_of_items,
1229 		      struct rte_flow_error *error)
1230 {
1231 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1232 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1233 
1234 	if (unlikely(!ops))
1235 		return -rte_errno;
1236 	if (likely(!!ops->tunnel_match)) {
1237 		return flow_err(port_id,
1238 				ops->tunnel_match(dev, tunnel, items,
1239 						  num_of_items, error),
1240 				error);
1241 	}
1242 	return rte_flow_error_set(error, ENOTSUP,
1243 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1244 				  NULL, rte_strerror(ENOTSUP));
1245 }
1246 
1247 int
1248 rte_flow_get_restore_info(uint16_t port_id,
1249 			  struct rte_mbuf *m,
1250 			  struct rte_flow_restore_info *restore_info,
1251 			  struct rte_flow_error *error)
1252 {
1253 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1254 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1255 
1256 	if (unlikely(!ops))
1257 		return -rte_errno;
1258 	if (likely(!!ops->get_restore_info)) {
1259 		return flow_err(port_id,
1260 				ops->get_restore_info(dev, m, restore_info,
1261 						      error),
1262 				error);
1263 	}
1264 	return rte_flow_error_set(error, ENOTSUP,
1265 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1266 				  NULL, rte_strerror(ENOTSUP));
1267 }
1268 
1269 int
1270 rte_flow_tunnel_action_decap_release(uint16_t port_id,
1271 				     struct rte_flow_action *actions,
1272 				     uint32_t num_of_actions,
1273 				     struct rte_flow_error *error)
1274 {
1275 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1276 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1277 
1278 	if (unlikely(!ops))
1279 		return -rte_errno;
1280 	if (likely(!!ops->tunnel_action_decap_release)) {
1281 		return flow_err(port_id,
1282 				ops->tunnel_action_decap_release(dev, actions,
1283 								 num_of_actions,
1284 								 error),
1285 				error);
1286 	}
1287 	return rte_flow_error_set(error, ENOTSUP,
1288 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1289 				  NULL, rte_strerror(ENOTSUP));
1290 }
1291 
1292 int
1293 rte_flow_tunnel_item_release(uint16_t port_id,
1294 			     struct rte_flow_item *items,
1295 			     uint32_t num_of_items,
1296 			     struct rte_flow_error *error)
1297 {
1298 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1299 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1300 
1301 	if (unlikely(!ops))
1302 		return -rte_errno;
1303 	if (likely(!!ops->tunnel_item_release)) {
1304 		return flow_err(port_id,
1305 				ops->tunnel_item_release(dev, items,
1306 							 num_of_items, error),
1307 				error);
1308 	}
1309 	return rte_flow_error_set(error, ENOTSUP,
1310 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1311 				  NULL, rte_strerror(ENOTSUP));
1312 }
1313 
1314 int
1315 rte_flow_pick_transfer_proxy(uint16_t port_id, uint16_t *proxy_port_id,
1316 			     struct rte_flow_error *error)
1317 {
1318 	const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1319 	struct rte_eth_dev *dev;
1320 
1321 	if (unlikely(ops == NULL))
1322 		return -rte_errno;
1323 
1324 	if (ops->pick_transfer_proxy == NULL) {
1325 		*proxy_port_id = port_id;
1326 		return 0;
1327 	}
1328 
1329 	dev = &rte_eth_devices[port_id];
1330 
1331 	return flow_err(port_id,
1332 			ops->pick_transfer_proxy(dev, proxy_port_id, error),
1333 			error);
1334 }
1335