1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016 6WIND S.A.
3 * Copyright 2016 Mellanox Technologies, Ltd
11 #include <rte_common.h>
12 #include <rte_errno.h>
13 #include <rte_branch_prediction.h>
14 #include <rte_string_fns.h>
16 #include <rte_mbuf_dyn.h>
17 #include "rte_ethdev.h"
18 #include "rte_flow_driver.h"
21 /* Mbuf dynamic field name for metadata. */
22 int32_t rte_flow_dynf_metadata_offs = -1;
24 /* Mbuf dynamic field flag bit number for metadata. */
25 uint64_t rte_flow_dynf_metadata_mask;
28 * Flow elements description tables.
30 struct rte_flow_desc_data {
35 /** Generate flow_item[] entry. */
36 #define MK_FLOW_ITEM(t, s) \
37 [RTE_FLOW_ITEM_TYPE_ ## t] = { \
42 /** Information about known flow pattern items. */
43 static const struct rte_flow_desc_data rte_flow_desc_item[] = {
45 MK_FLOW_ITEM(VOID, 0),
46 MK_FLOW_ITEM(INVERT, 0),
47 MK_FLOW_ITEM(ANY, sizeof(struct rte_flow_item_any)),
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(CONNTRACK, sizeof(uint32_t)),
104 /** Generate flow_action[] entry. */
105 #define MK_FLOW_ACTION(t, s) \
106 [RTE_FLOW_ACTION_TYPE_ ## t] = { \
111 /** Information about known flow actions. */
112 static const struct rte_flow_desc_data rte_flow_desc_action[] = {
113 MK_FLOW_ACTION(END, 0),
114 MK_FLOW_ACTION(VOID, 0),
115 MK_FLOW_ACTION(PASSTHRU, 0),
116 MK_FLOW_ACTION(JUMP, sizeof(struct rte_flow_action_jump)),
117 MK_FLOW_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
118 MK_FLOW_ACTION(FLAG, 0),
119 MK_FLOW_ACTION(QUEUE, sizeof(struct rte_flow_action_queue)),
120 MK_FLOW_ACTION(DROP, 0),
121 MK_FLOW_ACTION(COUNT, sizeof(struct rte_flow_action_count)),
122 MK_FLOW_ACTION(RSS, sizeof(struct rte_flow_action_rss)),
123 MK_FLOW_ACTION(PF, 0),
124 MK_FLOW_ACTION(VF, sizeof(struct rte_flow_action_vf)),
125 MK_FLOW_ACTION(PHY_PORT, sizeof(struct rte_flow_action_phy_port)),
126 MK_FLOW_ACTION(PORT_ID, sizeof(struct rte_flow_action_port_id)),
127 MK_FLOW_ACTION(METER, sizeof(struct rte_flow_action_meter)),
128 MK_FLOW_ACTION(SECURITY, sizeof(struct rte_flow_action_security)),
129 MK_FLOW_ACTION(OF_SET_MPLS_TTL,
130 sizeof(struct rte_flow_action_of_set_mpls_ttl)),
131 MK_FLOW_ACTION(OF_DEC_MPLS_TTL, 0),
132 MK_FLOW_ACTION(OF_SET_NW_TTL,
133 sizeof(struct rte_flow_action_of_set_nw_ttl)),
134 MK_FLOW_ACTION(OF_DEC_NW_TTL, 0),
135 MK_FLOW_ACTION(OF_COPY_TTL_OUT, 0),
136 MK_FLOW_ACTION(OF_COPY_TTL_IN, 0),
137 MK_FLOW_ACTION(OF_POP_VLAN, 0),
138 MK_FLOW_ACTION(OF_PUSH_VLAN,
139 sizeof(struct rte_flow_action_of_push_vlan)),
140 MK_FLOW_ACTION(OF_SET_VLAN_VID,
141 sizeof(struct rte_flow_action_of_set_vlan_vid)),
142 MK_FLOW_ACTION(OF_SET_VLAN_PCP,
143 sizeof(struct rte_flow_action_of_set_vlan_pcp)),
144 MK_FLOW_ACTION(OF_POP_MPLS,
145 sizeof(struct rte_flow_action_of_pop_mpls)),
146 MK_FLOW_ACTION(OF_PUSH_MPLS,
147 sizeof(struct rte_flow_action_of_push_mpls)),
148 MK_FLOW_ACTION(VXLAN_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
149 MK_FLOW_ACTION(VXLAN_DECAP, 0),
150 MK_FLOW_ACTION(NVGRE_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
151 MK_FLOW_ACTION(NVGRE_DECAP, 0),
152 MK_FLOW_ACTION(RAW_ENCAP, sizeof(struct rte_flow_action_raw_encap)),
153 MK_FLOW_ACTION(RAW_DECAP, sizeof(struct rte_flow_action_raw_decap)),
154 MK_FLOW_ACTION(SET_IPV4_SRC,
155 sizeof(struct rte_flow_action_set_ipv4)),
156 MK_FLOW_ACTION(SET_IPV4_DST,
157 sizeof(struct rte_flow_action_set_ipv4)),
158 MK_FLOW_ACTION(SET_IPV6_SRC,
159 sizeof(struct rte_flow_action_set_ipv6)),
160 MK_FLOW_ACTION(SET_IPV6_DST,
161 sizeof(struct rte_flow_action_set_ipv6)),
162 MK_FLOW_ACTION(SET_TP_SRC,
163 sizeof(struct rte_flow_action_set_tp)),
164 MK_FLOW_ACTION(SET_TP_DST,
165 sizeof(struct rte_flow_action_set_tp)),
166 MK_FLOW_ACTION(MAC_SWAP, 0),
167 MK_FLOW_ACTION(DEC_TTL, 0),
168 MK_FLOW_ACTION(SET_TTL, sizeof(struct rte_flow_action_set_ttl)),
169 MK_FLOW_ACTION(SET_MAC_SRC, sizeof(struct rte_flow_action_set_mac)),
170 MK_FLOW_ACTION(SET_MAC_DST, sizeof(struct rte_flow_action_set_mac)),
171 MK_FLOW_ACTION(INC_TCP_SEQ, sizeof(rte_be32_t)),
172 MK_FLOW_ACTION(DEC_TCP_SEQ, sizeof(rte_be32_t)),
173 MK_FLOW_ACTION(INC_TCP_ACK, sizeof(rte_be32_t)),
174 MK_FLOW_ACTION(DEC_TCP_ACK, sizeof(rte_be32_t)),
175 MK_FLOW_ACTION(SET_TAG, sizeof(struct rte_flow_action_set_tag)),
176 MK_FLOW_ACTION(SET_META, sizeof(struct rte_flow_action_set_meta)),
177 MK_FLOW_ACTION(SET_IPV4_DSCP, sizeof(struct rte_flow_action_set_dscp)),
178 MK_FLOW_ACTION(SET_IPV6_DSCP, sizeof(struct rte_flow_action_set_dscp)),
179 MK_FLOW_ACTION(AGE, sizeof(struct rte_flow_action_age)),
180 MK_FLOW_ACTION(SAMPLE, sizeof(struct rte_flow_action_sample)),
181 MK_FLOW_ACTION(MODIFY_FIELD,
182 sizeof(struct rte_flow_action_modify_field)),
184 * Indirect action represented as handle of type
185 * (struct rte_flow_action_handle *) stored in conf field (see
186 * struct rte_flow_action); no need for additional structure to * store
187 * indirect action handle.
189 MK_FLOW_ACTION(INDIRECT, 0),
190 MK_FLOW_ACTION(CONNTRACK, sizeof(struct rte_flow_action_conntrack)),
194 rte_flow_dynf_metadata_register(void)
199 static const struct rte_mbuf_dynfield desc_offs = {
200 .name = RTE_MBUF_DYNFIELD_METADATA_NAME,
201 .size = sizeof(uint32_t),
202 .align = __alignof__(uint32_t),
204 static const struct rte_mbuf_dynflag desc_flag = {
205 .name = RTE_MBUF_DYNFLAG_METADATA_NAME,
208 offset = rte_mbuf_dynfield_register(&desc_offs);
211 flag = rte_mbuf_dynflag_register(&desc_flag);
214 rte_flow_dynf_metadata_offs = offset;
215 rte_flow_dynf_metadata_mask = (1ULL << flag);
219 rte_flow_dynf_metadata_offs = -1;
220 rte_flow_dynf_metadata_mask = 0ULL;
225 fts_enter(struct rte_eth_dev *dev)
227 if (!(dev->data->dev_flags & RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE))
228 pthread_mutex_lock(&dev->data->flow_ops_mutex);
232 fts_exit(struct rte_eth_dev *dev)
234 if (!(dev->data->dev_flags & RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE))
235 pthread_mutex_unlock(&dev->data->flow_ops_mutex);
239 flow_err(uint16_t port_id, int ret, struct rte_flow_error *error)
243 if (rte_eth_dev_is_removed(port_id))
244 return rte_flow_error_set(error, EIO,
245 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
246 NULL, rte_strerror(EIO));
250 /* Get generic flow operations structure from a port. */
251 const struct rte_flow_ops *
252 rte_flow_ops_get(uint16_t port_id, struct rte_flow_error *error)
254 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
255 const struct rte_flow_ops *ops;
258 if (unlikely(!rte_eth_dev_is_valid_port(port_id)))
260 else if (unlikely(dev->dev_ops->flow_ops_get == NULL))
261 /* flow API not supported with this driver dev_ops */
264 code = dev->dev_ops->flow_ops_get(dev, &ops);
265 if (code == 0 && ops == NULL)
266 /* flow API not supported with this device */
270 rte_flow_error_set(error, code, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
271 NULL, rte_strerror(code));
277 /* Check whether a flow rule can be created on a given port. */
279 rte_flow_validate(uint16_t port_id,
280 const struct rte_flow_attr *attr,
281 const struct rte_flow_item pattern[],
282 const struct rte_flow_action actions[],
283 struct rte_flow_error *error)
285 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
286 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
291 if (likely(!!ops->validate)) {
293 ret = ops->validate(dev, attr, pattern, actions, error);
295 return flow_err(port_id, ret, error);
297 return rte_flow_error_set(error, ENOSYS,
298 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
299 NULL, rte_strerror(ENOSYS));
302 /* Create a flow rule on a given port. */
304 rte_flow_create(uint16_t port_id,
305 const struct rte_flow_attr *attr,
306 const struct rte_flow_item pattern[],
307 const struct rte_flow_action actions[],
308 struct rte_flow_error *error)
310 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
311 struct rte_flow *flow;
312 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
316 if (likely(!!ops->create)) {
318 flow = ops->create(dev, attr, pattern, actions, error);
321 flow_err(port_id, -rte_errno, error);
324 rte_flow_error_set(error, ENOSYS, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
325 NULL, rte_strerror(ENOSYS));
329 /* Destroy a flow rule on a given port. */
331 rte_flow_destroy(uint16_t port_id,
332 struct rte_flow *flow,
333 struct rte_flow_error *error)
335 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
336 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
341 if (likely(!!ops->destroy)) {
343 ret = ops->destroy(dev, flow, error);
345 return flow_err(port_id, ret, error);
347 return rte_flow_error_set(error, ENOSYS,
348 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
349 NULL, rte_strerror(ENOSYS));
352 /* Destroy all flow rules associated with a port. */
354 rte_flow_flush(uint16_t port_id,
355 struct rte_flow_error *error)
357 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
358 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
363 if (likely(!!ops->flush)) {
365 ret = ops->flush(dev, error);
367 return flow_err(port_id, ret, error);
369 return rte_flow_error_set(error, ENOSYS,
370 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
371 NULL, rte_strerror(ENOSYS));
374 /* Query an existing flow rule. */
376 rte_flow_query(uint16_t port_id,
377 struct rte_flow *flow,
378 const struct rte_flow_action *action,
380 struct rte_flow_error *error)
382 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
383 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
388 if (likely(!!ops->query)) {
390 ret = ops->query(dev, flow, action, data, error);
392 return flow_err(port_id, ret, error);
394 return rte_flow_error_set(error, ENOSYS,
395 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
396 NULL, rte_strerror(ENOSYS));
399 /* Restrict ingress traffic to the defined flow rules. */
401 rte_flow_isolate(uint16_t port_id,
403 struct rte_flow_error *error)
405 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
406 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
411 if (likely(!!ops->isolate)) {
413 ret = ops->isolate(dev, set, error);
415 return flow_err(port_id, ret, error);
417 return rte_flow_error_set(error, ENOSYS,
418 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
419 NULL, rte_strerror(ENOSYS));
422 /* Initialize flow error structure. */
424 rte_flow_error_set(struct rte_flow_error *error,
426 enum rte_flow_error_type type,
431 *error = (struct rte_flow_error){
441 /** Pattern item specification types. */
442 enum rte_flow_conv_item_spec_type {
443 RTE_FLOW_CONV_ITEM_SPEC,
444 RTE_FLOW_CONV_ITEM_LAST,
445 RTE_FLOW_CONV_ITEM_MASK,
449 * Copy pattern item specification.
452 * Output buffer. Can be NULL if @p size is zero.
454 * Size of @p buf in bytes.
456 * Pattern item to copy specification from.
458 * Specification selector for either @p spec, @p last or @p mask.
461 * Number of bytes needed to store pattern item specification regardless
462 * of @p size. @p buf contents are truncated to @p size if not large
466 rte_flow_conv_item_spec(void *buf, const size_t size,
467 const struct rte_flow_item *item,
468 enum rte_flow_conv_item_spec_type type)
472 type == RTE_FLOW_CONV_ITEM_SPEC ? item->spec :
473 type == RTE_FLOW_CONV_ITEM_LAST ? item->last :
474 type == RTE_FLOW_CONV_ITEM_MASK ? item->mask :
477 switch (item->type) {
479 const struct rte_flow_item_raw *raw;
482 const struct rte_flow_item_raw *raw;
485 const struct rte_flow_item_raw *raw;
488 const struct rte_flow_item_raw *raw;
491 struct rte_flow_item_raw *raw;
495 case RTE_FLOW_ITEM_TYPE_RAW:
496 spec.raw = item->spec;
497 last.raw = item->last ? item->last : item->spec;
498 mask.raw = item->mask ? item->mask : &rte_flow_item_raw_mask;
502 (&(struct rte_flow_item_raw){
503 .relative = src.raw->relative,
504 .search = src.raw->search,
505 .reserved = src.raw->reserved,
506 .offset = src.raw->offset,
507 .limit = src.raw->limit,
508 .length = src.raw->length,
510 size > sizeof(*dst.raw) ? sizeof(*dst.raw) : size);
511 off = sizeof(*dst.raw);
512 if (type == RTE_FLOW_CONV_ITEM_SPEC ||
513 (type == RTE_FLOW_CONV_ITEM_MASK &&
514 ((spec.raw->length & mask.raw->length) >=
515 (last.raw->length & mask.raw->length))))
516 tmp = spec.raw->length & mask.raw->length;
518 tmp = last.raw->length & mask.raw->length;
520 off = RTE_ALIGN_CEIL(off, sizeof(*dst.raw->pattern));
521 if (size >= off + tmp)
522 dst.raw->pattern = rte_memcpy
523 ((void *)((uintptr_t)dst.raw + off),
524 src.raw->pattern, tmp);
530 * allow PMD private flow item
532 off = (int)item->type >= 0 ?
533 rte_flow_desc_item[item->type].size : sizeof(void *);
534 rte_memcpy(buf, data, (size > off ? off : size));
541 * Copy action configuration.
544 * Output buffer. Can be NULL if @p size is zero.
546 * Size of @p buf in bytes.
548 * Action to copy configuration from.
551 * Number of bytes needed to store pattern item specification regardless
552 * of @p size. @p buf contents are truncated to @p size if not large
556 rte_flow_conv_action_conf(void *buf, const size_t size,
557 const struct rte_flow_action *action)
561 switch (action->type) {
563 const struct rte_flow_action_rss *rss;
564 const struct rte_flow_action_vxlan_encap *vxlan_encap;
565 const struct rte_flow_action_nvgre_encap *nvgre_encap;
568 struct rte_flow_action_rss *rss;
569 struct rte_flow_action_vxlan_encap *vxlan_encap;
570 struct rte_flow_action_nvgre_encap *nvgre_encap;
575 case RTE_FLOW_ACTION_TYPE_RSS:
576 src.rss = action->conf;
579 (&(struct rte_flow_action_rss){
580 .func = src.rss->func,
581 .level = src.rss->level,
582 .types = src.rss->types,
583 .key_len = src.rss->key_len,
584 .queue_num = src.rss->queue_num,
586 size > sizeof(*dst.rss) ? sizeof(*dst.rss) : size);
587 off = sizeof(*dst.rss);
588 if (src.rss->key_len && src.rss->key) {
589 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->key));
590 tmp = sizeof(*src.rss->key) * src.rss->key_len;
591 if (size >= off + tmp)
592 dst.rss->key = rte_memcpy
593 ((void *)((uintptr_t)dst.rss + off),
597 if (src.rss->queue_num) {
598 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->queue));
599 tmp = sizeof(*src.rss->queue) * src.rss->queue_num;
600 if (size >= off + tmp)
601 dst.rss->queue = rte_memcpy
602 ((void *)((uintptr_t)dst.rss + off),
603 src.rss->queue, tmp);
607 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
608 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
609 src.vxlan_encap = action->conf;
610 dst.vxlan_encap = buf;
611 RTE_BUILD_BUG_ON(sizeof(*src.vxlan_encap) !=
612 sizeof(*src.nvgre_encap) ||
613 offsetof(struct rte_flow_action_vxlan_encap,
615 offsetof(struct rte_flow_action_nvgre_encap,
617 off = sizeof(*dst.vxlan_encap);
618 if (src.vxlan_encap->definition) {
620 (off, sizeof(*dst.vxlan_encap->definition));
622 (RTE_FLOW_CONV_OP_PATTERN,
623 (void *)((uintptr_t)dst.vxlan_encap + off),
624 size > off ? size - off : 0,
625 src.vxlan_encap->definition, NULL);
628 if (size >= off + ret)
629 dst.vxlan_encap->definition =
630 (void *)((uintptr_t)dst.vxlan_encap +
637 * allow PMD private flow action
639 off = (int)action->type >= 0 ?
640 rte_flow_desc_action[action->type].size : sizeof(void *);
641 rte_memcpy(buf, action->conf, (size > off ? off : size));
648 * Copy a list of pattern items.
651 * Destination buffer. Can be NULL if @p size is zero.
653 * Size of @p dst in bytes.
655 * Source pattern items.
657 * Maximum number of pattern items to process from @p src or 0 to process
658 * the entire list. In both cases, processing stops after
659 * RTE_FLOW_ITEM_TYPE_END is encountered.
661 * Perform verbose error reporting if not NULL.
664 * A positive value representing the number of bytes needed to store
665 * pattern items regardless of @p size on success (@p buf contents are
666 * truncated to @p size if not large enough), a negative errno value
667 * otherwise and rte_errno is set.
670 rte_flow_conv_pattern(struct rte_flow_item *dst,
672 const struct rte_flow_item *src,
674 struct rte_flow_error *error)
676 uintptr_t data = (uintptr_t)dst;
681 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
683 * allow PMD private flow item
685 if (((int)src->type >= 0) &&
686 ((size_t)src->type >= RTE_DIM(rte_flow_desc_item) ||
687 !rte_flow_desc_item[src->type].name))
688 return rte_flow_error_set
689 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, src,
690 "cannot convert unknown item type");
691 if (size >= off + sizeof(*dst))
692 *dst = (struct rte_flow_item){
704 off = RTE_ALIGN_CEIL(off, sizeof(double));
705 ret = rte_flow_conv_item_spec
706 ((void *)(data + off),
707 size > off ? size - off : 0, src,
708 RTE_FLOW_CONV_ITEM_SPEC);
709 if (size && size >= off + ret)
710 dst->spec = (void *)(data + off);
715 off = RTE_ALIGN_CEIL(off, sizeof(double));
716 ret = rte_flow_conv_item_spec
717 ((void *)(data + off),
718 size > off ? size - off : 0, src,
719 RTE_FLOW_CONV_ITEM_LAST);
720 if (size && size >= off + ret)
721 dst->last = (void *)(data + off);
725 off = RTE_ALIGN_CEIL(off, sizeof(double));
726 ret = rte_flow_conv_item_spec
727 ((void *)(data + off),
728 size > off ? size - off : 0, src,
729 RTE_FLOW_CONV_ITEM_MASK);
730 if (size && size >= off + ret)
731 dst->mask = (void *)(data + off);
741 * Copy a list of actions.
744 * Destination buffer. Can be NULL if @p size is zero.
746 * Size of @p dst in bytes.
750 * Maximum number of actions to process from @p src or 0 to process the
751 * entire list. In both cases, processing stops after
752 * RTE_FLOW_ACTION_TYPE_END is encountered.
754 * Perform verbose error reporting if not NULL.
757 * A positive value representing the number of bytes needed to store
758 * actions regardless of @p size on success (@p buf contents are truncated
759 * to @p size if not large enough), a negative errno value otherwise and
763 rte_flow_conv_actions(struct rte_flow_action *dst,
765 const struct rte_flow_action *src,
767 struct rte_flow_error *error)
769 uintptr_t data = (uintptr_t)dst;
774 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
776 * allow PMD private flow action
778 if (((int)src->type >= 0) &&
779 ((size_t)src->type >= RTE_DIM(rte_flow_desc_action) ||
780 !rte_flow_desc_action[src->type].name))
781 return rte_flow_error_set
782 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
783 src, "cannot convert unknown action type");
784 if (size >= off + sizeof(*dst))
785 *dst = (struct rte_flow_action){
797 off = RTE_ALIGN_CEIL(off, sizeof(double));
798 ret = rte_flow_conv_action_conf
799 ((void *)(data + off),
800 size > off ? size - off : 0, src);
801 if (size && size >= off + ret)
802 dst->conf = (void *)(data + off);
812 * Copy flow rule components.
814 * This comprises the flow rule descriptor itself, attributes, pattern and
815 * actions list. NULL components in @p src are skipped.
818 * Destination buffer. Can be NULL if @p size is zero.
820 * Size of @p dst in bytes.
822 * Source flow rule descriptor.
824 * Perform verbose error reporting if not NULL.
827 * A positive value representing the number of bytes needed to store all
828 * components including the descriptor regardless of @p size on success
829 * (@p buf contents are truncated to @p size if not large enough), a
830 * negative errno value otherwise and rte_errno is set.
833 rte_flow_conv_rule(struct rte_flow_conv_rule *dst,
835 const struct rte_flow_conv_rule *src,
836 struct rte_flow_error *error)
842 (&(struct rte_flow_conv_rule){
847 size > sizeof(*dst) ? sizeof(*dst) : size);
850 off = RTE_ALIGN_CEIL(off, sizeof(double));
851 if (size && size >= off + sizeof(*dst->attr))
852 dst->attr = rte_memcpy
853 ((void *)((uintptr_t)dst + off),
854 src->attr_ro, sizeof(*dst->attr));
855 off += sizeof(*dst->attr);
857 if (src->pattern_ro) {
858 off = RTE_ALIGN_CEIL(off, sizeof(double));
859 ret = rte_flow_conv_pattern((void *)((uintptr_t)dst + off),
860 size > off ? size - off : 0,
861 src->pattern_ro, 0, error);
864 if (size && size >= off + (size_t)ret)
865 dst->pattern = (void *)((uintptr_t)dst + off);
868 if (src->actions_ro) {
869 off = RTE_ALIGN_CEIL(off, sizeof(double));
870 ret = rte_flow_conv_actions((void *)((uintptr_t)dst + off),
871 size > off ? size - off : 0,
872 src->actions_ro, 0, error);
875 if (size >= off + (size_t)ret)
876 dst->actions = (void *)((uintptr_t)dst + off);
883 * Retrieve the name of a pattern item/action type.
886 * Nonzero when @p src represents an action type instead of a pattern item
889 * Nonzero to write string address instead of contents into @p dst.
891 * Destination buffer. Can be NULL if @p size is zero.
893 * Size of @p dst in bytes.
895 * Depending on @p is_action, source pattern item or action type cast as a
898 * Perform verbose error reporting if not NULL.
901 * A positive value representing the number of bytes needed to store the
902 * name or its address regardless of @p size on success (@p buf contents
903 * are truncated to @p size if not large enough), a negative errno value
904 * otherwise and rte_errno is set.
907 rte_flow_conv_name(int is_action,
912 struct rte_flow_error *error)
915 const struct rte_flow_desc_data *data;
918 static const struct desc_info info_rep[2] = {
919 { rte_flow_desc_item, RTE_DIM(rte_flow_desc_item), },
920 { rte_flow_desc_action, RTE_DIM(rte_flow_desc_action), },
922 const struct desc_info *const info = &info_rep[!!is_action];
923 unsigned int type = (uintptr_t)src;
925 if (type >= info->num)
926 return rte_flow_error_set
927 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
928 "unknown object type to retrieve the name of");
930 return strlcpy(dst, info->data[type].name, size);
931 if (size >= sizeof(const char **))
932 *((const char **)dst) = info->data[type].name;
933 return sizeof(const char **);
936 /** Helper function to convert flow API objects. */
938 rte_flow_conv(enum rte_flow_conv_op op,
942 struct rte_flow_error *error)
945 const struct rte_flow_attr *attr;
947 case RTE_FLOW_CONV_OP_NONE:
949 case RTE_FLOW_CONV_OP_ATTR:
951 if (size > sizeof(*attr))
952 size = sizeof(*attr);
953 rte_memcpy(dst, attr, size);
954 return sizeof(*attr);
955 case RTE_FLOW_CONV_OP_ITEM:
956 return rte_flow_conv_pattern(dst, size, src, 1, error);
957 case RTE_FLOW_CONV_OP_ACTION:
958 return rte_flow_conv_actions(dst, size, src, 1, error);
959 case RTE_FLOW_CONV_OP_PATTERN:
960 return rte_flow_conv_pattern(dst, size, src, 0, error);
961 case RTE_FLOW_CONV_OP_ACTIONS:
962 return rte_flow_conv_actions(dst, size, src, 0, error);
963 case RTE_FLOW_CONV_OP_RULE:
964 return rte_flow_conv_rule(dst, size, src, error);
965 case RTE_FLOW_CONV_OP_ITEM_NAME:
966 return rte_flow_conv_name(0, 0, dst, size, src, error);
967 case RTE_FLOW_CONV_OP_ACTION_NAME:
968 return rte_flow_conv_name(1, 0, dst, size, src, error);
969 case RTE_FLOW_CONV_OP_ITEM_NAME_PTR:
970 return rte_flow_conv_name(0, 1, dst, size, src, error);
971 case RTE_FLOW_CONV_OP_ACTION_NAME_PTR:
972 return rte_flow_conv_name(1, 1, dst, size, src, error);
974 return rte_flow_error_set
975 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
976 "unknown object conversion operation");
979 /** Store a full rte_flow description. */
981 rte_flow_copy(struct rte_flow_desc *desc, size_t len,
982 const struct rte_flow_attr *attr,
983 const struct rte_flow_item *items,
984 const struct rte_flow_action *actions)
987 * Overlap struct rte_flow_conv with struct rte_flow_desc in order
988 * to convert the former to the latter without wasting space.
990 struct rte_flow_conv_rule *dst =
992 (void *)((uintptr_t)desc +
993 (offsetof(struct rte_flow_desc, actions) -
994 offsetof(struct rte_flow_conv_rule, actions))) :
997 len > sizeof(*desc) - sizeof(*dst) ?
998 len - (sizeof(*desc) - sizeof(*dst)) :
1000 struct rte_flow_conv_rule src = {
1002 .pattern_ro = items,
1003 .actions_ro = actions,
1007 RTE_BUILD_BUG_ON(sizeof(struct rte_flow_desc) <
1008 sizeof(struct rte_flow_conv_rule));
1010 (&dst->pattern != &desc->items ||
1011 &dst->actions != &desc->actions ||
1012 (uintptr_t)(dst + 1) != (uintptr_t)(desc + 1))) {
1016 ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, dst, dst_size, &src, NULL);
1019 ret += sizeof(*desc) - sizeof(*dst);
1021 (&(struct rte_flow_desc){
1024 .items = dst_size ? dst->pattern : NULL,
1025 .actions = dst_size ? dst->actions : NULL,
1027 len > sizeof(*desc) ? sizeof(*desc) : len);
1032 rte_flow_dev_dump(uint16_t port_id, struct rte_flow *flow,
1033 FILE *file, struct rte_flow_error *error)
1035 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1036 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1041 if (likely(!!ops->dev_dump)) {
1043 ret = ops->dev_dump(dev, flow, file, error);
1045 return flow_err(port_id, ret, error);
1047 return rte_flow_error_set(error, ENOSYS,
1048 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1049 NULL, rte_strerror(ENOSYS));
1053 rte_flow_get_aged_flows(uint16_t port_id, void **contexts,
1054 uint32_t nb_contexts, struct rte_flow_error *error)
1056 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1057 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1062 if (likely(!!ops->get_aged_flows)) {
1064 ret = ops->get_aged_flows(dev, contexts, nb_contexts, error);
1066 return flow_err(port_id, ret, error);
1068 return rte_flow_error_set(error, ENOTSUP,
1069 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1070 NULL, rte_strerror(ENOTSUP));
1073 struct rte_flow_action_handle *
1074 rte_flow_action_handle_create(uint16_t port_id,
1075 const struct rte_flow_indir_action_conf *conf,
1076 const struct rte_flow_action *action,
1077 struct rte_flow_error *error)
1079 struct rte_flow_action_handle *handle;
1080 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1084 if (unlikely(!ops->action_handle_create)) {
1085 rte_flow_error_set(error, ENOSYS,
1086 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1087 rte_strerror(ENOSYS));
1090 handle = ops->action_handle_create(&rte_eth_devices[port_id],
1091 conf, action, error);
1093 flow_err(port_id, -rte_errno, error);
1098 rte_flow_action_handle_destroy(uint16_t port_id,
1099 struct rte_flow_action_handle *handle,
1100 struct rte_flow_error *error)
1103 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1107 if (unlikely(!ops->action_handle_destroy))
1108 return rte_flow_error_set(error, ENOSYS,
1109 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1110 NULL, rte_strerror(ENOSYS));
1111 ret = ops->action_handle_destroy(&rte_eth_devices[port_id],
1113 return flow_err(port_id, ret, error);
1117 rte_flow_action_handle_update(uint16_t port_id,
1118 struct rte_flow_action_handle *handle,
1120 struct rte_flow_error *error)
1123 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1127 if (unlikely(!ops->action_handle_update))
1128 return rte_flow_error_set(error, ENOSYS,
1129 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1130 NULL, rte_strerror(ENOSYS));
1131 ret = ops->action_handle_update(&rte_eth_devices[port_id], handle,
1133 return flow_err(port_id, ret, error);
1137 rte_flow_action_handle_query(uint16_t port_id,
1138 const struct rte_flow_action_handle *handle,
1140 struct rte_flow_error *error)
1143 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1147 if (unlikely(!ops->action_handle_query))
1148 return rte_flow_error_set(error, ENOSYS,
1149 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1150 NULL, rte_strerror(ENOSYS));
1151 ret = ops->action_handle_query(&rte_eth_devices[port_id], handle,
1153 return flow_err(port_id, ret, error);
1157 rte_flow_tunnel_decap_set(uint16_t port_id,
1158 struct rte_flow_tunnel *tunnel,
1159 struct rte_flow_action **actions,
1160 uint32_t *num_of_actions,
1161 struct rte_flow_error *error)
1163 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1164 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1168 if (likely(!!ops->tunnel_decap_set)) {
1169 return flow_err(port_id,
1170 ops->tunnel_decap_set(dev, tunnel, actions,
1171 num_of_actions, error),
1174 return rte_flow_error_set(error, ENOTSUP,
1175 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1176 NULL, rte_strerror(ENOTSUP));
1180 rte_flow_tunnel_match(uint16_t port_id,
1181 struct rte_flow_tunnel *tunnel,
1182 struct rte_flow_item **items,
1183 uint32_t *num_of_items,
1184 struct rte_flow_error *error)
1186 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1187 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1191 if (likely(!!ops->tunnel_match)) {
1192 return flow_err(port_id,
1193 ops->tunnel_match(dev, tunnel, items,
1194 num_of_items, error),
1197 return rte_flow_error_set(error, ENOTSUP,
1198 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1199 NULL, rte_strerror(ENOTSUP));
1203 rte_flow_get_restore_info(uint16_t port_id,
1205 struct rte_flow_restore_info *restore_info,
1206 struct rte_flow_error *error)
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);
1213 if (likely(!!ops->get_restore_info)) {
1214 return flow_err(port_id,
1215 ops->get_restore_info(dev, m, restore_info,
1219 return rte_flow_error_set(error, ENOTSUP,
1220 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1221 NULL, rte_strerror(ENOTSUP));
1225 rte_flow_tunnel_action_decap_release(uint16_t port_id,
1226 struct rte_flow_action *actions,
1227 uint32_t num_of_actions,
1228 struct rte_flow_error *error)
1230 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1231 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1235 if (likely(!!ops->tunnel_action_decap_release)) {
1236 return flow_err(port_id,
1237 ops->tunnel_action_decap_release(dev, actions,
1242 return rte_flow_error_set(error, ENOTSUP,
1243 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1244 NULL, rte_strerror(ENOTSUP));
1248 rte_flow_tunnel_item_release(uint16_t port_id,
1249 struct rte_flow_item *items,
1250 uint32_t num_of_items,
1251 struct rte_flow_error *error)
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);
1258 if (likely(!!ops->tunnel_item_release)) {
1259 return flow_err(port_id,
1260 ops->tunnel_item_release(dev, items,
1261 num_of_items, error),
1264 return rte_flow_error_set(error, ENOTSUP,
1265 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1266 NULL, rte_strerror(ENOTSUP));