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)),
102 /** Generate flow_action[] entry. */
103 #define MK_FLOW_ACTION(t, s) \
104 [RTE_FLOW_ACTION_TYPE_ ## t] = { \
109 /** Information about known flow actions. */
110 static const struct rte_flow_desc_data rte_flow_desc_action[] = {
111 MK_FLOW_ACTION(END, 0),
112 MK_FLOW_ACTION(VOID, 0),
113 MK_FLOW_ACTION(PASSTHRU, 0),
114 MK_FLOW_ACTION(JUMP, sizeof(struct rte_flow_action_jump)),
115 MK_FLOW_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
116 MK_FLOW_ACTION(FLAG, 0),
117 MK_FLOW_ACTION(QUEUE, sizeof(struct rte_flow_action_queue)),
118 MK_FLOW_ACTION(DROP, 0),
119 MK_FLOW_ACTION(COUNT, sizeof(struct rte_flow_action_count)),
120 MK_FLOW_ACTION(RSS, sizeof(struct rte_flow_action_rss)),
121 MK_FLOW_ACTION(PF, 0),
122 MK_FLOW_ACTION(VF, sizeof(struct rte_flow_action_vf)),
123 MK_FLOW_ACTION(PHY_PORT, sizeof(struct rte_flow_action_phy_port)),
124 MK_FLOW_ACTION(PORT_ID, sizeof(struct rte_flow_action_port_id)),
125 MK_FLOW_ACTION(METER, sizeof(struct rte_flow_action_meter)),
126 MK_FLOW_ACTION(SECURITY, sizeof(struct rte_flow_action_security)),
127 MK_FLOW_ACTION(OF_SET_MPLS_TTL,
128 sizeof(struct rte_flow_action_of_set_mpls_ttl)),
129 MK_FLOW_ACTION(OF_DEC_MPLS_TTL, 0),
130 MK_FLOW_ACTION(OF_SET_NW_TTL,
131 sizeof(struct rte_flow_action_of_set_nw_ttl)),
132 MK_FLOW_ACTION(OF_DEC_NW_TTL, 0),
133 MK_FLOW_ACTION(OF_COPY_TTL_OUT, 0),
134 MK_FLOW_ACTION(OF_COPY_TTL_IN, 0),
135 MK_FLOW_ACTION(OF_POP_VLAN, 0),
136 MK_FLOW_ACTION(OF_PUSH_VLAN,
137 sizeof(struct rte_flow_action_of_push_vlan)),
138 MK_FLOW_ACTION(OF_SET_VLAN_VID,
139 sizeof(struct rte_flow_action_of_set_vlan_vid)),
140 MK_FLOW_ACTION(OF_SET_VLAN_PCP,
141 sizeof(struct rte_flow_action_of_set_vlan_pcp)),
142 MK_FLOW_ACTION(OF_POP_MPLS,
143 sizeof(struct rte_flow_action_of_pop_mpls)),
144 MK_FLOW_ACTION(OF_PUSH_MPLS,
145 sizeof(struct rte_flow_action_of_push_mpls)),
146 MK_FLOW_ACTION(VXLAN_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
147 MK_FLOW_ACTION(VXLAN_DECAP, 0),
148 MK_FLOW_ACTION(NVGRE_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
149 MK_FLOW_ACTION(NVGRE_DECAP, 0),
150 MK_FLOW_ACTION(RAW_ENCAP, sizeof(struct rte_flow_action_raw_encap)),
151 MK_FLOW_ACTION(RAW_DECAP, sizeof(struct rte_flow_action_raw_decap)),
152 MK_FLOW_ACTION(SET_IPV4_SRC,
153 sizeof(struct rte_flow_action_set_ipv4)),
154 MK_FLOW_ACTION(SET_IPV4_DST,
155 sizeof(struct rte_flow_action_set_ipv4)),
156 MK_FLOW_ACTION(SET_IPV6_SRC,
157 sizeof(struct rte_flow_action_set_ipv6)),
158 MK_FLOW_ACTION(SET_IPV6_DST,
159 sizeof(struct rte_flow_action_set_ipv6)),
160 MK_FLOW_ACTION(SET_TP_SRC,
161 sizeof(struct rte_flow_action_set_tp)),
162 MK_FLOW_ACTION(SET_TP_DST,
163 sizeof(struct rte_flow_action_set_tp)),
164 MK_FLOW_ACTION(MAC_SWAP, 0),
165 MK_FLOW_ACTION(DEC_TTL, 0),
166 MK_FLOW_ACTION(SET_TTL, sizeof(struct rte_flow_action_set_ttl)),
167 MK_FLOW_ACTION(SET_MAC_SRC, sizeof(struct rte_flow_action_set_mac)),
168 MK_FLOW_ACTION(SET_MAC_DST, sizeof(struct rte_flow_action_set_mac)),
169 MK_FLOW_ACTION(INC_TCP_SEQ, sizeof(rte_be32_t)),
170 MK_FLOW_ACTION(DEC_TCP_SEQ, sizeof(rte_be32_t)),
171 MK_FLOW_ACTION(INC_TCP_ACK, sizeof(rte_be32_t)),
172 MK_FLOW_ACTION(DEC_TCP_ACK, sizeof(rte_be32_t)),
173 MK_FLOW_ACTION(SET_TAG, sizeof(struct rte_flow_action_set_tag)),
174 MK_FLOW_ACTION(SET_META, sizeof(struct rte_flow_action_set_meta)),
175 MK_FLOW_ACTION(SET_IPV4_DSCP, sizeof(struct rte_flow_action_set_dscp)),
176 MK_FLOW_ACTION(SET_IPV6_DSCP, sizeof(struct rte_flow_action_set_dscp)),
177 MK_FLOW_ACTION(AGE, sizeof(struct rte_flow_action_age)),
178 MK_FLOW_ACTION(SAMPLE, sizeof(struct rte_flow_action_sample)),
180 * Shared action represented as handle of type
181 * (struct rte_flow_shared action *) stored in conf field (see
182 * struct rte_flow_action); no need for additional structure to * store
183 * shared action handle.
185 MK_FLOW_ACTION(SHARED, 0),
189 rte_flow_dynf_metadata_register(void)
194 static const struct rte_mbuf_dynfield desc_offs = {
195 .name = RTE_MBUF_DYNFIELD_METADATA_NAME,
196 .size = sizeof(uint32_t),
197 .align = __alignof__(uint32_t),
199 static const struct rte_mbuf_dynflag desc_flag = {
200 .name = RTE_MBUF_DYNFLAG_METADATA_NAME,
203 offset = rte_mbuf_dynfield_register(&desc_offs);
206 flag = rte_mbuf_dynflag_register(&desc_flag);
209 rte_flow_dynf_metadata_offs = offset;
210 rte_flow_dynf_metadata_mask = (1ULL << flag);
214 rte_flow_dynf_metadata_offs = -1;
215 rte_flow_dynf_metadata_mask = 0ULL;
220 fts_enter(struct rte_eth_dev *dev)
222 if (!(dev->data->dev_flags & RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE))
223 pthread_mutex_lock(&dev->data->flow_ops_mutex);
227 fts_exit(struct rte_eth_dev *dev)
229 if (!(dev->data->dev_flags & RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE))
230 pthread_mutex_unlock(&dev->data->flow_ops_mutex);
234 flow_err(uint16_t port_id, int ret, struct rte_flow_error *error)
238 if (rte_eth_dev_is_removed(port_id))
239 return rte_flow_error_set(error, EIO,
240 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
241 NULL, rte_strerror(EIO));
245 /* Get generic flow operations structure from a port. */
246 const struct rte_flow_ops *
247 rte_flow_ops_get(uint16_t port_id, struct rte_flow_error *error)
249 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
250 const struct rte_flow_ops *ops;
253 if (unlikely(!rte_eth_dev_is_valid_port(port_id)))
255 else if (unlikely(!dev->dev_ops->filter_ctrl ||
256 dev->dev_ops->filter_ctrl(dev,
257 RTE_ETH_FILTER_GENERIC,
264 rte_flow_error_set(error, code, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
265 NULL, rte_strerror(code));
269 /* Check whether a flow rule can be created on a given port. */
271 rte_flow_validate(uint16_t port_id,
272 const struct rte_flow_attr *attr,
273 const struct rte_flow_item pattern[],
274 const struct rte_flow_action actions[],
275 struct rte_flow_error *error)
277 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
278 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
283 if (likely(!!ops->validate)) {
285 ret = ops->validate(dev, attr, pattern, actions, error);
287 return flow_err(port_id, ret, error);
289 return rte_flow_error_set(error, ENOSYS,
290 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
291 NULL, rte_strerror(ENOSYS));
294 /* Create a flow rule on a given port. */
296 rte_flow_create(uint16_t port_id,
297 const struct rte_flow_attr *attr,
298 const struct rte_flow_item pattern[],
299 const struct rte_flow_action actions[],
300 struct rte_flow_error *error)
302 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
303 struct rte_flow *flow;
304 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
308 if (likely(!!ops->create)) {
310 flow = ops->create(dev, attr, pattern, actions, error);
313 flow_err(port_id, -rte_errno, error);
316 rte_flow_error_set(error, ENOSYS, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
317 NULL, rte_strerror(ENOSYS));
321 /* Destroy a flow rule on a given port. */
323 rte_flow_destroy(uint16_t port_id,
324 struct rte_flow *flow,
325 struct rte_flow_error *error)
327 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
328 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
333 if (likely(!!ops->destroy)) {
335 ret = ops->destroy(dev, flow, error);
337 return flow_err(port_id, ret, error);
339 return rte_flow_error_set(error, ENOSYS,
340 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
341 NULL, rte_strerror(ENOSYS));
344 /* Destroy all flow rules associated with a port. */
346 rte_flow_flush(uint16_t port_id,
347 struct rte_flow_error *error)
349 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
350 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
355 if (likely(!!ops->flush)) {
357 ret = ops->flush(dev, error);
359 return flow_err(port_id, ret, error);
361 return rte_flow_error_set(error, ENOSYS,
362 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
363 NULL, rte_strerror(ENOSYS));
366 /* Query an existing flow rule. */
368 rte_flow_query(uint16_t port_id,
369 struct rte_flow *flow,
370 const struct rte_flow_action *action,
372 struct rte_flow_error *error)
374 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
375 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
380 if (likely(!!ops->query)) {
382 ret = ops->query(dev, flow, action, data, error);
384 return flow_err(port_id, ret, error);
386 return rte_flow_error_set(error, ENOSYS,
387 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
388 NULL, rte_strerror(ENOSYS));
391 /* Restrict ingress traffic to the defined flow rules. */
393 rte_flow_isolate(uint16_t port_id,
395 struct rte_flow_error *error)
397 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
398 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
403 if (likely(!!ops->isolate)) {
405 ret = ops->isolate(dev, set, error);
407 return flow_err(port_id, ret, error);
409 return rte_flow_error_set(error, ENOSYS,
410 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
411 NULL, rte_strerror(ENOSYS));
414 /* Initialize flow error structure. */
416 rte_flow_error_set(struct rte_flow_error *error,
418 enum rte_flow_error_type type,
423 *error = (struct rte_flow_error){
433 /** Pattern item specification types. */
434 enum rte_flow_conv_item_spec_type {
435 RTE_FLOW_CONV_ITEM_SPEC,
436 RTE_FLOW_CONV_ITEM_LAST,
437 RTE_FLOW_CONV_ITEM_MASK,
441 * Copy pattern item specification.
444 * Output buffer. Can be NULL if @p size is zero.
446 * Size of @p buf in bytes.
448 * Pattern item to copy specification from.
450 * Specification selector for either @p spec, @p last or @p mask.
453 * Number of bytes needed to store pattern item specification regardless
454 * of @p size. @p buf contents are truncated to @p size if not large
458 rte_flow_conv_item_spec(void *buf, const size_t size,
459 const struct rte_flow_item *item,
460 enum rte_flow_conv_item_spec_type type)
464 type == RTE_FLOW_CONV_ITEM_SPEC ? item->spec :
465 type == RTE_FLOW_CONV_ITEM_LAST ? item->last :
466 type == RTE_FLOW_CONV_ITEM_MASK ? item->mask :
469 switch (item->type) {
471 const struct rte_flow_item_raw *raw;
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 struct rte_flow_item_raw *raw;
487 case RTE_FLOW_ITEM_TYPE_RAW:
488 spec.raw = item->spec;
489 last.raw = item->last ? item->last : item->spec;
490 mask.raw = item->mask ? item->mask : &rte_flow_item_raw_mask;
494 (&(struct rte_flow_item_raw){
495 .relative = src.raw->relative,
496 .search = src.raw->search,
497 .reserved = src.raw->reserved,
498 .offset = src.raw->offset,
499 .limit = src.raw->limit,
500 .length = src.raw->length,
502 size > sizeof(*dst.raw) ? sizeof(*dst.raw) : size);
503 off = sizeof(*dst.raw);
504 if (type == RTE_FLOW_CONV_ITEM_SPEC ||
505 (type == RTE_FLOW_CONV_ITEM_MASK &&
506 ((spec.raw->length & mask.raw->length) >=
507 (last.raw->length & mask.raw->length))))
508 tmp = spec.raw->length & mask.raw->length;
510 tmp = last.raw->length & mask.raw->length;
512 off = RTE_ALIGN_CEIL(off, sizeof(*dst.raw->pattern));
513 if (size >= off + tmp)
514 dst.raw->pattern = rte_memcpy
515 ((void *)((uintptr_t)dst.raw + off),
516 src.raw->pattern, tmp);
521 off = rte_flow_desc_item[item->type].size;
522 rte_memcpy(buf, data, (size > off ? off : size));
529 * Copy action configuration.
532 * Output buffer. Can be NULL if @p size is zero.
534 * Size of @p buf in bytes.
536 * Action to copy configuration from.
539 * Number of bytes needed to store pattern item specification regardless
540 * of @p size. @p buf contents are truncated to @p size if not large
544 rte_flow_conv_action_conf(void *buf, const size_t size,
545 const struct rte_flow_action *action)
549 switch (action->type) {
551 const struct rte_flow_action_rss *rss;
552 const struct rte_flow_action_vxlan_encap *vxlan_encap;
553 const struct rte_flow_action_nvgre_encap *nvgre_encap;
556 struct rte_flow_action_rss *rss;
557 struct rte_flow_action_vxlan_encap *vxlan_encap;
558 struct rte_flow_action_nvgre_encap *nvgre_encap;
563 case RTE_FLOW_ACTION_TYPE_RSS:
564 src.rss = action->conf;
567 (&(struct rte_flow_action_rss){
568 .func = src.rss->func,
569 .level = src.rss->level,
570 .types = src.rss->types,
571 .key_len = src.rss->key_len,
572 .queue_num = src.rss->queue_num,
574 size > sizeof(*dst.rss) ? sizeof(*dst.rss) : size);
575 off = sizeof(*dst.rss);
576 if (src.rss->key_len) {
577 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->key));
578 tmp = sizeof(*src.rss->key) * src.rss->key_len;
579 if (size >= off + tmp)
580 dst.rss->key = rte_memcpy
581 ((void *)((uintptr_t)dst.rss + off),
585 if (src.rss->queue_num) {
586 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->queue));
587 tmp = sizeof(*src.rss->queue) * src.rss->queue_num;
588 if (size >= off + tmp)
589 dst.rss->queue = rte_memcpy
590 ((void *)((uintptr_t)dst.rss + off),
591 src.rss->queue, tmp);
595 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
596 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
597 src.vxlan_encap = action->conf;
598 dst.vxlan_encap = buf;
599 RTE_BUILD_BUG_ON(sizeof(*src.vxlan_encap) !=
600 sizeof(*src.nvgre_encap) ||
601 offsetof(struct rte_flow_action_vxlan_encap,
603 offsetof(struct rte_flow_action_nvgre_encap,
605 off = sizeof(*dst.vxlan_encap);
606 if (src.vxlan_encap->definition) {
608 (off, sizeof(*dst.vxlan_encap->definition));
610 (RTE_FLOW_CONV_OP_PATTERN,
611 (void *)((uintptr_t)dst.vxlan_encap + off),
612 size > off ? size - off : 0,
613 src.vxlan_encap->definition, NULL);
616 if (size >= off + ret)
617 dst.vxlan_encap->definition =
618 (void *)((uintptr_t)dst.vxlan_encap +
624 off = rte_flow_desc_action[action->type].size;
625 rte_memcpy(buf, action->conf, (size > off ? off : size));
632 * Copy a list of pattern items.
635 * Destination buffer. Can be NULL if @p size is zero.
637 * Size of @p dst in bytes.
639 * Source pattern items.
641 * Maximum number of pattern items to process from @p src or 0 to process
642 * the entire list. In both cases, processing stops after
643 * RTE_FLOW_ITEM_TYPE_END is encountered.
645 * Perform verbose error reporting if not NULL.
648 * A positive value representing the number of bytes needed to store
649 * pattern items regardless of @p size on success (@p buf contents are
650 * truncated to @p size if not large enough), a negative errno value
651 * otherwise and rte_errno is set.
654 rte_flow_conv_pattern(struct rte_flow_item *dst,
656 const struct rte_flow_item *src,
658 struct rte_flow_error *error)
660 uintptr_t data = (uintptr_t)dst;
665 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
666 if ((size_t)src->type >= RTE_DIM(rte_flow_desc_item) ||
667 !rte_flow_desc_item[src->type].name)
668 return rte_flow_error_set
669 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, src,
670 "cannot convert unknown item type");
671 if (size >= off + sizeof(*dst))
672 *dst = (struct rte_flow_item){
684 off = RTE_ALIGN_CEIL(off, sizeof(double));
685 ret = rte_flow_conv_item_spec
686 ((void *)(data + off),
687 size > off ? size - off : 0, src,
688 RTE_FLOW_CONV_ITEM_SPEC);
689 if (size && size >= off + ret)
690 dst->spec = (void *)(data + off);
695 off = RTE_ALIGN_CEIL(off, sizeof(double));
696 ret = rte_flow_conv_item_spec
697 ((void *)(data + off),
698 size > off ? size - off : 0, src,
699 RTE_FLOW_CONV_ITEM_LAST);
700 if (size && size >= off + ret)
701 dst->last = (void *)(data + off);
705 off = RTE_ALIGN_CEIL(off, sizeof(double));
706 ret = rte_flow_conv_item_spec
707 ((void *)(data + off),
708 size > off ? size - off : 0, src,
709 RTE_FLOW_CONV_ITEM_MASK);
710 if (size && size >= off + ret)
711 dst->mask = (void *)(data + off);
721 * Copy a list of actions.
724 * Destination buffer. Can be NULL if @p size is zero.
726 * Size of @p dst in bytes.
730 * Maximum number of actions to process from @p src or 0 to process the
731 * entire list. In both cases, processing stops after
732 * RTE_FLOW_ACTION_TYPE_END is encountered.
734 * Perform verbose error reporting if not NULL.
737 * A positive value representing the number of bytes needed to store
738 * actions regardless of @p size on success (@p buf contents are truncated
739 * to @p size if not large enough), a negative errno value otherwise and
743 rte_flow_conv_actions(struct rte_flow_action *dst,
745 const struct rte_flow_action *src,
747 struct rte_flow_error *error)
749 uintptr_t data = (uintptr_t)dst;
754 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
755 if ((size_t)src->type >= RTE_DIM(rte_flow_desc_action) ||
756 !rte_flow_desc_action[src->type].name)
757 return rte_flow_error_set
758 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
759 src, "cannot convert unknown action type");
760 if (size >= off + sizeof(*dst))
761 *dst = (struct rte_flow_action){
773 off = RTE_ALIGN_CEIL(off, sizeof(double));
774 ret = rte_flow_conv_action_conf
775 ((void *)(data + off),
776 size > off ? size - off : 0, src);
777 if (size && size >= off + ret)
778 dst->conf = (void *)(data + off);
788 * Copy flow rule components.
790 * This comprises the flow rule descriptor itself, attributes, pattern and
791 * actions list. NULL components in @p src are skipped.
794 * Destination buffer. Can be NULL if @p size is zero.
796 * Size of @p dst in bytes.
798 * Source flow rule descriptor.
800 * Perform verbose error reporting if not NULL.
803 * A positive value representing the number of bytes needed to store all
804 * components including the descriptor regardless of @p size on success
805 * (@p buf contents are truncated to @p size if not large enough), a
806 * negative errno value otherwise and rte_errno is set.
809 rte_flow_conv_rule(struct rte_flow_conv_rule *dst,
811 const struct rte_flow_conv_rule *src,
812 struct rte_flow_error *error)
818 (&(struct rte_flow_conv_rule){
823 size > sizeof(*dst) ? sizeof(*dst) : size);
826 off = RTE_ALIGN_CEIL(off, sizeof(double));
827 if (size && size >= off + sizeof(*dst->attr))
828 dst->attr = rte_memcpy
829 ((void *)((uintptr_t)dst + off),
830 src->attr_ro, sizeof(*dst->attr));
831 off += sizeof(*dst->attr);
833 if (src->pattern_ro) {
834 off = RTE_ALIGN_CEIL(off, sizeof(double));
835 ret = rte_flow_conv_pattern((void *)((uintptr_t)dst + off),
836 size > off ? size - off : 0,
837 src->pattern_ro, 0, error);
840 if (size && size >= off + (size_t)ret)
841 dst->pattern = (void *)((uintptr_t)dst + off);
844 if (src->actions_ro) {
845 off = RTE_ALIGN_CEIL(off, sizeof(double));
846 ret = rte_flow_conv_actions((void *)((uintptr_t)dst + off),
847 size > off ? size - off : 0,
848 src->actions_ro, 0, error);
851 if (size >= off + (size_t)ret)
852 dst->actions = (void *)((uintptr_t)dst + off);
859 * Retrieve the name of a pattern item/action type.
862 * Nonzero when @p src represents an action type instead of a pattern item
865 * Nonzero to write string address instead of contents into @p dst.
867 * Destination buffer. Can be NULL if @p size is zero.
869 * Size of @p dst in bytes.
871 * Depending on @p is_action, source pattern item or action type cast as a
874 * Perform verbose error reporting if not NULL.
877 * A positive value representing the number of bytes needed to store the
878 * name or its address regardless of @p size on success (@p buf contents
879 * are truncated to @p size if not large enough), a negative errno value
880 * otherwise and rte_errno is set.
883 rte_flow_conv_name(int is_action,
888 struct rte_flow_error *error)
891 const struct rte_flow_desc_data *data;
894 static const struct desc_info info_rep[2] = {
895 { rte_flow_desc_item, RTE_DIM(rte_flow_desc_item), },
896 { rte_flow_desc_action, RTE_DIM(rte_flow_desc_action), },
898 const struct desc_info *const info = &info_rep[!!is_action];
899 unsigned int type = (uintptr_t)src;
901 if (type >= info->num)
902 return rte_flow_error_set
903 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
904 "unknown object type to retrieve the name of");
906 return strlcpy(dst, info->data[type].name, size);
907 if (size >= sizeof(const char **))
908 *((const char **)dst) = info->data[type].name;
909 return sizeof(const char **);
912 /** Helper function to convert flow API objects. */
914 rte_flow_conv(enum rte_flow_conv_op op,
918 struct rte_flow_error *error)
921 const struct rte_flow_attr *attr;
923 case RTE_FLOW_CONV_OP_NONE:
925 case RTE_FLOW_CONV_OP_ATTR:
927 if (size > sizeof(*attr))
928 size = sizeof(*attr);
929 rte_memcpy(dst, attr, size);
930 return sizeof(*attr);
931 case RTE_FLOW_CONV_OP_ITEM:
932 return rte_flow_conv_pattern(dst, size, src, 1, error);
933 case RTE_FLOW_CONV_OP_ACTION:
934 return rte_flow_conv_actions(dst, size, src, 1, error);
935 case RTE_FLOW_CONV_OP_PATTERN:
936 return rte_flow_conv_pattern(dst, size, src, 0, error);
937 case RTE_FLOW_CONV_OP_ACTIONS:
938 return rte_flow_conv_actions(dst, size, src, 0, error);
939 case RTE_FLOW_CONV_OP_RULE:
940 return rte_flow_conv_rule(dst, size, src, error);
941 case RTE_FLOW_CONV_OP_ITEM_NAME:
942 return rte_flow_conv_name(0, 0, dst, size, src, error);
943 case RTE_FLOW_CONV_OP_ACTION_NAME:
944 return rte_flow_conv_name(1, 0, dst, size, src, error);
945 case RTE_FLOW_CONV_OP_ITEM_NAME_PTR:
946 return rte_flow_conv_name(0, 1, dst, size, src, error);
947 case RTE_FLOW_CONV_OP_ACTION_NAME_PTR:
948 return rte_flow_conv_name(1, 1, dst, size, src, error);
950 return rte_flow_error_set
951 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
952 "unknown object conversion operation");
955 /** Store a full rte_flow description. */
957 rte_flow_copy(struct rte_flow_desc *desc, size_t len,
958 const struct rte_flow_attr *attr,
959 const struct rte_flow_item *items,
960 const struct rte_flow_action *actions)
963 * Overlap struct rte_flow_conv with struct rte_flow_desc in order
964 * to convert the former to the latter without wasting space.
966 struct rte_flow_conv_rule *dst =
968 (void *)((uintptr_t)desc +
969 (offsetof(struct rte_flow_desc, actions) -
970 offsetof(struct rte_flow_conv_rule, actions))) :
973 len > sizeof(*desc) - sizeof(*dst) ?
974 len - (sizeof(*desc) - sizeof(*dst)) :
976 struct rte_flow_conv_rule src = {
979 .actions_ro = actions,
983 RTE_BUILD_BUG_ON(sizeof(struct rte_flow_desc) <
984 sizeof(struct rte_flow_conv_rule));
986 (&dst->pattern != &desc->items ||
987 &dst->actions != &desc->actions ||
988 (uintptr_t)(dst + 1) != (uintptr_t)(desc + 1))) {
992 ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, dst, dst_size, &src, NULL);
995 ret += sizeof(*desc) - sizeof(*dst);
997 (&(struct rte_flow_desc){
1000 .items = dst_size ? dst->pattern : NULL,
1001 .actions = dst_size ? dst->actions : NULL,
1003 len > sizeof(*desc) ? sizeof(*desc) : len);
1008 rte_flow_dev_dump(uint16_t port_id, FILE *file, struct rte_flow_error *error)
1010 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1011 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1016 if (likely(!!ops->dev_dump)) {
1018 ret = ops->dev_dump(dev, file, error);
1020 return flow_err(port_id, ret, error);
1022 return rte_flow_error_set(error, ENOSYS,
1023 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1024 NULL, rte_strerror(ENOSYS));
1028 rte_flow_get_aged_flows(uint16_t port_id, void **contexts,
1029 uint32_t nb_contexts, struct rte_flow_error *error)
1031 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1032 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1037 if (likely(!!ops->get_aged_flows)) {
1039 ret = ops->get_aged_flows(dev, contexts, nb_contexts, error);
1041 return flow_err(port_id, ret, error);
1043 return rte_flow_error_set(error, ENOTSUP,
1044 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1045 NULL, rte_strerror(ENOTSUP));
1048 struct rte_flow_shared_action *
1049 rte_flow_shared_action_create(uint16_t port_id,
1050 const struct rte_flow_shared_action_conf *conf,
1051 const struct rte_flow_action *action,
1052 struct rte_flow_error *error)
1054 struct rte_flow_shared_action *shared_action;
1055 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1059 if (unlikely(!ops->shared_action_create)) {
1060 rte_flow_error_set(error, ENOSYS,
1061 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1062 rte_strerror(ENOSYS));
1065 shared_action = ops->shared_action_create(&rte_eth_devices[port_id],
1066 conf, action, error);
1067 if (shared_action == NULL)
1068 flow_err(port_id, -rte_errno, error);
1069 return shared_action;
1073 rte_flow_shared_action_destroy(uint16_t port_id,
1074 struct rte_flow_shared_action *action,
1075 struct rte_flow_error *error)
1078 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1082 if (unlikely(!ops->shared_action_destroy))
1083 return rte_flow_error_set(error, ENOSYS,
1084 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1085 NULL, rte_strerror(ENOSYS));
1086 ret = ops->shared_action_destroy(&rte_eth_devices[port_id], action,
1088 return flow_err(port_id, ret, error);
1092 rte_flow_shared_action_update(uint16_t port_id,
1093 struct rte_flow_shared_action *action,
1094 const struct rte_flow_action *update,
1095 struct rte_flow_error *error)
1098 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1102 if (unlikely(!ops->shared_action_update))
1103 return rte_flow_error_set(error, ENOSYS,
1104 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1105 NULL, rte_strerror(ENOSYS));
1106 ret = ops->shared_action_update(&rte_eth_devices[port_id], action,
1108 return flow_err(port_id, ret, error);
1112 rte_flow_shared_action_query(uint16_t port_id,
1113 const struct rte_flow_shared_action *action,
1115 struct rte_flow_error *error)
1118 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1122 if (unlikely(!ops->shared_action_query))
1123 return rte_flow_error_set(error, ENOSYS,
1124 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1125 NULL, rte_strerror(ENOSYS));
1126 ret = ops->shared_action_query(&rte_eth_devices[port_id], action,
1128 return flow_err(port_id, ret, error);