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)),
103 /** Generate flow_action[] entry. */
104 #define MK_FLOW_ACTION(t, s) \
105 [RTE_FLOW_ACTION_TYPE_ ## t] = { \
110 /** Information about known flow actions. */
111 static const struct rte_flow_desc_data rte_flow_desc_action[] = {
112 MK_FLOW_ACTION(END, 0),
113 MK_FLOW_ACTION(VOID, 0),
114 MK_FLOW_ACTION(PASSTHRU, 0),
115 MK_FLOW_ACTION(JUMP, sizeof(struct rte_flow_action_jump)),
116 MK_FLOW_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
117 MK_FLOW_ACTION(FLAG, 0),
118 MK_FLOW_ACTION(QUEUE, sizeof(struct rte_flow_action_queue)),
119 MK_FLOW_ACTION(DROP, 0),
120 MK_FLOW_ACTION(COUNT, sizeof(struct rte_flow_action_count)),
121 MK_FLOW_ACTION(RSS, sizeof(struct rte_flow_action_rss)),
122 MK_FLOW_ACTION(PF, 0),
123 MK_FLOW_ACTION(VF, sizeof(struct rte_flow_action_vf)),
124 MK_FLOW_ACTION(PHY_PORT, sizeof(struct rte_flow_action_phy_port)),
125 MK_FLOW_ACTION(PORT_ID, sizeof(struct rte_flow_action_port_id)),
126 MK_FLOW_ACTION(METER, sizeof(struct rte_flow_action_meter)),
127 MK_FLOW_ACTION(SECURITY, sizeof(struct rte_flow_action_security)),
128 MK_FLOW_ACTION(OF_SET_MPLS_TTL,
129 sizeof(struct rte_flow_action_of_set_mpls_ttl)),
130 MK_FLOW_ACTION(OF_DEC_MPLS_TTL, 0),
131 MK_FLOW_ACTION(OF_SET_NW_TTL,
132 sizeof(struct rte_flow_action_of_set_nw_ttl)),
133 MK_FLOW_ACTION(OF_DEC_NW_TTL, 0),
134 MK_FLOW_ACTION(OF_COPY_TTL_OUT, 0),
135 MK_FLOW_ACTION(OF_COPY_TTL_IN, 0),
136 MK_FLOW_ACTION(OF_POP_VLAN, 0),
137 MK_FLOW_ACTION(OF_PUSH_VLAN,
138 sizeof(struct rte_flow_action_of_push_vlan)),
139 MK_FLOW_ACTION(OF_SET_VLAN_VID,
140 sizeof(struct rte_flow_action_of_set_vlan_vid)),
141 MK_FLOW_ACTION(OF_SET_VLAN_PCP,
142 sizeof(struct rte_flow_action_of_set_vlan_pcp)),
143 MK_FLOW_ACTION(OF_POP_MPLS,
144 sizeof(struct rte_flow_action_of_pop_mpls)),
145 MK_FLOW_ACTION(OF_PUSH_MPLS,
146 sizeof(struct rte_flow_action_of_push_mpls)),
147 MK_FLOW_ACTION(VXLAN_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
148 MK_FLOW_ACTION(VXLAN_DECAP, 0),
149 MK_FLOW_ACTION(NVGRE_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
150 MK_FLOW_ACTION(NVGRE_DECAP, 0),
151 MK_FLOW_ACTION(RAW_ENCAP, sizeof(struct rte_flow_action_raw_encap)),
152 MK_FLOW_ACTION(RAW_DECAP, sizeof(struct rte_flow_action_raw_decap)),
153 MK_FLOW_ACTION(SET_IPV4_SRC,
154 sizeof(struct rte_flow_action_set_ipv4)),
155 MK_FLOW_ACTION(SET_IPV4_DST,
156 sizeof(struct rte_flow_action_set_ipv4)),
157 MK_FLOW_ACTION(SET_IPV6_SRC,
158 sizeof(struct rte_flow_action_set_ipv6)),
159 MK_FLOW_ACTION(SET_IPV6_DST,
160 sizeof(struct rte_flow_action_set_ipv6)),
161 MK_FLOW_ACTION(SET_TP_SRC,
162 sizeof(struct rte_flow_action_set_tp)),
163 MK_FLOW_ACTION(SET_TP_DST,
164 sizeof(struct rte_flow_action_set_tp)),
165 MK_FLOW_ACTION(MAC_SWAP, 0),
166 MK_FLOW_ACTION(DEC_TTL, 0),
167 MK_FLOW_ACTION(SET_TTL, sizeof(struct rte_flow_action_set_ttl)),
168 MK_FLOW_ACTION(SET_MAC_SRC, sizeof(struct rte_flow_action_set_mac)),
169 MK_FLOW_ACTION(SET_MAC_DST, sizeof(struct rte_flow_action_set_mac)),
170 MK_FLOW_ACTION(INC_TCP_SEQ, sizeof(rte_be32_t)),
171 MK_FLOW_ACTION(DEC_TCP_SEQ, sizeof(rte_be32_t)),
172 MK_FLOW_ACTION(INC_TCP_ACK, sizeof(rte_be32_t)),
173 MK_FLOW_ACTION(DEC_TCP_ACK, sizeof(rte_be32_t)),
174 MK_FLOW_ACTION(SET_TAG, sizeof(struct rte_flow_action_set_tag)),
175 MK_FLOW_ACTION(SET_META, sizeof(struct rte_flow_action_set_meta)),
176 MK_FLOW_ACTION(SET_IPV4_DSCP, sizeof(struct rte_flow_action_set_dscp)),
177 MK_FLOW_ACTION(SET_IPV6_DSCP, sizeof(struct rte_flow_action_set_dscp)),
178 MK_FLOW_ACTION(AGE, sizeof(struct rte_flow_action_age)),
179 MK_FLOW_ACTION(SAMPLE, sizeof(struct rte_flow_action_sample)),
180 MK_FLOW_ACTION(MODIFY_FIELD,
181 sizeof(struct rte_flow_action_modify_field)),
183 * Shared action represented as handle of type
184 * (struct rte_flow_shared action *) stored in conf field (see
185 * struct rte_flow_action); no need for additional structure to * store
186 * shared action handle.
188 MK_FLOW_ACTION(SHARED, 0),
192 rte_flow_dynf_metadata_register(void)
197 static const struct rte_mbuf_dynfield desc_offs = {
198 .name = RTE_MBUF_DYNFIELD_METADATA_NAME,
199 .size = sizeof(uint32_t),
200 .align = __alignof__(uint32_t),
202 static const struct rte_mbuf_dynflag desc_flag = {
203 .name = RTE_MBUF_DYNFLAG_METADATA_NAME,
206 offset = rte_mbuf_dynfield_register(&desc_offs);
209 flag = rte_mbuf_dynflag_register(&desc_flag);
212 rte_flow_dynf_metadata_offs = offset;
213 rte_flow_dynf_metadata_mask = (1ULL << flag);
217 rte_flow_dynf_metadata_offs = -1;
218 rte_flow_dynf_metadata_mask = 0ULL;
223 fts_enter(struct rte_eth_dev *dev)
225 if (!(dev->data->dev_flags & RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE))
226 pthread_mutex_lock(&dev->data->flow_ops_mutex);
230 fts_exit(struct rte_eth_dev *dev)
232 if (!(dev->data->dev_flags & RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE))
233 pthread_mutex_unlock(&dev->data->flow_ops_mutex);
237 flow_err(uint16_t port_id, int ret, struct rte_flow_error *error)
241 if (rte_eth_dev_is_removed(port_id))
242 return rte_flow_error_set(error, EIO,
243 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
244 NULL, rte_strerror(EIO));
248 /* Get generic flow operations structure from a port. */
249 const struct rte_flow_ops *
250 rte_flow_ops_get(uint16_t port_id, struct rte_flow_error *error)
252 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
253 const struct rte_flow_ops *ops;
256 if (unlikely(!rte_eth_dev_is_valid_port(port_id)))
258 else if (unlikely(dev->dev_ops->flow_ops_get == NULL))
259 /* flow API not supported with this driver dev_ops */
262 code = dev->dev_ops->flow_ops_get(dev, &ops);
263 if (code == 0 && ops == NULL)
264 /* flow API not supported with this device */
268 rte_flow_error_set(error, code, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
269 NULL, rte_strerror(code));
275 /* Check whether a flow rule can be created on a given port. */
277 rte_flow_validate(uint16_t port_id,
278 const struct rte_flow_attr *attr,
279 const struct rte_flow_item pattern[],
280 const struct rte_flow_action actions[],
281 struct rte_flow_error *error)
283 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
284 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
289 if (likely(!!ops->validate)) {
291 ret = ops->validate(dev, attr, pattern, actions, error);
293 return flow_err(port_id, ret, error);
295 return rte_flow_error_set(error, ENOSYS,
296 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
297 NULL, rte_strerror(ENOSYS));
300 /* Create a flow rule on a given port. */
302 rte_flow_create(uint16_t port_id,
303 const struct rte_flow_attr *attr,
304 const struct rte_flow_item pattern[],
305 const struct rte_flow_action actions[],
306 struct rte_flow_error *error)
308 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
309 struct rte_flow *flow;
310 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
314 if (likely(!!ops->create)) {
316 flow = ops->create(dev, attr, pattern, actions, error);
319 flow_err(port_id, -rte_errno, error);
322 rte_flow_error_set(error, ENOSYS, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
323 NULL, rte_strerror(ENOSYS));
327 /* Destroy a flow rule on a given port. */
329 rte_flow_destroy(uint16_t port_id,
330 struct rte_flow *flow,
331 struct rte_flow_error *error)
333 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
334 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
339 if (likely(!!ops->destroy)) {
341 ret = ops->destroy(dev, flow, error);
343 return flow_err(port_id, ret, error);
345 return rte_flow_error_set(error, ENOSYS,
346 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
347 NULL, rte_strerror(ENOSYS));
350 /* Destroy all flow rules associated with a port. */
352 rte_flow_flush(uint16_t port_id,
353 struct rte_flow_error *error)
355 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
356 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
361 if (likely(!!ops->flush)) {
363 ret = ops->flush(dev, error);
365 return flow_err(port_id, ret, error);
367 return rte_flow_error_set(error, ENOSYS,
368 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
369 NULL, rte_strerror(ENOSYS));
372 /* Query an existing flow rule. */
374 rte_flow_query(uint16_t port_id,
375 struct rte_flow *flow,
376 const struct rte_flow_action *action,
378 struct rte_flow_error *error)
380 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
381 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
386 if (likely(!!ops->query)) {
388 ret = ops->query(dev, flow, action, data, error);
390 return flow_err(port_id, ret, error);
392 return rte_flow_error_set(error, ENOSYS,
393 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
394 NULL, rte_strerror(ENOSYS));
397 /* Restrict ingress traffic to the defined flow rules. */
399 rte_flow_isolate(uint16_t port_id,
401 struct rte_flow_error *error)
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);
409 if (likely(!!ops->isolate)) {
411 ret = ops->isolate(dev, set, error);
413 return flow_err(port_id, ret, error);
415 return rte_flow_error_set(error, ENOSYS,
416 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
417 NULL, rte_strerror(ENOSYS));
420 /* Initialize flow error structure. */
422 rte_flow_error_set(struct rte_flow_error *error,
424 enum rte_flow_error_type type,
429 *error = (struct rte_flow_error){
439 /** Pattern item specification types. */
440 enum rte_flow_conv_item_spec_type {
441 RTE_FLOW_CONV_ITEM_SPEC,
442 RTE_FLOW_CONV_ITEM_LAST,
443 RTE_FLOW_CONV_ITEM_MASK,
447 * Copy pattern item specification.
450 * Output buffer. Can be NULL if @p size is zero.
452 * Size of @p buf in bytes.
454 * Pattern item to copy specification from.
456 * Specification selector for either @p spec, @p last or @p mask.
459 * Number of bytes needed to store pattern item specification regardless
460 * of @p size. @p buf contents are truncated to @p size if not large
464 rte_flow_conv_item_spec(void *buf, const size_t size,
465 const struct rte_flow_item *item,
466 enum rte_flow_conv_item_spec_type type)
470 type == RTE_FLOW_CONV_ITEM_SPEC ? item->spec :
471 type == RTE_FLOW_CONV_ITEM_LAST ? item->last :
472 type == RTE_FLOW_CONV_ITEM_MASK ? item->mask :
475 switch (item->type) {
477 const struct rte_flow_item_raw *raw;
480 const struct rte_flow_item_raw *raw;
483 const struct rte_flow_item_raw *raw;
486 const struct rte_flow_item_raw *raw;
489 struct rte_flow_item_raw *raw;
493 case RTE_FLOW_ITEM_TYPE_RAW:
494 spec.raw = item->spec;
495 last.raw = item->last ? item->last : item->spec;
496 mask.raw = item->mask ? item->mask : &rte_flow_item_raw_mask;
500 (&(struct rte_flow_item_raw){
501 .relative = src.raw->relative,
502 .search = src.raw->search,
503 .reserved = src.raw->reserved,
504 .offset = src.raw->offset,
505 .limit = src.raw->limit,
506 .length = src.raw->length,
508 size > sizeof(*dst.raw) ? sizeof(*dst.raw) : size);
509 off = sizeof(*dst.raw);
510 if (type == RTE_FLOW_CONV_ITEM_SPEC ||
511 (type == RTE_FLOW_CONV_ITEM_MASK &&
512 ((spec.raw->length & mask.raw->length) >=
513 (last.raw->length & mask.raw->length))))
514 tmp = spec.raw->length & mask.raw->length;
516 tmp = last.raw->length & mask.raw->length;
518 off = RTE_ALIGN_CEIL(off, sizeof(*dst.raw->pattern));
519 if (size >= off + tmp)
520 dst.raw->pattern = rte_memcpy
521 ((void *)((uintptr_t)dst.raw + off),
522 src.raw->pattern, tmp);
528 * allow PMD private flow item
530 off = (int)item->type >= 0 ?
531 rte_flow_desc_item[item->type].size : sizeof(void *);
532 rte_memcpy(buf, data, (size > off ? off : size));
539 * Copy action configuration.
542 * Output buffer. Can be NULL if @p size is zero.
544 * Size of @p buf in bytes.
546 * Action to copy configuration from.
549 * Number of bytes needed to store pattern item specification regardless
550 * of @p size. @p buf contents are truncated to @p size if not large
554 rte_flow_conv_action_conf(void *buf, const size_t size,
555 const struct rte_flow_action *action)
559 switch (action->type) {
561 const struct rte_flow_action_rss *rss;
562 const struct rte_flow_action_vxlan_encap *vxlan_encap;
563 const struct rte_flow_action_nvgre_encap *nvgre_encap;
566 struct rte_flow_action_rss *rss;
567 struct rte_flow_action_vxlan_encap *vxlan_encap;
568 struct rte_flow_action_nvgre_encap *nvgre_encap;
573 case RTE_FLOW_ACTION_TYPE_RSS:
574 src.rss = action->conf;
577 (&(struct rte_flow_action_rss){
578 .func = src.rss->func,
579 .level = src.rss->level,
580 .types = src.rss->types,
581 .key_len = src.rss->key_len,
582 .queue_num = src.rss->queue_num,
584 size > sizeof(*dst.rss) ? sizeof(*dst.rss) : size);
585 off = sizeof(*dst.rss);
586 if (src.rss->key_len && src.rss->key) {
587 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->key));
588 tmp = sizeof(*src.rss->key) * src.rss->key_len;
589 if (size >= off + tmp)
590 dst.rss->key = rte_memcpy
591 ((void *)((uintptr_t)dst.rss + off),
595 if (src.rss->queue_num) {
596 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->queue));
597 tmp = sizeof(*src.rss->queue) * src.rss->queue_num;
598 if (size >= off + tmp)
599 dst.rss->queue = rte_memcpy
600 ((void *)((uintptr_t)dst.rss + off),
601 src.rss->queue, tmp);
605 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
606 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
607 src.vxlan_encap = action->conf;
608 dst.vxlan_encap = buf;
609 RTE_BUILD_BUG_ON(sizeof(*src.vxlan_encap) !=
610 sizeof(*src.nvgre_encap) ||
611 offsetof(struct rte_flow_action_vxlan_encap,
613 offsetof(struct rte_flow_action_nvgre_encap,
615 off = sizeof(*dst.vxlan_encap);
616 if (src.vxlan_encap->definition) {
618 (off, sizeof(*dst.vxlan_encap->definition));
620 (RTE_FLOW_CONV_OP_PATTERN,
621 (void *)((uintptr_t)dst.vxlan_encap + off),
622 size > off ? size - off : 0,
623 src.vxlan_encap->definition, NULL);
626 if (size >= off + ret)
627 dst.vxlan_encap->definition =
628 (void *)((uintptr_t)dst.vxlan_encap +
635 * allow PMD private flow action
637 off = (int)action->type >= 0 ?
638 rte_flow_desc_action[action->type].size : sizeof(void *);
639 rte_memcpy(buf, action->conf, (size > off ? off : size));
646 * Copy a list of pattern items.
649 * Destination buffer. Can be NULL if @p size is zero.
651 * Size of @p dst in bytes.
653 * Source pattern items.
655 * Maximum number of pattern items to process from @p src or 0 to process
656 * the entire list. In both cases, processing stops after
657 * RTE_FLOW_ITEM_TYPE_END is encountered.
659 * Perform verbose error reporting if not NULL.
662 * A positive value representing the number of bytes needed to store
663 * pattern items regardless of @p size on success (@p buf contents are
664 * truncated to @p size if not large enough), a negative errno value
665 * otherwise and rte_errno is set.
668 rte_flow_conv_pattern(struct rte_flow_item *dst,
670 const struct rte_flow_item *src,
672 struct rte_flow_error *error)
674 uintptr_t data = (uintptr_t)dst;
679 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
681 * allow PMD private flow item
683 if (((int)src->type >= 0) &&
684 ((size_t)src->type >= RTE_DIM(rte_flow_desc_item) ||
685 !rte_flow_desc_item[src->type].name))
686 return rte_flow_error_set
687 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, src,
688 "cannot convert unknown item type");
689 if (size >= off + sizeof(*dst))
690 *dst = (struct rte_flow_item){
702 off = RTE_ALIGN_CEIL(off, sizeof(double));
703 ret = rte_flow_conv_item_spec
704 ((void *)(data + off),
705 size > off ? size - off : 0, src,
706 RTE_FLOW_CONV_ITEM_SPEC);
707 if (size && size >= off + ret)
708 dst->spec = (void *)(data + off);
713 off = RTE_ALIGN_CEIL(off, sizeof(double));
714 ret = rte_flow_conv_item_spec
715 ((void *)(data + off),
716 size > off ? size - off : 0, src,
717 RTE_FLOW_CONV_ITEM_LAST);
718 if (size && size >= off + ret)
719 dst->last = (void *)(data + off);
723 off = RTE_ALIGN_CEIL(off, sizeof(double));
724 ret = rte_flow_conv_item_spec
725 ((void *)(data + off),
726 size > off ? size - off : 0, src,
727 RTE_FLOW_CONV_ITEM_MASK);
728 if (size && size >= off + ret)
729 dst->mask = (void *)(data + off);
739 * Copy a list of actions.
742 * Destination buffer. Can be NULL if @p size is zero.
744 * Size of @p dst in bytes.
748 * Maximum number of actions to process from @p src or 0 to process the
749 * entire list. In both cases, processing stops after
750 * RTE_FLOW_ACTION_TYPE_END is encountered.
752 * Perform verbose error reporting if not NULL.
755 * A positive value representing the number of bytes needed to store
756 * actions regardless of @p size on success (@p buf contents are truncated
757 * to @p size if not large enough), a negative errno value otherwise and
761 rte_flow_conv_actions(struct rte_flow_action *dst,
763 const struct rte_flow_action *src,
765 struct rte_flow_error *error)
767 uintptr_t data = (uintptr_t)dst;
772 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
774 * allow PMD private flow action
776 if (((int)src->type >= 0) &&
777 ((size_t)src->type >= RTE_DIM(rte_flow_desc_action) ||
778 !rte_flow_desc_action[src->type].name))
779 return rte_flow_error_set
780 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
781 src, "cannot convert unknown action type");
782 if (size >= off + sizeof(*dst))
783 *dst = (struct rte_flow_action){
795 off = RTE_ALIGN_CEIL(off, sizeof(double));
796 ret = rte_flow_conv_action_conf
797 ((void *)(data + off),
798 size > off ? size - off : 0, src);
799 if (size && size >= off + ret)
800 dst->conf = (void *)(data + off);
810 * Copy flow rule components.
812 * This comprises the flow rule descriptor itself, attributes, pattern and
813 * actions list. NULL components in @p src are skipped.
816 * Destination buffer. Can be NULL if @p size is zero.
818 * Size of @p dst in bytes.
820 * Source flow rule descriptor.
822 * Perform verbose error reporting if not NULL.
825 * A positive value representing the number of bytes needed to store all
826 * components including the descriptor regardless of @p size on success
827 * (@p buf contents are truncated to @p size if not large enough), a
828 * negative errno value otherwise and rte_errno is set.
831 rte_flow_conv_rule(struct rte_flow_conv_rule *dst,
833 const struct rte_flow_conv_rule *src,
834 struct rte_flow_error *error)
840 (&(struct rte_flow_conv_rule){
845 size > sizeof(*dst) ? sizeof(*dst) : size);
848 off = RTE_ALIGN_CEIL(off, sizeof(double));
849 if (size && size >= off + sizeof(*dst->attr))
850 dst->attr = rte_memcpy
851 ((void *)((uintptr_t)dst + off),
852 src->attr_ro, sizeof(*dst->attr));
853 off += sizeof(*dst->attr);
855 if (src->pattern_ro) {
856 off = RTE_ALIGN_CEIL(off, sizeof(double));
857 ret = rte_flow_conv_pattern((void *)((uintptr_t)dst + off),
858 size > off ? size - off : 0,
859 src->pattern_ro, 0, error);
862 if (size && size >= off + (size_t)ret)
863 dst->pattern = (void *)((uintptr_t)dst + off);
866 if (src->actions_ro) {
867 off = RTE_ALIGN_CEIL(off, sizeof(double));
868 ret = rte_flow_conv_actions((void *)((uintptr_t)dst + off),
869 size > off ? size - off : 0,
870 src->actions_ro, 0, error);
873 if (size >= off + (size_t)ret)
874 dst->actions = (void *)((uintptr_t)dst + off);
881 * Retrieve the name of a pattern item/action type.
884 * Nonzero when @p src represents an action type instead of a pattern item
887 * Nonzero to write string address instead of contents into @p dst.
889 * Destination buffer. Can be NULL if @p size is zero.
891 * Size of @p dst in bytes.
893 * Depending on @p is_action, source pattern item or action type cast as a
896 * Perform verbose error reporting if not NULL.
899 * A positive value representing the number of bytes needed to store the
900 * name or its address regardless of @p size on success (@p buf contents
901 * are truncated to @p size if not large enough), a negative errno value
902 * otherwise and rte_errno is set.
905 rte_flow_conv_name(int is_action,
910 struct rte_flow_error *error)
913 const struct rte_flow_desc_data *data;
916 static const struct desc_info info_rep[2] = {
917 { rte_flow_desc_item, RTE_DIM(rte_flow_desc_item), },
918 { rte_flow_desc_action, RTE_DIM(rte_flow_desc_action), },
920 const struct desc_info *const info = &info_rep[!!is_action];
921 unsigned int type = (uintptr_t)src;
923 if (type >= info->num)
924 return rte_flow_error_set
925 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
926 "unknown object type to retrieve the name of");
928 return strlcpy(dst, info->data[type].name, size);
929 if (size >= sizeof(const char **))
930 *((const char **)dst) = info->data[type].name;
931 return sizeof(const char **);
934 /** Helper function to convert flow API objects. */
936 rte_flow_conv(enum rte_flow_conv_op op,
940 struct rte_flow_error *error)
943 const struct rte_flow_attr *attr;
945 case RTE_FLOW_CONV_OP_NONE:
947 case RTE_FLOW_CONV_OP_ATTR:
949 if (size > sizeof(*attr))
950 size = sizeof(*attr);
951 rte_memcpy(dst, attr, size);
952 return sizeof(*attr);
953 case RTE_FLOW_CONV_OP_ITEM:
954 return rte_flow_conv_pattern(dst, size, src, 1, error);
955 case RTE_FLOW_CONV_OP_ACTION:
956 return rte_flow_conv_actions(dst, size, src, 1, error);
957 case RTE_FLOW_CONV_OP_PATTERN:
958 return rte_flow_conv_pattern(dst, size, src, 0, error);
959 case RTE_FLOW_CONV_OP_ACTIONS:
960 return rte_flow_conv_actions(dst, size, src, 0, error);
961 case RTE_FLOW_CONV_OP_RULE:
962 return rte_flow_conv_rule(dst, size, src, error);
963 case RTE_FLOW_CONV_OP_ITEM_NAME:
964 return rte_flow_conv_name(0, 0, dst, size, src, error);
965 case RTE_FLOW_CONV_OP_ACTION_NAME:
966 return rte_flow_conv_name(1, 0, dst, size, src, error);
967 case RTE_FLOW_CONV_OP_ITEM_NAME_PTR:
968 return rte_flow_conv_name(0, 1, dst, size, src, error);
969 case RTE_FLOW_CONV_OP_ACTION_NAME_PTR:
970 return rte_flow_conv_name(1, 1, dst, size, src, error);
972 return rte_flow_error_set
973 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
974 "unknown object conversion operation");
977 /** Store a full rte_flow description. */
979 rte_flow_copy(struct rte_flow_desc *desc, size_t len,
980 const struct rte_flow_attr *attr,
981 const struct rte_flow_item *items,
982 const struct rte_flow_action *actions)
985 * Overlap struct rte_flow_conv with struct rte_flow_desc in order
986 * to convert the former to the latter without wasting space.
988 struct rte_flow_conv_rule *dst =
990 (void *)((uintptr_t)desc +
991 (offsetof(struct rte_flow_desc, actions) -
992 offsetof(struct rte_flow_conv_rule, actions))) :
995 len > sizeof(*desc) - sizeof(*dst) ?
996 len - (sizeof(*desc) - sizeof(*dst)) :
998 struct rte_flow_conv_rule src = {
1000 .pattern_ro = items,
1001 .actions_ro = actions,
1005 RTE_BUILD_BUG_ON(sizeof(struct rte_flow_desc) <
1006 sizeof(struct rte_flow_conv_rule));
1008 (&dst->pattern != &desc->items ||
1009 &dst->actions != &desc->actions ||
1010 (uintptr_t)(dst + 1) != (uintptr_t)(desc + 1))) {
1014 ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, dst, dst_size, &src, NULL);
1017 ret += sizeof(*desc) - sizeof(*dst);
1019 (&(struct rte_flow_desc){
1022 .items = dst_size ? dst->pattern : NULL,
1023 .actions = dst_size ? dst->actions : NULL,
1025 len > sizeof(*desc) ? sizeof(*desc) : len);
1030 rte_flow_dev_dump(uint16_t port_id, FILE *file, struct rte_flow_error *error)
1032 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1033 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1038 if (likely(!!ops->dev_dump)) {
1040 ret = ops->dev_dump(dev, file, error);
1042 return flow_err(port_id, ret, error);
1044 return rte_flow_error_set(error, ENOSYS,
1045 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1046 NULL, rte_strerror(ENOSYS));
1050 rte_flow_get_aged_flows(uint16_t port_id, void **contexts,
1051 uint32_t nb_contexts, struct rte_flow_error *error)
1053 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1054 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1059 if (likely(!!ops->get_aged_flows)) {
1061 ret = ops->get_aged_flows(dev, contexts, nb_contexts, error);
1063 return flow_err(port_id, ret, error);
1065 return rte_flow_error_set(error, ENOTSUP,
1066 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1067 NULL, rte_strerror(ENOTSUP));
1070 struct rte_flow_shared_action *
1071 rte_flow_shared_action_create(uint16_t port_id,
1072 const struct rte_flow_shared_action_conf *conf,
1073 const struct rte_flow_action *action,
1074 struct rte_flow_error *error)
1076 struct rte_flow_shared_action *shared_action;
1077 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1081 if (unlikely(!ops->shared_action_create)) {
1082 rte_flow_error_set(error, ENOSYS,
1083 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1084 rte_strerror(ENOSYS));
1087 shared_action = ops->shared_action_create(&rte_eth_devices[port_id],
1088 conf, action, error);
1089 if (shared_action == NULL)
1090 flow_err(port_id, -rte_errno, error);
1091 return shared_action;
1095 rte_flow_shared_action_destroy(uint16_t port_id,
1096 struct rte_flow_shared_action *action,
1097 struct rte_flow_error *error)
1100 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1104 if (unlikely(!ops->shared_action_destroy))
1105 return rte_flow_error_set(error, ENOSYS,
1106 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1107 NULL, rte_strerror(ENOSYS));
1108 ret = ops->shared_action_destroy(&rte_eth_devices[port_id], action,
1110 return flow_err(port_id, ret, error);
1114 rte_flow_shared_action_update(uint16_t port_id,
1115 struct rte_flow_shared_action *action,
1116 const struct rte_flow_action *update,
1117 struct rte_flow_error *error)
1120 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1124 if (unlikely(!ops->shared_action_update))
1125 return rte_flow_error_set(error, ENOSYS,
1126 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1127 NULL, rte_strerror(ENOSYS));
1128 ret = ops->shared_action_update(&rte_eth_devices[port_id], action,
1130 return flow_err(port_id, ret, error);
1134 rte_flow_shared_action_query(uint16_t port_id,
1135 const struct rte_flow_shared_action *action,
1137 struct rte_flow_error *error)
1140 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1144 if (unlikely(!ops->shared_action_query))
1145 return rte_flow_error_set(error, ENOSYS,
1146 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1147 NULL, rte_strerror(ENOSYS));
1148 ret = ops->shared_action_query(&rte_eth_devices[port_id], action,
1150 return flow_err(port_id, ret, error);
1154 rte_flow_tunnel_decap_set(uint16_t port_id,
1155 struct rte_flow_tunnel *tunnel,
1156 struct rte_flow_action **actions,
1157 uint32_t *num_of_actions,
1158 struct rte_flow_error *error)
1160 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1161 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1165 if (likely(!!ops->tunnel_decap_set)) {
1166 return flow_err(port_id,
1167 ops->tunnel_decap_set(dev, tunnel, actions,
1168 num_of_actions, error),
1171 return rte_flow_error_set(error, ENOTSUP,
1172 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1173 NULL, rte_strerror(ENOTSUP));
1177 rte_flow_tunnel_match(uint16_t port_id,
1178 struct rte_flow_tunnel *tunnel,
1179 struct rte_flow_item **items,
1180 uint32_t *num_of_items,
1181 struct rte_flow_error *error)
1183 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1184 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1188 if (likely(!!ops->tunnel_match)) {
1189 return flow_err(port_id,
1190 ops->tunnel_match(dev, tunnel, items,
1191 num_of_items, error),
1194 return rte_flow_error_set(error, ENOTSUP,
1195 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1196 NULL, rte_strerror(ENOTSUP));
1200 rte_flow_get_restore_info(uint16_t port_id,
1202 struct rte_flow_restore_info *restore_info,
1203 struct rte_flow_error *error)
1205 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1206 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1210 if (likely(!!ops->get_restore_info)) {
1211 return flow_err(port_id,
1212 ops->get_restore_info(dev, m, restore_info,
1216 return rte_flow_error_set(error, ENOTSUP,
1217 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1218 NULL, rte_strerror(ENOTSUP));
1222 rte_flow_tunnel_action_decap_release(uint16_t port_id,
1223 struct rte_flow_action *actions,
1224 uint32_t num_of_actions,
1225 struct rte_flow_error *error)
1227 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1228 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1232 if (likely(!!ops->tunnel_action_decap_release)) {
1233 return flow_err(port_id,
1234 ops->tunnel_action_decap_release(dev, actions,
1239 return rte_flow_error_set(error, ENOTSUP,
1240 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1241 NULL, rte_strerror(ENOTSUP));
1245 rte_flow_tunnel_item_release(uint16_t port_id,
1246 struct rte_flow_item *items,
1247 uint32_t num_of_items,
1248 struct rte_flow_error *error)
1250 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1251 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1255 if (likely(!!ops->tunnel_item_release)) {
1256 return flow_err(port_id,
1257 ops->tunnel_item_release(dev, items,
1258 num_of_items, error),
1261 return rte_flow_error_set(error, ENOTSUP,
1262 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1263 NULL, rte_strerror(ENOTSUP));