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(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)),
106 /** Generate flow_action[] entry. */
107 #define MK_FLOW_ACTION(t, s) \
108 [RTE_FLOW_ACTION_TYPE_ ## t] = { \
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)),
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.
191 MK_FLOW_ACTION(INDIRECT, 0),
192 MK_FLOW_ACTION(CONNTRACK, sizeof(struct rte_flow_action_conntrack)),
196 rte_flow_dynf_metadata_register(void)
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),
206 static const struct rte_mbuf_dynflag desc_flag = {
207 .name = RTE_MBUF_DYNFLAG_METADATA_NAME,
210 offset = rte_mbuf_dynfield_register(&desc_offs);
213 flag = rte_mbuf_dynflag_register(&desc_flag);
216 rte_flow_dynf_metadata_offs = offset;
217 rte_flow_dynf_metadata_mask = (1ULL << flag);
221 rte_flow_dynf_metadata_offs = -1;
222 rte_flow_dynf_metadata_mask = 0ULL;
227 fts_enter(struct rte_eth_dev *dev)
229 if (!(dev->data->dev_flags & RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE))
230 pthread_mutex_lock(&dev->data->flow_ops_mutex);
234 fts_exit(struct rte_eth_dev *dev)
236 if (!(dev->data->dev_flags & RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE))
237 pthread_mutex_unlock(&dev->data->flow_ops_mutex);
241 flow_err(uint16_t port_id, int ret, struct rte_flow_error *error)
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));
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)
256 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
257 const struct rte_flow_ops *ops;
260 if (unlikely(!rte_eth_dev_is_valid_port(port_id)))
262 else if (unlikely(dev->dev_ops->flow_ops_get == NULL))
263 /* flow API not supported with this driver dev_ops */
266 code = dev->dev_ops->flow_ops_get(dev, &ops);
267 if (code == 0 && ops == NULL)
268 /* flow API not supported with this device */
272 rte_flow_error_set(error, code, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
273 NULL, rte_strerror(code));
279 /* Check whether a flow rule can be created on a given port. */
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)
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];
293 if (likely(!!ops->validate)) {
295 ret = ops->validate(dev, attr, pattern, actions, error);
297 return flow_err(port_id, ret, error);
299 return rte_flow_error_set(error, ENOSYS,
300 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
301 NULL, rte_strerror(ENOSYS));
304 /* Create a flow rule on a given port. */
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)
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);
318 if (likely(!!ops->create)) {
320 flow = ops->create(dev, attr, pattern, actions, error);
323 flow_err(port_id, -rte_errno, error);
326 rte_flow_error_set(error, ENOSYS, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
327 NULL, rte_strerror(ENOSYS));
331 /* Destroy a flow rule on a given port. */
333 rte_flow_destroy(uint16_t port_id,
334 struct rte_flow *flow,
335 struct rte_flow_error *error)
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);
343 if (likely(!!ops->destroy)) {
345 ret = ops->destroy(dev, flow, error);
347 return flow_err(port_id, ret, error);
349 return rte_flow_error_set(error, ENOSYS,
350 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
351 NULL, rte_strerror(ENOSYS));
354 /* Destroy all flow rules associated with a port. */
356 rte_flow_flush(uint16_t port_id,
357 struct rte_flow_error *error)
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);
365 if (likely(!!ops->flush)) {
367 ret = ops->flush(dev, error);
369 return flow_err(port_id, ret, error);
371 return rte_flow_error_set(error, ENOSYS,
372 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
373 NULL, rte_strerror(ENOSYS));
376 /* Query an existing flow rule. */
378 rte_flow_query(uint16_t port_id,
379 struct rte_flow *flow,
380 const struct rte_flow_action *action,
382 struct rte_flow_error *error)
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);
390 if (likely(!!ops->query)) {
392 ret = ops->query(dev, flow, action, data, error);
394 return flow_err(port_id, ret, error);
396 return rte_flow_error_set(error, ENOSYS,
397 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
398 NULL, rte_strerror(ENOSYS));
401 /* Restrict ingress traffic to the defined flow rules. */
403 rte_flow_isolate(uint16_t port_id,
405 struct rte_flow_error *error)
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);
413 if (likely(!!ops->isolate)) {
415 ret = ops->isolate(dev, set, error);
417 return flow_err(port_id, ret, error);
419 return rte_flow_error_set(error, ENOSYS,
420 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
421 NULL, rte_strerror(ENOSYS));
424 /* Initialize flow error structure. */
426 rte_flow_error_set(struct rte_flow_error *error,
428 enum rte_flow_error_type type,
433 *error = (struct rte_flow_error){
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,
451 * Copy pattern item specification.
454 * Output buffer. Can be NULL if @p size is zero.
456 * Size of @p buf in bytes.
458 * Pattern item to copy specification from.
460 * Specification selector for either @p spec, @p last or @p mask.
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
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)
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 :
479 switch (item->type) {
481 const struct rte_flow_item_raw *raw;
484 const struct rte_flow_item_raw *raw;
487 const struct rte_flow_item_raw *raw;
490 const struct rte_flow_item_raw *raw;
493 struct rte_flow_item_raw *raw;
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;
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,
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;
520 tmp = last.raw->length & mask.raw->length;
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);
532 * allow PMD private flow item
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));
543 * Copy action configuration.
546 * Output buffer. Can be NULL if @p size is zero.
548 * Size of @p buf in bytes.
550 * Action to copy configuration from.
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
558 rte_flow_conv_action_conf(void *buf, const size_t size,
559 const struct rte_flow_action *action)
563 switch (action->type) {
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;
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;
577 case RTE_FLOW_ACTION_TYPE_RSS:
578 src.rss = action->conf;
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,
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),
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);
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,
617 offsetof(struct rte_flow_action_nvgre_encap,
619 off = sizeof(*dst.vxlan_encap);
620 if (src.vxlan_encap->definition) {
622 (off, sizeof(*dst.vxlan_encap->definition));
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);
630 if (size >= off + ret)
631 dst.vxlan_encap->definition =
632 (void *)((uintptr_t)dst.vxlan_encap +
639 * allow PMD private flow action
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));
650 * Copy a list of pattern items.
653 * Destination buffer. Can be NULL if @p size is zero.
655 * Size of @p dst in bytes.
657 * Source pattern items.
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.
663 * Perform verbose error reporting if not NULL.
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.
672 rte_flow_conv_pattern(struct rte_flow_item *dst,
674 const struct rte_flow_item *src,
676 struct rte_flow_error *error)
678 uintptr_t data = (uintptr_t)dst;
683 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
685 * allow PMD private flow item
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){
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);
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);
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);
743 * Copy a list of actions.
746 * Destination buffer. Can be NULL if @p size is zero.
748 * Size of @p dst in bytes.
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.
756 * Perform verbose error reporting if not NULL.
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
765 rte_flow_conv_actions(struct rte_flow_action *dst,
767 const struct rte_flow_action *src,
769 struct rte_flow_error *error)
771 uintptr_t data = (uintptr_t)dst;
776 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
778 * allow PMD private flow action
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){
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);
814 * Copy flow rule components.
816 * This comprises the flow rule descriptor itself, attributes, pattern and
817 * actions list. NULL components in @p src are skipped.
820 * Destination buffer. Can be NULL if @p size is zero.
822 * Size of @p dst in bytes.
824 * Source flow rule descriptor.
826 * Perform verbose error reporting if not NULL.
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.
835 rte_flow_conv_rule(struct rte_flow_conv_rule *dst,
837 const struct rte_flow_conv_rule *src,
838 struct rte_flow_error *error)
844 (&(struct rte_flow_conv_rule){
849 size > sizeof(*dst) ? sizeof(*dst) : size);
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);
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);
866 if (size && size >= off + (size_t)ret)
867 dst->pattern = (void *)((uintptr_t)dst + off);
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);
877 if (size >= off + (size_t)ret)
878 dst->actions = (void *)((uintptr_t)dst + off);
885 * Retrieve the name of a pattern item/action type.
888 * Nonzero when @p src represents an action type instead of a pattern item
891 * Nonzero to write string address instead of contents into @p dst.
893 * Destination buffer. Can be NULL if @p size is zero.
895 * Size of @p dst in bytes.
897 * Depending on @p is_action, source pattern item or action type cast as a
900 * Perform verbose error reporting if not NULL.
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.
909 rte_flow_conv_name(int is_action,
914 struct rte_flow_error *error)
917 const struct rte_flow_desc_data *data;
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), },
924 const struct desc_info *const info = &info_rep[!!is_action];
925 unsigned int type = (uintptr_t)src;
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");
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 **);
938 /** Helper function to convert flow API objects. */
940 rte_flow_conv(enum rte_flow_conv_op op,
944 struct rte_flow_error *error)
947 const struct rte_flow_attr *attr;
949 case RTE_FLOW_CONV_OP_NONE:
951 case RTE_FLOW_CONV_OP_ATTR:
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);
976 return rte_flow_error_set
977 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
978 "unknown object conversion operation");
981 /** Store a full rte_flow description. */
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)
989 * Overlap struct rte_flow_conv with struct rte_flow_desc in order
990 * to convert the former to the latter without wasting space.
992 struct rte_flow_conv_rule *dst =
994 (void *)((uintptr_t)desc +
995 (offsetof(struct rte_flow_desc, actions) -
996 offsetof(struct rte_flow_conv_rule, actions))) :
999 len > sizeof(*desc) - sizeof(*dst) ?
1000 len - (sizeof(*desc) - sizeof(*dst)) :
1002 struct rte_flow_conv_rule src = {
1004 .pattern_ro = items,
1005 .actions_ro = actions,
1009 RTE_BUILD_BUG_ON(sizeof(struct rte_flow_desc) <
1010 sizeof(struct rte_flow_conv_rule));
1012 (&dst->pattern != &desc->items ||
1013 &dst->actions != &desc->actions ||
1014 (uintptr_t)(dst + 1) != (uintptr_t)(desc + 1))) {
1018 ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, dst, dst_size, &src, NULL);
1021 ret += sizeof(*desc) - sizeof(*dst);
1023 (&(struct rte_flow_desc){
1026 .items = dst_size ? dst->pattern : NULL,
1027 .actions = dst_size ? dst->actions : NULL,
1029 len > sizeof(*desc) ? sizeof(*desc) : len);
1034 rte_flow_dev_dump(uint16_t port_id, struct rte_flow *flow,
1035 FILE *file, struct rte_flow_error *error)
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);
1043 if (likely(!!ops->dev_dump)) {
1045 ret = ops->dev_dump(dev, flow, file, error);
1047 return flow_err(port_id, ret, error);
1049 return rte_flow_error_set(error, ENOSYS,
1050 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1051 NULL, rte_strerror(ENOSYS));
1055 rte_flow_get_aged_flows(uint16_t port_id, void **contexts,
1056 uint32_t nb_contexts, struct rte_flow_error *error)
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);
1064 if (likely(!!ops->get_aged_flows)) {
1066 ret = ops->get_aged_flows(dev, contexts, nb_contexts, error);
1068 return flow_err(port_id, ret, error);
1070 return rte_flow_error_set(error, ENOTSUP,
1071 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1072 NULL, rte_strerror(ENOTSUP));
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)
1081 struct rte_flow_action_handle *handle;
1082 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
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));
1092 handle = ops->action_handle_create(&rte_eth_devices[port_id],
1093 conf, action, error);
1095 flow_err(port_id, -rte_errno, error);
1100 rte_flow_action_handle_destroy(uint16_t port_id,
1101 struct rte_flow_action_handle *handle,
1102 struct rte_flow_error *error)
1105 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
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],
1115 return flow_err(port_id, ret, error);
1119 rte_flow_action_handle_update(uint16_t port_id,
1120 struct rte_flow_action_handle *handle,
1122 struct rte_flow_error *error)
1125 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
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,
1135 return flow_err(port_id, ret, error);
1139 rte_flow_action_handle_query(uint16_t port_id,
1140 const struct rte_flow_action_handle *handle,
1142 struct rte_flow_error *error)
1145 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
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,
1155 return flow_err(port_id, ret, error);
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)
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);
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),
1176 return rte_flow_error_set(error, ENOTSUP,
1177 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1178 NULL, rte_strerror(ENOTSUP));
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)
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);
1193 if (likely(!!ops->tunnel_match)) {
1194 return flow_err(port_id,
1195 ops->tunnel_match(dev, tunnel, items,
1196 num_of_items, error),
1199 return rte_flow_error_set(error, ENOTSUP,
1200 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1201 NULL, rte_strerror(ENOTSUP));
1205 rte_flow_get_restore_info(uint16_t port_id,
1207 struct rte_flow_restore_info *restore_info,
1208 struct rte_flow_error *error)
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);
1215 if (likely(!!ops->get_restore_info)) {
1216 return flow_err(port_id,
1217 ops->get_restore_info(dev, m, restore_info,
1221 return rte_flow_error_set(error, ENOTSUP,
1222 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1223 NULL, rte_strerror(ENOTSUP));
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)
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);
1237 if (likely(!!ops->tunnel_action_decap_release)) {
1238 return flow_err(port_id,
1239 ops->tunnel_action_decap_release(dev, actions,
1244 return rte_flow_error_set(error, ENOTSUP,
1245 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1246 NULL, rte_strerror(ENOTSUP));
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)
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);
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),
1266 return rte_flow_error_set(error, ENOTSUP,
1267 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1268 NULL, rte_strerror(ENOTSUP));