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