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>
15 #include "rte_ethdev.h"
16 #include "rte_flow_driver.h"
20 * Flow elements description tables.
22 struct rte_flow_desc_data {
27 /** Generate flow_item[] entry. */
28 #define MK_FLOW_ITEM(t, s) \
29 [RTE_FLOW_ITEM_TYPE_ ## t] = { \
34 /** Information about known flow pattern items. */
35 static const struct rte_flow_desc_data rte_flow_desc_item[] = {
37 MK_FLOW_ITEM(VOID, 0),
38 MK_FLOW_ITEM(INVERT, 0),
39 MK_FLOW_ITEM(ANY, sizeof(struct rte_flow_item_any)),
41 MK_FLOW_ITEM(VF, sizeof(struct rte_flow_item_vf)),
42 MK_FLOW_ITEM(PHY_PORT, sizeof(struct rte_flow_item_phy_port)),
43 MK_FLOW_ITEM(PORT_ID, sizeof(struct rte_flow_item_port_id)),
44 MK_FLOW_ITEM(RAW, sizeof(struct rte_flow_item_raw)),
45 MK_FLOW_ITEM(ETH, sizeof(struct rte_flow_item_eth)),
46 MK_FLOW_ITEM(VLAN, sizeof(struct rte_flow_item_vlan)),
47 MK_FLOW_ITEM(IPV4, sizeof(struct rte_flow_item_ipv4)),
48 MK_FLOW_ITEM(IPV6, sizeof(struct rte_flow_item_ipv6)),
49 MK_FLOW_ITEM(ICMP, sizeof(struct rte_flow_item_icmp)),
50 MK_FLOW_ITEM(UDP, sizeof(struct rte_flow_item_udp)),
51 MK_FLOW_ITEM(TCP, sizeof(struct rte_flow_item_tcp)),
52 MK_FLOW_ITEM(SCTP, sizeof(struct rte_flow_item_sctp)),
53 MK_FLOW_ITEM(VXLAN, sizeof(struct rte_flow_item_vxlan)),
54 MK_FLOW_ITEM(E_TAG, sizeof(struct rte_flow_item_e_tag)),
55 MK_FLOW_ITEM(NVGRE, sizeof(struct rte_flow_item_nvgre)),
56 MK_FLOW_ITEM(MPLS, sizeof(struct rte_flow_item_mpls)),
57 MK_FLOW_ITEM(GRE, sizeof(struct rte_flow_item_gre)),
58 MK_FLOW_ITEM(FUZZY, sizeof(struct rte_flow_item_fuzzy)),
59 MK_FLOW_ITEM(GTP, sizeof(struct rte_flow_item_gtp)),
60 MK_FLOW_ITEM(GTPC, sizeof(struct rte_flow_item_gtp)),
61 MK_FLOW_ITEM(GTPU, sizeof(struct rte_flow_item_gtp)),
62 MK_FLOW_ITEM(ESP, sizeof(struct rte_flow_item_esp)),
63 MK_FLOW_ITEM(GENEVE, sizeof(struct rte_flow_item_geneve)),
64 MK_FLOW_ITEM(VXLAN_GPE, sizeof(struct rte_flow_item_vxlan_gpe)),
65 MK_FLOW_ITEM(ARP_ETH_IPV4, sizeof(struct rte_flow_item_arp_eth_ipv4)),
66 MK_FLOW_ITEM(IPV6_EXT, sizeof(struct rte_flow_item_ipv6_ext)),
67 MK_FLOW_ITEM(ICMP6, sizeof(struct rte_flow_item_icmp6)),
68 MK_FLOW_ITEM(ICMP6_ND_NS, sizeof(struct rte_flow_item_icmp6_nd_ns)),
69 MK_FLOW_ITEM(ICMP6_ND_NA, sizeof(struct rte_flow_item_icmp6_nd_na)),
70 MK_FLOW_ITEM(ICMP6_ND_OPT, sizeof(struct rte_flow_item_icmp6_nd_opt)),
71 MK_FLOW_ITEM(ICMP6_ND_OPT_SLA_ETH,
72 sizeof(struct rte_flow_item_icmp6_nd_opt_sla_eth)),
73 MK_FLOW_ITEM(ICMP6_ND_OPT_TLA_ETH,
74 sizeof(struct rte_flow_item_icmp6_nd_opt_tla_eth)),
75 MK_FLOW_ITEM(MARK, sizeof(struct rte_flow_item_mark)),
78 /** Generate flow_action[] entry. */
79 #define MK_FLOW_ACTION(t, s) \
80 [RTE_FLOW_ACTION_TYPE_ ## t] = { \
85 /** Information about known flow actions. */
86 static const struct rte_flow_desc_data rte_flow_desc_action[] = {
87 MK_FLOW_ACTION(END, 0),
88 MK_FLOW_ACTION(VOID, 0),
89 MK_FLOW_ACTION(PASSTHRU, 0),
90 MK_FLOW_ACTION(JUMP, sizeof(struct rte_flow_action_jump)),
91 MK_FLOW_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
92 MK_FLOW_ACTION(FLAG, 0),
93 MK_FLOW_ACTION(QUEUE, sizeof(struct rte_flow_action_queue)),
94 MK_FLOW_ACTION(DROP, 0),
95 MK_FLOW_ACTION(COUNT, sizeof(struct rte_flow_action_count)),
96 MK_FLOW_ACTION(RSS, sizeof(struct rte_flow_action_rss)),
97 MK_FLOW_ACTION(PF, 0),
98 MK_FLOW_ACTION(VF, sizeof(struct rte_flow_action_vf)),
99 MK_FLOW_ACTION(PHY_PORT, sizeof(struct rte_flow_action_phy_port)),
100 MK_FLOW_ACTION(PORT_ID, sizeof(struct rte_flow_action_port_id)),
101 MK_FLOW_ACTION(METER, sizeof(struct rte_flow_action_meter)),
102 MK_FLOW_ACTION(SECURITY, sizeof(struct rte_flow_action_security)),
103 MK_FLOW_ACTION(OF_SET_MPLS_TTL,
104 sizeof(struct rte_flow_action_of_set_mpls_ttl)),
105 MK_FLOW_ACTION(OF_DEC_MPLS_TTL, 0),
106 MK_FLOW_ACTION(OF_SET_NW_TTL,
107 sizeof(struct rte_flow_action_of_set_nw_ttl)),
108 MK_FLOW_ACTION(OF_DEC_NW_TTL, 0),
109 MK_FLOW_ACTION(OF_COPY_TTL_OUT, 0),
110 MK_FLOW_ACTION(OF_COPY_TTL_IN, 0),
111 MK_FLOW_ACTION(OF_POP_VLAN, 0),
112 MK_FLOW_ACTION(OF_PUSH_VLAN,
113 sizeof(struct rte_flow_action_of_push_vlan)),
114 MK_FLOW_ACTION(OF_SET_VLAN_VID,
115 sizeof(struct rte_flow_action_of_set_vlan_vid)),
116 MK_FLOW_ACTION(OF_SET_VLAN_PCP,
117 sizeof(struct rte_flow_action_of_set_vlan_pcp)),
118 MK_FLOW_ACTION(OF_POP_MPLS,
119 sizeof(struct rte_flow_action_of_pop_mpls)),
120 MK_FLOW_ACTION(OF_PUSH_MPLS,
121 sizeof(struct rte_flow_action_of_push_mpls)),
122 MK_FLOW_ACTION(VXLAN_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
123 MK_FLOW_ACTION(VXLAN_DECAP, 0),
124 MK_FLOW_ACTION(NVGRE_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
125 MK_FLOW_ACTION(NVGRE_DECAP, 0),
126 MK_FLOW_ACTION(SET_IPV4_SRC,
127 sizeof(struct rte_flow_action_set_ipv4)),
128 MK_FLOW_ACTION(SET_IPV4_DST,
129 sizeof(struct rte_flow_action_set_ipv4)),
130 MK_FLOW_ACTION(SET_IPV6_SRC,
131 sizeof(struct rte_flow_action_set_ipv6)),
132 MK_FLOW_ACTION(SET_IPV6_DST,
133 sizeof(struct rte_flow_action_set_ipv6)),
137 flow_err(uint16_t port_id, int ret, struct rte_flow_error *error)
141 if (rte_eth_dev_is_removed(port_id))
142 return rte_flow_error_set(error, EIO,
143 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
144 NULL, rte_strerror(EIO));
148 /* Get generic flow operations structure from a port. */
149 const struct rte_flow_ops *
150 rte_flow_ops_get(uint16_t port_id, struct rte_flow_error *error)
152 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
153 const struct rte_flow_ops *ops;
156 if (unlikely(!rte_eth_dev_is_valid_port(port_id)))
158 else if (unlikely(!dev->dev_ops->filter_ctrl ||
159 dev->dev_ops->filter_ctrl(dev,
160 RTE_ETH_FILTER_GENERIC,
167 rte_flow_error_set(error, code, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
168 NULL, rte_strerror(code));
172 /* Check whether a flow rule can be created on a given port. */
174 rte_flow_validate(uint16_t port_id,
175 const struct rte_flow_attr *attr,
176 const struct rte_flow_item pattern[],
177 const struct rte_flow_action actions[],
178 struct rte_flow_error *error)
180 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
181 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
185 if (likely(!!ops->validate))
186 return flow_err(port_id, ops->validate(dev, attr, pattern,
187 actions, error), error);
188 return rte_flow_error_set(error, ENOSYS,
189 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
190 NULL, rte_strerror(ENOSYS));
193 /* Create a flow rule on a given port. */
195 rte_flow_create(uint16_t port_id,
196 const struct rte_flow_attr *attr,
197 const struct rte_flow_item pattern[],
198 const struct rte_flow_action actions[],
199 struct rte_flow_error *error)
201 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
202 struct rte_flow *flow;
203 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
207 if (likely(!!ops->create)) {
208 flow = ops->create(dev, attr, pattern, actions, error);
210 flow_err(port_id, -rte_errno, error);
213 rte_flow_error_set(error, ENOSYS, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
214 NULL, rte_strerror(ENOSYS));
218 /* Destroy a flow rule on a given port. */
220 rte_flow_destroy(uint16_t port_id,
221 struct rte_flow *flow,
222 struct rte_flow_error *error)
224 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
225 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
229 if (likely(!!ops->destroy))
230 return flow_err(port_id, ops->destroy(dev, flow, error),
232 return rte_flow_error_set(error, ENOSYS,
233 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
234 NULL, rte_strerror(ENOSYS));
237 /* Destroy all flow rules associated with a port. */
239 rte_flow_flush(uint16_t port_id,
240 struct rte_flow_error *error)
242 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
243 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
247 if (likely(!!ops->flush))
248 return flow_err(port_id, ops->flush(dev, error), error);
249 return rte_flow_error_set(error, ENOSYS,
250 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
251 NULL, rte_strerror(ENOSYS));
254 /* Query an existing flow rule. */
256 rte_flow_query(uint16_t port_id,
257 struct rte_flow *flow,
258 const struct rte_flow_action *action,
260 struct rte_flow_error *error)
262 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
263 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
267 if (likely(!!ops->query))
268 return flow_err(port_id, ops->query(dev, flow, action, data,
270 return rte_flow_error_set(error, ENOSYS,
271 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
272 NULL, rte_strerror(ENOSYS));
275 /* Restrict ingress traffic to the defined flow rules. */
277 rte_flow_isolate(uint16_t port_id,
279 struct rte_flow_error *error)
281 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
282 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
286 if (likely(!!ops->isolate))
287 return flow_err(port_id, ops->isolate(dev, set, error), error);
288 return rte_flow_error_set(error, ENOSYS,
289 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
290 NULL, rte_strerror(ENOSYS));
293 /* Initialize flow error structure. */
295 rte_flow_error_set(struct rte_flow_error *error,
297 enum rte_flow_error_type type,
302 *error = (struct rte_flow_error){
312 /** Pattern item specification types. */
313 enum rte_flow_conv_item_spec_type {
314 RTE_FLOW_CONV_ITEM_SPEC,
315 RTE_FLOW_CONV_ITEM_LAST,
316 RTE_FLOW_CONV_ITEM_MASK,
320 * Copy pattern item specification.
323 * Output buffer. Can be NULL if @p size is zero.
325 * Size of @p buf in bytes.
327 * Pattern item to copy specification from.
329 * Specification selector for either @p spec, @p last or @p mask.
332 * Number of bytes needed to store pattern item specification regardless
333 * of @p size. @p buf contents are truncated to @p size if not large
337 rte_flow_conv_item_spec(void *buf, const size_t size,
338 const struct rte_flow_item *item,
339 enum rte_flow_conv_item_spec_type type)
343 type == RTE_FLOW_CONV_ITEM_SPEC ? item->spec :
344 type == RTE_FLOW_CONV_ITEM_LAST ? item->last :
345 type == RTE_FLOW_CONV_ITEM_MASK ? item->mask :
348 switch (item->type) {
350 const struct rte_flow_item_raw *raw;
353 const struct rte_flow_item_raw *raw;
356 const struct rte_flow_item_raw *raw;
359 const struct rte_flow_item_raw *raw;
362 struct rte_flow_item_raw *raw;
366 case RTE_FLOW_ITEM_TYPE_RAW:
367 spec.raw = item->spec;
368 last.raw = item->last ? item->last : item->spec;
369 mask.raw = item->mask ? item->mask : &rte_flow_item_raw_mask;
373 (&(struct rte_flow_item_raw){
374 .relative = src.raw->relative,
375 .search = src.raw->search,
376 .reserved = src.raw->reserved,
377 .offset = src.raw->offset,
378 .limit = src.raw->limit,
379 .length = src.raw->length,
381 size > sizeof(*dst.raw) ? sizeof(*dst.raw) : size);
382 off = sizeof(*dst.raw);
383 if (type == RTE_FLOW_CONV_ITEM_SPEC ||
384 (type == RTE_FLOW_CONV_ITEM_MASK &&
385 ((spec.raw->length & mask.raw->length) >=
386 (last.raw->length & mask.raw->length))))
387 tmp = spec.raw->length & mask.raw->length;
389 tmp = last.raw->length & mask.raw->length;
391 off = RTE_ALIGN_CEIL(off, sizeof(*dst.raw->pattern));
392 if (size >= off + tmp)
393 dst.raw->pattern = rte_memcpy
394 ((void *)((uintptr_t)dst.raw + off),
395 src.raw->pattern, tmp);
400 off = rte_flow_desc_item[item->type].size;
401 rte_memcpy(buf, data, (size > off ? off : size));
408 * Copy action configuration.
411 * Output buffer. Can be NULL if @p size is zero.
413 * Size of @p buf in bytes.
415 * Action to copy configuration from.
418 * Number of bytes needed to store pattern item specification regardless
419 * of @p size. @p buf contents are truncated to @p size if not large
423 rte_flow_conv_action_conf(void *buf, const size_t size,
424 const struct rte_flow_action *action)
428 switch (action->type) {
430 const struct rte_flow_action_rss *rss;
431 const struct rte_flow_action_vxlan_encap *vxlan_encap;
432 const struct rte_flow_action_nvgre_encap *nvgre_encap;
435 struct rte_flow_action_rss *rss;
436 struct rte_flow_action_vxlan_encap *vxlan_encap;
437 struct rte_flow_action_nvgre_encap *nvgre_encap;
442 case RTE_FLOW_ACTION_TYPE_RSS:
443 src.rss = action->conf;
446 (&(struct rte_flow_action_rss){
447 .func = src.rss->func,
448 .level = src.rss->level,
449 .types = src.rss->types,
450 .key_len = src.rss->key_len,
451 .queue_num = src.rss->queue_num,
453 size > sizeof(*dst.rss) ? sizeof(*dst.rss) : size);
454 off = sizeof(*dst.rss);
455 if (src.rss->key_len) {
456 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->key));
457 tmp = sizeof(*src.rss->key) * src.rss->key_len;
458 if (size >= off + tmp)
459 dst.rss->key = rte_memcpy
460 ((void *)((uintptr_t)dst.rss + off),
464 if (src.rss->queue_num) {
465 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->queue));
466 tmp = sizeof(*src.rss->queue) * src.rss->queue_num;
467 if (size >= off + tmp)
468 dst.rss->queue = rte_memcpy
469 ((void *)((uintptr_t)dst.rss + off),
470 src.rss->queue, tmp);
474 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
475 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
476 src.vxlan_encap = action->conf;
477 dst.vxlan_encap = buf;
478 RTE_BUILD_BUG_ON(sizeof(*src.vxlan_encap) !=
479 sizeof(*src.nvgre_encap) ||
480 offsetof(struct rte_flow_action_vxlan_encap,
482 offsetof(struct rte_flow_action_nvgre_encap,
484 off = sizeof(*dst.vxlan_encap);
485 if (src.vxlan_encap->definition) {
487 (off, sizeof(*dst.vxlan_encap->definition));
489 (RTE_FLOW_CONV_OP_PATTERN,
490 (void *)((uintptr_t)dst.vxlan_encap + off),
491 size > off ? size - off : 0,
492 src.vxlan_encap->definition, NULL);
495 if (size >= off + ret)
496 dst.vxlan_encap->definition =
497 (void *)((uintptr_t)dst.vxlan_encap +
503 off = rte_flow_desc_action[action->type].size;
504 rte_memcpy(buf, action->conf, (size > off ? off : size));
511 * Copy a list of pattern items.
514 * Destination buffer. Can be NULL if @p size is zero.
516 * Size of @p dst in bytes.
518 * Source pattern items.
520 * Maximum number of pattern items to process from @p src or 0 to process
521 * the entire list. In both cases, processing stops after
522 * RTE_FLOW_ITEM_TYPE_END is encountered.
524 * Perform verbose error reporting if not NULL.
527 * A positive value representing the number of bytes needed to store
528 * pattern items regardless of @p size on success (@p buf contents are
529 * truncated to @p size if not large enough), a negative errno value
530 * otherwise and rte_errno is set.
533 rte_flow_conv_pattern(struct rte_flow_item *dst,
535 const struct rte_flow_item *src,
537 struct rte_flow_error *error)
539 uintptr_t data = (uintptr_t)dst;
544 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
545 if ((size_t)src->type >= RTE_DIM(rte_flow_desc_item) ||
546 !rte_flow_desc_item[src->type].name)
547 return rte_flow_error_set
548 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, src,
549 "cannot convert unknown item type");
550 if (size >= off + sizeof(*dst))
551 *dst = (struct rte_flow_item){
563 off = RTE_ALIGN_CEIL(off, sizeof(double));
564 ret = rte_flow_conv_item_spec
565 ((void *)(data + off),
566 size > off ? size - off : 0, src,
567 RTE_FLOW_CONV_ITEM_SPEC);
568 if (size && size >= off + ret)
569 dst->spec = (void *)(data + off);
574 off = RTE_ALIGN_CEIL(off, sizeof(double));
575 ret = rte_flow_conv_item_spec
576 ((void *)(data + off),
577 size > off ? size - off : 0, src,
578 RTE_FLOW_CONV_ITEM_LAST);
579 if (size && size >= off + ret)
580 dst->last = (void *)(data + off);
584 off = RTE_ALIGN_CEIL(off, sizeof(double));
585 ret = rte_flow_conv_item_spec
586 ((void *)(data + off),
587 size > off ? size - off : 0, src,
588 RTE_FLOW_CONV_ITEM_MASK);
589 if (size && size >= off + ret)
590 dst->mask = (void *)(data + off);
600 * Copy a list of actions.
603 * Destination buffer. Can be NULL if @p size is zero.
605 * Size of @p dst in bytes.
609 * Maximum number of actions to process from @p src or 0 to process the
610 * entire list. In both cases, processing stops after
611 * RTE_FLOW_ACTION_TYPE_END is encountered.
613 * Perform verbose error reporting if not NULL.
616 * A positive value representing the number of bytes needed to store
617 * actions regardless of @p size on success (@p buf contents are truncated
618 * to @p size if not large enough), a negative errno value otherwise and
622 rte_flow_conv_actions(struct rte_flow_action *dst,
624 const struct rte_flow_action *src,
626 struct rte_flow_error *error)
628 uintptr_t data = (uintptr_t)dst;
633 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
634 if ((size_t)src->type >= RTE_DIM(rte_flow_desc_action) ||
635 !rte_flow_desc_action[src->type].name)
636 return rte_flow_error_set
637 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
638 src, "cannot convert unknown action type");
639 if (size >= off + sizeof(*dst))
640 *dst = (struct rte_flow_action){
652 off = RTE_ALIGN_CEIL(off, sizeof(double));
653 ret = rte_flow_conv_action_conf
654 ((void *)(data + off),
655 size > off ? size - off : 0, src);
656 if (size && size >= off + ret)
657 dst->conf = (void *)(data + off);
667 * Copy flow rule components.
669 * This comprises the flow rule descriptor itself, attributes, pattern and
670 * actions list. NULL components in @p src are skipped.
673 * Destination buffer. Can be NULL if @p size is zero.
675 * Size of @p dst in bytes.
677 * Source flow rule descriptor.
679 * Perform verbose error reporting if not NULL.
682 * A positive value representing the number of bytes needed to store all
683 * components including the descriptor regardless of @p size on success
684 * (@p buf contents are truncated to @p size if not large enough), a
685 * negative errno value otherwise and rte_errno is set.
688 rte_flow_conv_rule(struct rte_flow_conv_rule *dst,
690 const struct rte_flow_conv_rule *src,
691 struct rte_flow_error *error)
697 (&(struct rte_flow_conv_rule){
702 size > sizeof(*dst) ? sizeof(*dst) : size);
705 off = RTE_ALIGN_CEIL(off, sizeof(double));
706 if (size && size >= off + sizeof(*dst->attr))
707 dst->attr = rte_memcpy
708 ((void *)((uintptr_t)dst + off),
709 src->attr_ro, sizeof(*dst->attr));
710 off += sizeof(*dst->attr);
712 if (src->pattern_ro) {
713 off = RTE_ALIGN_CEIL(off, sizeof(double));
714 ret = rte_flow_conv_pattern((void *)((uintptr_t)dst + off),
715 size > off ? size - off : 0,
716 src->pattern_ro, 0, error);
719 if (size && size >= off + (size_t)ret)
720 dst->pattern = (void *)((uintptr_t)dst + off);
723 if (src->actions_ro) {
724 off = RTE_ALIGN_CEIL(off, sizeof(double));
725 ret = rte_flow_conv_actions((void *)((uintptr_t)dst + off),
726 size > off ? size - off : 0,
727 src->actions_ro, 0, error);
730 if (size >= off + (size_t)ret)
731 dst->actions = (void *)((uintptr_t)dst + off);
738 * Retrieve the name of a pattern item/action type.
741 * Nonzero when @p src represents an action type instead of a pattern item
744 * Nonzero to write string address instead of contents into @p dst.
746 * Destination buffer. Can be NULL if @p size is zero.
748 * Size of @p dst in bytes.
750 * Depending on @p is_action, source pattern item or action type cast as a
753 * Perform verbose error reporting if not NULL.
756 * A positive value representing the number of bytes needed to store the
757 * name or its address regardless of @p size on success (@p buf contents
758 * are truncated to @p size if not large enough), a negative errno value
759 * otherwise and rte_errno is set.
762 rte_flow_conv_name(int is_action,
767 struct rte_flow_error *error)
770 const struct rte_flow_desc_data *data;
773 static const struct desc_info info_rep[2] = {
774 { rte_flow_desc_item, RTE_DIM(rte_flow_desc_item), },
775 { rte_flow_desc_action, RTE_DIM(rte_flow_desc_action), },
777 const struct desc_info *const info = &info_rep[!!is_action];
778 unsigned int type = (uintptr_t)src;
780 if (type >= info->num)
781 return rte_flow_error_set
782 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
783 "unknown object type to retrieve the name of");
785 return strlcpy(dst, info->data[type].name, size);
786 if (size >= sizeof(const char **))
787 *((const char **)dst) = info->data[type].name;
788 return sizeof(const char **);
791 /** Helper function to convert flow API objects. */
793 rte_flow_conv(enum rte_flow_conv_op op,
797 struct rte_flow_error *error)
800 const struct rte_flow_attr *attr;
802 case RTE_FLOW_CONV_OP_NONE:
804 case RTE_FLOW_CONV_OP_ATTR:
806 if (size > sizeof(*attr))
807 size = sizeof(*attr);
808 rte_memcpy(dst, attr, size);
809 return sizeof(*attr);
810 case RTE_FLOW_CONV_OP_ITEM:
811 return rte_flow_conv_pattern(dst, size, src, 1, error);
812 case RTE_FLOW_CONV_OP_ACTION:
813 return rte_flow_conv_actions(dst, size, src, 1, error);
814 case RTE_FLOW_CONV_OP_PATTERN:
815 return rte_flow_conv_pattern(dst, size, src, 0, error);
816 case RTE_FLOW_CONV_OP_ACTIONS:
817 return rte_flow_conv_actions(dst, size, src, 0, error);
818 case RTE_FLOW_CONV_OP_RULE:
819 return rte_flow_conv_rule(dst, size, src, error);
820 case RTE_FLOW_CONV_OP_ITEM_NAME:
821 return rte_flow_conv_name(0, 0, dst, size, src, error);
822 case RTE_FLOW_CONV_OP_ACTION_NAME:
823 return rte_flow_conv_name(1, 0, dst, size, src, error);
824 case RTE_FLOW_CONV_OP_ITEM_NAME_PTR:
825 return rte_flow_conv_name(0, 1, dst, size, src, error);
826 case RTE_FLOW_CONV_OP_ACTION_NAME_PTR:
827 return rte_flow_conv_name(1, 1, dst, size, src, error);
829 return rte_flow_error_set
830 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
831 "unknown object conversion operation");
834 /** Store a full rte_flow description. */
836 rte_flow_copy(struct rte_flow_desc *desc, size_t len,
837 const struct rte_flow_attr *attr,
838 const struct rte_flow_item *items,
839 const struct rte_flow_action *actions)
842 * Overlap struct rte_flow_conv with struct rte_flow_desc in order
843 * to convert the former to the latter without wasting space.
845 struct rte_flow_conv_rule *dst =
847 (void *)((uintptr_t)desc +
848 (offsetof(struct rte_flow_desc, actions) -
849 offsetof(struct rte_flow_conv_rule, actions))) :
852 len > sizeof(*desc) - sizeof(*dst) ?
853 len - (sizeof(*desc) - sizeof(*dst)) :
855 struct rte_flow_conv_rule src = {
858 .actions_ro = actions,
862 RTE_BUILD_BUG_ON(sizeof(struct rte_flow_desc) <
863 sizeof(struct rte_flow_conv_rule));
865 (&dst->pattern != &desc->items ||
866 &dst->actions != &desc->actions ||
867 (uintptr_t)(dst + 1) != (uintptr_t)(desc + 1))) {
871 ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, dst, dst_size, &src, NULL);
874 ret += sizeof(*desc) - sizeof(*dst);
876 (&(struct rte_flow_desc){
879 .items = dst_size ? dst->pattern : NULL,
880 .actions = dst_size ? dst->actions : NULL,
882 len > sizeof(*desc) ? sizeof(*desc) : len);
887 * Expand RSS flows into several possible flows according to the RSS hash
888 * fields requested and the driver capabilities.
890 int __rte_experimental
891 rte_flow_expand_rss(struct rte_flow_expand_rss *buf, size_t size,
892 const struct rte_flow_item *pattern, uint64_t types,
893 const struct rte_flow_expand_node graph[],
894 int graph_root_index)
897 const struct rte_flow_item *item;
898 const struct rte_flow_expand_node *node = &graph[graph_root_index];
899 const int *next_node;
900 const int *stack[elt_n];
902 struct rte_flow_item flow_items[elt_n];
905 size_t user_pattern_size = 0;
908 lsize = offsetof(struct rte_flow_expand_rss, entry) +
909 elt_n * sizeof(buf->entry[0]);
911 buf->entry[0].priority = 0;
912 buf->entry[0].pattern = (void *)&buf->entry[elt_n];
914 addr = buf->entry[0].pattern;
916 for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
917 const struct rte_flow_expand_node *next = NULL;
919 for (i = 0; node->next && node->next[i]; ++i) {
920 next = &graph[node->next[i]];
921 if (next->type == item->type)
926 user_pattern_size += sizeof(*item);
928 user_pattern_size += sizeof(*item); /* Handle END item. */
929 lsize += user_pattern_size;
930 /* Copy the user pattern in the first entry of the buffer. */
932 rte_memcpy(addr, pattern, user_pattern_size);
933 addr = (void *)(((uintptr_t)addr) + user_pattern_size);
936 /* Start expanding. */
937 memset(flow_items, 0, sizeof(flow_items));
938 user_pattern_size -= sizeof(*item);
939 next_node = node->next;
940 stack[stack_pos] = next_node;
941 node = next_node ? &graph[*next_node] : NULL;
943 flow_items[stack_pos].type = node->type;
944 if (node->rss_types & types) {
946 * compute the number of items to copy from the
947 * expansion and copy it.
948 * When the stack_pos is 0, there are 1 element in it,
949 * plus the addition END item.
951 int elt = stack_pos + 2;
953 flow_items[stack_pos + 1].type = RTE_FLOW_ITEM_TYPE_END;
954 lsize += elt * sizeof(*item) + user_pattern_size;
956 size_t n = elt * sizeof(*item);
958 buf->entry[buf->entries].priority =
960 buf->entry[buf->entries].pattern = addr;
962 rte_memcpy(addr, buf->entry[0].pattern,
964 addr = (void *)(((uintptr_t)addr) +
966 rte_memcpy(addr, flow_items, n);
967 addr = (void *)(((uintptr_t)addr) + n);
972 next_node = node->next;
973 if (stack_pos++ == elt_n) {
977 stack[stack_pos] = next_node;
978 } else if (*(next_node + 1)) {
979 /* Follow up with the next possibility. */
982 /* Move to the next path. */
984 next_node = stack[--stack_pos];
986 stack[stack_pos] = next_node;
988 node = *next_node ? &graph[*next_node] : NULL;