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->filter_ctrl ||
259 dev->dev_ops->filter_ctrl(dev,
260 RTE_ETH_FILTER_GENERIC,
267 rte_flow_error_set(error, code, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
268 NULL, rte_strerror(code));
272 /* Check whether a flow rule can be created on a given port. */
274 rte_flow_validate(uint16_t port_id,
275 const struct rte_flow_attr *attr,
276 const struct rte_flow_item pattern[],
277 const struct rte_flow_action actions[],
278 struct rte_flow_error *error)
280 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
281 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
286 if (likely(!!ops->validate)) {
288 ret = ops->validate(dev, attr, pattern, actions, error);
290 return flow_err(port_id, ret, error);
292 return rte_flow_error_set(error, ENOSYS,
293 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
294 NULL, rte_strerror(ENOSYS));
297 /* Create a flow rule on a given port. */
299 rte_flow_create(uint16_t port_id,
300 const struct rte_flow_attr *attr,
301 const struct rte_flow_item pattern[],
302 const struct rte_flow_action actions[],
303 struct rte_flow_error *error)
305 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
306 struct rte_flow *flow;
307 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
311 if (likely(!!ops->create)) {
313 flow = ops->create(dev, attr, pattern, actions, error);
316 flow_err(port_id, -rte_errno, error);
319 rte_flow_error_set(error, ENOSYS, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
320 NULL, rte_strerror(ENOSYS));
324 /* Destroy a flow rule on a given port. */
326 rte_flow_destroy(uint16_t port_id,
327 struct rte_flow *flow,
328 struct rte_flow_error *error)
330 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
331 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
336 if (likely(!!ops->destroy)) {
338 ret = ops->destroy(dev, flow, error);
340 return flow_err(port_id, ret, error);
342 return rte_flow_error_set(error, ENOSYS,
343 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
344 NULL, rte_strerror(ENOSYS));
347 /* Destroy all flow rules associated with a port. */
349 rte_flow_flush(uint16_t port_id,
350 struct rte_flow_error *error)
352 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
353 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
358 if (likely(!!ops->flush)) {
360 ret = ops->flush(dev, error);
362 return flow_err(port_id, ret, error);
364 return rte_flow_error_set(error, ENOSYS,
365 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
366 NULL, rte_strerror(ENOSYS));
369 /* Query an existing flow rule. */
371 rte_flow_query(uint16_t port_id,
372 struct rte_flow *flow,
373 const struct rte_flow_action *action,
375 struct rte_flow_error *error)
377 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
378 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
383 if (likely(!!ops->query)) {
385 ret = ops->query(dev, flow, action, data, error);
387 return flow_err(port_id, ret, error);
389 return rte_flow_error_set(error, ENOSYS,
390 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
391 NULL, rte_strerror(ENOSYS));
394 /* Restrict ingress traffic to the defined flow rules. */
396 rte_flow_isolate(uint16_t port_id,
398 struct rte_flow_error *error)
400 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
401 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
406 if (likely(!!ops->isolate)) {
408 ret = ops->isolate(dev, set, error);
410 return flow_err(port_id, ret, error);
412 return rte_flow_error_set(error, ENOSYS,
413 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
414 NULL, rte_strerror(ENOSYS));
417 /* Initialize flow error structure. */
419 rte_flow_error_set(struct rte_flow_error *error,
421 enum rte_flow_error_type type,
426 *error = (struct rte_flow_error){
436 /** Pattern item specification types. */
437 enum rte_flow_conv_item_spec_type {
438 RTE_FLOW_CONV_ITEM_SPEC,
439 RTE_FLOW_CONV_ITEM_LAST,
440 RTE_FLOW_CONV_ITEM_MASK,
444 * Copy pattern item specification.
447 * Output buffer. Can be NULL if @p size is zero.
449 * Size of @p buf in bytes.
451 * Pattern item to copy specification from.
453 * Specification selector for either @p spec, @p last or @p mask.
456 * Number of bytes needed to store pattern item specification regardless
457 * of @p size. @p buf contents are truncated to @p size if not large
461 rte_flow_conv_item_spec(void *buf, const size_t size,
462 const struct rte_flow_item *item,
463 enum rte_flow_conv_item_spec_type type)
467 type == RTE_FLOW_CONV_ITEM_SPEC ? item->spec :
468 type == RTE_FLOW_CONV_ITEM_LAST ? item->last :
469 type == RTE_FLOW_CONV_ITEM_MASK ? item->mask :
472 switch (item->type) {
474 const struct rte_flow_item_raw *raw;
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 struct rte_flow_item_raw *raw;
490 case RTE_FLOW_ITEM_TYPE_RAW:
491 spec.raw = item->spec;
492 last.raw = item->last ? item->last : item->spec;
493 mask.raw = item->mask ? item->mask : &rte_flow_item_raw_mask;
497 (&(struct rte_flow_item_raw){
498 .relative = src.raw->relative,
499 .search = src.raw->search,
500 .reserved = src.raw->reserved,
501 .offset = src.raw->offset,
502 .limit = src.raw->limit,
503 .length = src.raw->length,
505 size > sizeof(*dst.raw) ? sizeof(*dst.raw) : size);
506 off = sizeof(*dst.raw);
507 if (type == RTE_FLOW_CONV_ITEM_SPEC ||
508 (type == RTE_FLOW_CONV_ITEM_MASK &&
509 ((spec.raw->length & mask.raw->length) >=
510 (last.raw->length & mask.raw->length))))
511 tmp = spec.raw->length & mask.raw->length;
513 tmp = last.raw->length & mask.raw->length;
515 off = RTE_ALIGN_CEIL(off, sizeof(*dst.raw->pattern));
516 if (size >= off + tmp)
517 dst.raw->pattern = rte_memcpy
518 ((void *)((uintptr_t)dst.raw + off),
519 src.raw->pattern, tmp);
525 * allow PMD private flow item
527 off = (int)item->type >= 0 ?
528 rte_flow_desc_item[item->type].size : sizeof(void *);
529 rte_memcpy(buf, data, (size > off ? off : size));
536 * Copy action configuration.
539 * Output buffer. Can be NULL if @p size is zero.
541 * Size of @p buf in bytes.
543 * Action to copy configuration from.
546 * Number of bytes needed to store pattern item specification regardless
547 * of @p size. @p buf contents are truncated to @p size if not large
551 rte_flow_conv_action_conf(void *buf, const size_t size,
552 const struct rte_flow_action *action)
556 switch (action->type) {
558 const struct rte_flow_action_rss *rss;
559 const struct rte_flow_action_vxlan_encap *vxlan_encap;
560 const struct rte_flow_action_nvgre_encap *nvgre_encap;
563 struct rte_flow_action_rss *rss;
564 struct rte_flow_action_vxlan_encap *vxlan_encap;
565 struct rte_flow_action_nvgre_encap *nvgre_encap;
570 case RTE_FLOW_ACTION_TYPE_RSS:
571 src.rss = action->conf;
574 (&(struct rte_flow_action_rss){
575 .func = src.rss->func,
576 .level = src.rss->level,
577 .types = src.rss->types,
578 .key_len = src.rss->key_len,
579 .queue_num = src.rss->queue_num,
581 size > sizeof(*dst.rss) ? sizeof(*dst.rss) : size);
582 off = sizeof(*dst.rss);
583 if (src.rss->key_len && src.rss->key) {
584 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->key));
585 tmp = sizeof(*src.rss->key) * src.rss->key_len;
586 if (size >= off + tmp)
587 dst.rss->key = rte_memcpy
588 ((void *)((uintptr_t)dst.rss + off),
592 if (src.rss->queue_num) {
593 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->queue));
594 tmp = sizeof(*src.rss->queue) * src.rss->queue_num;
595 if (size >= off + tmp)
596 dst.rss->queue = rte_memcpy
597 ((void *)((uintptr_t)dst.rss + off),
598 src.rss->queue, tmp);
602 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
603 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
604 src.vxlan_encap = action->conf;
605 dst.vxlan_encap = buf;
606 RTE_BUILD_BUG_ON(sizeof(*src.vxlan_encap) !=
607 sizeof(*src.nvgre_encap) ||
608 offsetof(struct rte_flow_action_vxlan_encap,
610 offsetof(struct rte_flow_action_nvgre_encap,
612 off = sizeof(*dst.vxlan_encap);
613 if (src.vxlan_encap->definition) {
615 (off, sizeof(*dst.vxlan_encap->definition));
617 (RTE_FLOW_CONV_OP_PATTERN,
618 (void *)((uintptr_t)dst.vxlan_encap + off),
619 size > off ? size - off : 0,
620 src.vxlan_encap->definition, NULL);
623 if (size >= off + ret)
624 dst.vxlan_encap->definition =
625 (void *)((uintptr_t)dst.vxlan_encap +
632 * allow PMD private flow action
634 off = (int)action->type >= 0 ?
635 rte_flow_desc_action[action->type].size : sizeof(void *);
636 rte_memcpy(buf, action->conf, (size > off ? off : size));
643 * Copy a list of pattern items.
646 * Destination buffer. Can be NULL if @p size is zero.
648 * Size of @p dst in bytes.
650 * Source pattern items.
652 * Maximum number of pattern items to process from @p src or 0 to process
653 * the entire list. In both cases, processing stops after
654 * RTE_FLOW_ITEM_TYPE_END is encountered.
656 * Perform verbose error reporting if not NULL.
659 * A positive value representing the number of bytes needed to store
660 * pattern items regardless of @p size on success (@p buf contents are
661 * truncated to @p size if not large enough), a negative errno value
662 * otherwise and rte_errno is set.
665 rte_flow_conv_pattern(struct rte_flow_item *dst,
667 const struct rte_flow_item *src,
669 struct rte_flow_error *error)
671 uintptr_t data = (uintptr_t)dst;
676 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
678 * allow PMD private flow item
680 if (((int)src->type >= 0) &&
681 ((size_t)src->type >= RTE_DIM(rte_flow_desc_item) ||
682 !rte_flow_desc_item[src->type].name))
683 return rte_flow_error_set
684 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, src,
685 "cannot convert unknown item type");
686 if (size >= off + sizeof(*dst))
687 *dst = (struct rte_flow_item){
699 off = RTE_ALIGN_CEIL(off, sizeof(double));
700 ret = rte_flow_conv_item_spec
701 ((void *)(data + off),
702 size > off ? size - off : 0, src,
703 RTE_FLOW_CONV_ITEM_SPEC);
704 if (size && size >= off + ret)
705 dst->spec = (void *)(data + off);
710 off = RTE_ALIGN_CEIL(off, sizeof(double));
711 ret = rte_flow_conv_item_spec
712 ((void *)(data + off),
713 size > off ? size - off : 0, src,
714 RTE_FLOW_CONV_ITEM_LAST);
715 if (size && size >= off + ret)
716 dst->last = (void *)(data + off);
720 off = RTE_ALIGN_CEIL(off, sizeof(double));
721 ret = rte_flow_conv_item_spec
722 ((void *)(data + off),
723 size > off ? size - off : 0, src,
724 RTE_FLOW_CONV_ITEM_MASK);
725 if (size && size >= off + ret)
726 dst->mask = (void *)(data + off);
736 * Copy a list of actions.
739 * Destination buffer. Can be NULL if @p size is zero.
741 * Size of @p dst in bytes.
745 * Maximum number of actions to process from @p src or 0 to process the
746 * entire list. In both cases, processing stops after
747 * RTE_FLOW_ACTION_TYPE_END is encountered.
749 * Perform verbose error reporting if not NULL.
752 * A positive value representing the number of bytes needed to store
753 * actions regardless of @p size on success (@p buf contents are truncated
754 * to @p size if not large enough), a negative errno value otherwise and
758 rte_flow_conv_actions(struct rte_flow_action *dst,
760 const struct rte_flow_action *src,
762 struct rte_flow_error *error)
764 uintptr_t data = (uintptr_t)dst;
769 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
771 * allow PMD private flow action
773 if (((int)src->type >= 0) &&
774 ((size_t)src->type >= RTE_DIM(rte_flow_desc_action) ||
775 !rte_flow_desc_action[src->type].name))
776 return rte_flow_error_set
777 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
778 src, "cannot convert unknown action type");
779 if (size >= off + sizeof(*dst))
780 *dst = (struct rte_flow_action){
792 off = RTE_ALIGN_CEIL(off, sizeof(double));
793 ret = rte_flow_conv_action_conf
794 ((void *)(data + off),
795 size > off ? size - off : 0, src);
796 if (size && size >= off + ret)
797 dst->conf = (void *)(data + off);
807 * Copy flow rule components.
809 * This comprises the flow rule descriptor itself, attributes, pattern and
810 * actions list. NULL components in @p src are skipped.
813 * Destination buffer. Can be NULL if @p size is zero.
815 * Size of @p dst in bytes.
817 * Source flow rule descriptor.
819 * Perform verbose error reporting if not NULL.
822 * A positive value representing the number of bytes needed to store all
823 * components including the descriptor regardless of @p size on success
824 * (@p buf contents are truncated to @p size if not large enough), a
825 * negative errno value otherwise and rte_errno is set.
828 rte_flow_conv_rule(struct rte_flow_conv_rule *dst,
830 const struct rte_flow_conv_rule *src,
831 struct rte_flow_error *error)
837 (&(struct rte_flow_conv_rule){
842 size > sizeof(*dst) ? sizeof(*dst) : size);
845 off = RTE_ALIGN_CEIL(off, sizeof(double));
846 if (size && size >= off + sizeof(*dst->attr))
847 dst->attr = rte_memcpy
848 ((void *)((uintptr_t)dst + off),
849 src->attr_ro, sizeof(*dst->attr));
850 off += sizeof(*dst->attr);
852 if (src->pattern_ro) {
853 off = RTE_ALIGN_CEIL(off, sizeof(double));
854 ret = rte_flow_conv_pattern((void *)((uintptr_t)dst + off),
855 size > off ? size - off : 0,
856 src->pattern_ro, 0, error);
859 if (size && size >= off + (size_t)ret)
860 dst->pattern = (void *)((uintptr_t)dst + off);
863 if (src->actions_ro) {
864 off = RTE_ALIGN_CEIL(off, sizeof(double));
865 ret = rte_flow_conv_actions((void *)((uintptr_t)dst + off),
866 size > off ? size - off : 0,
867 src->actions_ro, 0, error);
870 if (size >= off + (size_t)ret)
871 dst->actions = (void *)((uintptr_t)dst + off);
878 * Retrieve the name of a pattern item/action type.
881 * Nonzero when @p src represents an action type instead of a pattern item
884 * Nonzero to write string address instead of contents into @p dst.
886 * Destination buffer. Can be NULL if @p size is zero.
888 * Size of @p dst in bytes.
890 * Depending on @p is_action, source pattern item or action type cast as a
893 * Perform verbose error reporting if not NULL.
896 * A positive value representing the number of bytes needed to store the
897 * name or its address regardless of @p size on success (@p buf contents
898 * are truncated to @p size if not large enough), a negative errno value
899 * otherwise and rte_errno is set.
902 rte_flow_conv_name(int is_action,
907 struct rte_flow_error *error)
910 const struct rte_flow_desc_data *data;
913 static const struct desc_info info_rep[2] = {
914 { rte_flow_desc_item, RTE_DIM(rte_flow_desc_item), },
915 { rte_flow_desc_action, RTE_DIM(rte_flow_desc_action), },
917 const struct desc_info *const info = &info_rep[!!is_action];
918 unsigned int type = (uintptr_t)src;
920 if (type >= info->num)
921 return rte_flow_error_set
922 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
923 "unknown object type to retrieve the name of");
925 return strlcpy(dst, info->data[type].name, size);
926 if (size >= sizeof(const char **))
927 *((const char **)dst) = info->data[type].name;
928 return sizeof(const char **);
931 /** Helper function to convert flow API objects. */
933 rte_flow_conv(enum rte_flow_conv_op op,
937 struct rte_flow_error *error)
940 const struct rte_flow_attr *attr;
942 case RTE_FLOW_CONV_OP_NONE:
944 case RTE_FLOW_CONV_OP_ATTR:
946 if (size > sizeof(*attr))
947 size = sizeof(*attr);
948 rte_memcpy(dst, attr, size);
949 return sizeof(*attr);
950 case RTE_FLOW_CONV_OP_ITEM:
951 return rte_flow_conv_pattern(dst, size, src, 1, error);
952 case RTE_FLOW_CONV_OP_ACTION:
953 return rte_flow_conv_actions(dst, size, src, 1, error);
954 case RTE_FLOW_CONV_OP_PATTERN:
955 return rte_flow_conv_pattern(dst, size, src, 0, error);
956 case RTE_FLOW_CONV_OP_ACTIONS:
957 return rte_flow_conv_actions(dst, size, src, 0, error);
958 case RTE_FLOW_CONV_OP_RULE:
959 return rte_flow_conv_rule(dst, size, src, error);
960 case RTE_FLOW_CONV_OP_ITEM_NAME:
961 return rte_flow_conv_name(0, 0, dst, size, src, error);
962 case RTE_FLOW_CONV_OP_ACTION_NAME:
963 return rte_flow_conv_name(1, 0, dst, size, src, error);
964 case RTE_FLOW_CONV_OP_ITEM_NAME_PTR:
965 return rte_flow_conv_name(0, 1, dst, size, src, error);
966 case RTE_FLOW_CONV_OP_ACTION_NAME_PTR:
967 return rte_flow_conv_name(1, 1, dst, size, src, error);
969 return rte_flow_error_set
970 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
971 "unknown object conversion operation");
974 /** Store a full rte_flow description. */
976 rte_flow_copy(struct rte_flow_desc *desc, size_t len,
977 const struct rte_flow_attr *attr,
978 const struct rte_flow_item *items,
979 const struct rte_flow_action *actions)
982 * Overlap struct rte_flow_conv with struct rte_flow_desc in order
983 * to convert the former to the latter without wasting space.
985 struct rte_flow_conv_rule *dst =
987 (void *)((uintptr_t)desc +
988 (offsetof(struct rte_flow_desc, actions) -
989 offsetof(struct rte_flow_conv_rule, actions))) :
992 len > sizeof(*desc) - sizeof(*dst) ?
993 len - (sizeof(*desc) - sizeof(*dst)) :
995 struct rte_flow_conv_rule src = {
998 .actions_ro = actions,
1002 RTE_BUILD_BUG_ON(sizeof(struct rte_flow_desc) <
1003 sizeof(struct rte_flow_conv_rule));
1005 (&dst->pattern != &desc->items ||
1006 &dst->actions != &desc->actions ||
1007 (uintptr_t)(dst + 1) != (uintptr_t)(desc + 1))) {
1011 ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, dst, dst_size, &src, NULL);
1014 ret += sizeof(*desc) - sizeof(*dst);
1016 (&(struct rte_flow_desc){
1019 .items = dst_size ? dst->pattern : NULL,
1020 .actions = dst_size ? dst->actions : NULL,
1022 len > sizeof(*desc) ? sizeof(*desc) : len);
1027 rte_flow_dev_dump(uint16_t port_id, FILE *file, struct rte_flow_error *error)
1029 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1030 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1035 if (likely(!!ops->dev_dump)) {
1037 ret = ops->dev_dump(dev, file, error);
1039 return flow_err(port_id, ret, error);
1041 return rte_flow_error_set(error, ENOSYS,
1042 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1043 NULL, rte_strerror(ENOSYS));
1047 rte_flow_get_aged_flows(uint16_t port_id, void **contexts,
1048 uint32_t nb_contexts, struct rte_flow_error *error)
1050 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1051 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1056 if (likely(!!ops->get_aged_flows)) {
1058 ret = ops->get_aged_flows(dev, contexts, nb_contexts, error);
1060 return flow_err(port_id, ret, error);
1062 return rte_flow_error_set(error, ENOTSUP,
1063 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1064 NULL, rte_strerror(ENOTSUP));
1067 struct rte_flow_shared_action *
1068 rte_flow_shared_action_create(uint16_t port_id,
1069 const struct rte_flow_shared_action_conf *conf,
1070 const struct rte_flow_action *action,
1071 struct rte_flow_error *error)
1073 struct rte_flow_shared_action *shared_action;
1074 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1078 if (unlikely(!ops->shared_action_create)) {
1079 rte_flow_error_set(error, ENOSYS,
1080 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1081 rte_strerror(ENOSYS));
1084 shared_action = ops->shared_action_create(&rte_eth_devices[port_id],
1085 conf, action, error);
1086 if (shared_action == NULL)
1087 flow_err(port_id, -rte_errno, error);
1088 return shared_action;
1092 rte_flow_shared_action_destroy(uint16_t port_id,
1093 struct rte_flow_shared_action *action,
1094 struct rte_flow_error *error)
1097 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1101 if (unlikely(!ops->shared_action_destroy))
1102 return rte_flow_error_set(error, ENOSYS,
1103 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1104 NULL, rte_strerror(ENOSYS));
1105 ret = ops->shared_action_destroy(&rte_eth_devices[port_id], action,
1107 return flow_err(port_id, ret, error);
1111 rte_flow_shared_action_update(uint16_t port_id,
1112 struct rte_flow_shared_action *action,
1113 const struct rte_flow_action *update,
1114 struct rte_flow_error *error)
1117 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1121 if (unlikely(!ops->shared_action_update))
1122 return rte_flow_error_set(error, ENOSYS,
1123 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1124 NULL, rte_strerror(ENOSYS));
1125 ret = ops->shared_action_update(&rte_eth_devices[port_id], action,
1127 return flow_err(port_id, ret, error);
1131 rte_flow_shared_action_query(uint16_t port_id,
1132 const struct rte_flow_shared_action *action,
1134 struct rte_flow_error *error)
1137 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1141 if (unlikely(!ops->shared_action_query))
1142 return rte_flow_error_set(error, ENOSYS,
1143 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1144 NULL, rte_strerror(ENOSYS));
1145 ret = ops->shared_action_query(&rte_eth_devices[port_id], action,
1147 return flow_err(port_id, ret, error);
1151 rte_flow_tunnel_decap_set(uint16_t port_id,
1152 struct rte_flow_tunnel *tunnel,
1153 struct rte_flow_action **actions,
1154 uint32_t *num_of_actions,
1155 struct rte_flow_error *error)
1157 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1158 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1162 if (likely(!!ops->tunnel_decap_set)) {
1163 return flow_err(port_id,
1164 ops->tunnel_decap_set(dev, tunnel, actions,
1165 num_of_actions, error),
1168 return rte_flow_error_set(error, ENOTSUP,
1169 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1170 NULL, rte_strerror(ENOTSUP));
1174 rte_flow_tunnel_match(uint16_t port_id,
1175 struct rte_flow_tunnel *tunnel,
1176 struct rte_flow_item **items,
1177 uint32_t *num_of_items,
1178 struct rte_flow_error *error)
1180 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1181 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1185 if (likely(!!ops->tunnel_match)) {
1186 return flow_err(port_id,
1187 ops->tunnel_match(dev, tunnel, items,
1188 num_of_items, error),
1191 return rte_flow_error_set(error, ENOTSUP,
1192 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1193 NULL, rte_strerror(ENOTSUP));
1197 rte_flow_get_restore_info(uint16_t port_id,
1199 struct rte_flow_restore_info *restore_info,
1200 struct rte_flow_error *error)
1202 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1203 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1207 if (likely(!!ops->get_restore_info)) {
1208 return flow_err(port_id,
1209 ops->get_restore_info(dev, m, restore_info,
1213 return rte_flow_error_set(error, ENOTSUP,
1214 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1215 NULL, rte_strerror(ENOTSUP));
1219 rte_flow_tunnel_action_decap_release(uint16_t port_id,
1220 struct rte_flow_action *actions,
1221 uint32_t num_of_actions,
1222 struct rte_flow_error *error)
1224 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1225 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1229 if (likely(!!ops->tunnel_action_decap_release)) {
1230 return flow_err(port_id,
1231 ops->tunnel_action_decap_release(dev, actions,
1236 return rte_flow_error_set(error, ENOTSUP,
1237 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1238 NULL, rte_strerror(ENOTSUP));
1242 rte_flow_tunnel_item_release(uint16_t port_id,
1243 struct rte_flow_item *items,
1244 uint32_t num_of_items,
1245 struct rte_flow_error *error)
1247 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1248 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1252 if (likely(!!ops->tunnel_item_release)) {
1253 return flow_err(port_id,
1254 ops->tunnel_item_release(dev, items,
1255 num_of_items, error),
1258 return rte_flow_error_set(error, ENOTSUP,
1259 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1260 NULL, rte_strerror(ENOTSUP));