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