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)),
134 MK_FLOW_ACTION(SET_TP_SRC,
135 sizeof(struct rte_flow_action_set_tp)),
136 MK_FLOW_ACTION(SET_TP_DST,
137 sizeof(struct rte_flow_action_set_tp)),
141 flow_err(uint16_t port_id, int ret, struct rte_flow_error *error)
145 if (rte_eth_dev_is_removed(port_id))
146 return rte_flow_error_set(error, EIO,
147 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
148 NULL, rte_strerror(EIO));
152 /* Get generic flow operations structure from a port. */
153 const struct rte_flow_ops *
154 rte_flow_ops_get(uint16_t port_id, struct rte_flow_error *error)
156 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
157 const struct rte_flow_ops *ops;
160 if (unlikely(!rte_eth_dev_is_valid_port(port_id)))
162 else if (unlikely(!dev->dev_ops->filter_ctrl ||
163 dev->dev_ops->filter_ctrl(dev,
164 RTE_ETH_FILTER_GENERIC,
171 rte_flow_error_set(error, code, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
172 NULL, rte_strerror(code));
176 /* Check whether a flow rule can be created on a given port. */
178 rte_flow_validate(uint16_t port_id,
179 const struct rte_flow_attr *attr,
180 const struct rte_flow_item pattern[],
181 const struct rte_flow_action actions[],
182 struct rte_flow_error *error)
184 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
185 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
189 if (likely(!!ops->validate))
190 return flow_err(port_id, ops->validate(dev, attr, pattern,
191 actions, error), error);
192 return rte_flow_error_set(error, ENOSYS,
193 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
194 NULL, rte_strerror(ENOSYS));
197 /* Create a flow rule on a given port. */
199 rte_flow_create(uint16_t port_id,
200 const struct rte_flow_attr *attr,
201 const struct rte_flow_item pattern[],
202 const struct rte_flow_action actions[],
203 struct rte_flow_error *error)
205 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
206 struct rte_flow *flow;
207 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
211 if (likely(!!ops->create)) {
212 flow = ops->create(dev, attr, pattern, actions, error);
214 flow_err(port_id, -rte_errno, error);
217 rte_flow_error_set(error, ENOSYS, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
218 NULL, rte_strerror(ENOSYS));
222 /* Destroy a flow rule on a given port. */
224 rte_flow_destroy(uint16_t port_id,
225 struct rte_flow *flow,
226 struct rte_flow_error *error)
228 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
229 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
233 if (likely(!!ops->destroy))
234 return flow_err(port_id, ops->destroy(dev, flow, error),
236 return rte_flow_error_set(error, ENOSYS,
237 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
238 NULL, rte_strerror(ENOSYS));
241 /* Destroy all flow rules associated with a port. */
243 rte_flow_flush(uint16_t port_id,
244 struct rte_flow_error *error)
246 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
247 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
251 if (likely(!!ops->flush))
252 return flow_err(port_id, ops->flush(dev, error), error);
253 return rte_flow_error_set(error, ENOSYS,
254 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
255 NULL, rte_strerror(ENOSYS));
258 /* Query an existing flow rule. */
260 rte_flow_query(uint16_t port_id,
261 struct rte_flow *flow,
262 const struct rte_flow_action *action,
264 struct rte_flow_error *error)
266 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
267 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
271 if (likely(!!ops->query))
272 return flow_err(port_id, ops->query(dev, flow, action, data,
274 return rte_flow_error_set(error, ENOSYS,
275 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
276 NULL, rte_strerror(ENOSYS));
279 /* Restrict ingress traffic to the defined flow rules. */
281 rte_flow_isolate(uint16_t port_id,
283 struct rte_flow_error *error)
285 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
286 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
290 if (likely(!!ops->isolate))
291 return flow_err(port_id, ops->isolate(dev, set, error), error);
292 return rte_flow_error_set(error, ENOSYS,
293 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
294 NULL, rte_strerror(ENOSYS));
297 /* Initialize flow error structure. */
299 rte_flow_error_set(struct rte_flow_error *error,
301 enum rte_flow_error_type type,
306 *error = (struct rte_flow_error){
316 /** Pattern item specification types. */
317 enum rte_flow_conv_item_spec_type {
318 RTE_FLOW_CONV_ITEM_SPEC,
319 RTE_FLOW_CONV_ITEM_LAST,
320 RTE_FLOW_CONV_ITEM_MASK,
324 * Copy pattern item specification.
327 * Output buffer. Can be NULL if @p size is zero.
329 * Size of @p buf in bytes.
331 * Pattern item to copy specification from.
333 * Specification selector for either @p spec, @p last or @p mask.
336 * Number of bytes needed to store pattern item specification regardless
337 * of @p size. @p buf contents are truncated to @p size if not large
341 rte_flow_conv_item_spec(void *buf, const size_t size,
342 const struct rte_flow_item *item,
343 enum rte_flow_conv_item_spec_type type)
347 type == RTE_FLOW_CONV_ITEM_SPEC ? item->spec :
348 type == RTE_FLOW_CONV_ITEM_LAST ? item->last :
349 type == RTE_FLOW_CONV_ITEM_MASK ? item->mask :
352 switch (item->type) {
354 const struct rte_flow_item_raw *raw;
357 const struct rte_flow_item_raw *raw;
360 const struct rte_flow_item_raw *raw;
363 const struct rte_flow_item_raw *raw;
366 struct rte_flow_item_raw *raw;
370 case RTE_FLOW_ITEM_TYPE_RAW:
371 spec.raw = item->spec;
372 last.raw = item->last ? item->last : item->spec;
373 mask.raw = item->mask ? item->mask : &rte_flow_item_raw_mask;
377 (&(struct rte_flow_item_raw){
378 .relative = src.raw->relative,
379 .search = src.raw->search,
380 .reserved = src.raw->reserved,
381 .offset = src.raw->offset,
382 .limit = src.raw->limit,
383 .length = src.raw->length,
385 size > sizeof(*dst.raw) ? sizeof(*dst.raw) : size);
386 off = sizeof(*dst.raw);
387 if (type == RTE_FLOW_CONV_ITEM_SPEC ||
388 (type == RTE_FLOW_CONV_ITEM_MASK &&
389 ((spec.raw->length & mask.raw->length) >=
390 (last.raw->length & mask.raw->length))))
391 tmp = spec.raw->length & mask.raw->length;
393 tmp = last.raw->length & mask.raw->length;
395 off = RTE_ALIGN_CEIL(off, sizeof(*dst.raw->pattern));
396 if (size >= off + tmp)
397 dst.raw->pattern = rte_memcpy
398 ((void *)((uintptr_t)dst.raw + off),
399 src.raw->pattern, tmp);
404 off = rte_flow_desc_item[item->type].size;
405 rte_memcpy(buf, data, (size > off ? off : size));
412 * Copy action configuration.
415 * Output buffer. Can be NULL if @p size is zero.
417 * Size of @p buf in bytes.
419 * Action to copy configuration from.
422 * Number of bytes needed to store pattern item specification regardless
423 * of @p size. @p buf contents are truncated to @p size if not large
427 rte_flow_conv_action_conf(void *buf, const size_t size,
428 const struct rte_flow_action *action)
432 switch (action->type) {
434 const struct rte_flow_action_rss *rss;
435 const struct rte_flow_action_vxlan_encap *vxlan_encap;
436 const struct rte_flow_action_nvgre_encap *nvgre_encap;
439 struct rte_flow_action_rss *rss;
440 struct rte_flow_action_vxlan_encap *vxlan_encap;
441 struct rte_flow_action_nvgre_encap *nvgre_encap;
446 case RTE_FLOW_ACTION_TYPE_RSS:
447 src.rss = action->conf;
450 (&(struct rte_flow_action_rss){
451 .func = src.rss->func,
452 .level = src.rss->level,
453 .types = src.rss->types,
454 .key_len = src.rss->key_len,
455 .queue_num = src.rss->queue_num,
457 size > sizeof(*dst.rss) ? sizeof(*dst.rss) : size);
458 off = sizeof(*dst.rss);
459 if (src.rss->key_len) {
460 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->key));
461 tmp = sizeof(*src.rss->key) * src.rss->key_len;
462 if (size >= off + tmp)
463 dst.rss->key = rte_memcpy
464 ((void *)((uintptr_t)dst.rss + off),
468 if (src.rss->queue_num) {
469 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->queue));
470 tmp = sizeof(*src.rss->queue) * src.rss->queue_num;
471 if (size >= off + tmp)
472 dst.rss->queue = rte_memcpy
473 ((void *)((uintptr_t)dst.rss + off),
474 src.rss->queue, tmp);
478 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
479 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
480 src.vxlan_encap = action->conf;
481 dst.vxlan_encap = buf;
482 RTE_BUILD_BUG_ON(sizeof(*src.vxlan_encap) !=
483 sizeof(*src.nvgre_encap) ||
484 offsetof(struct rte_flow_action_vxlan_encap,
486 offsetof(struct rte_flow_action_nvgre_encap,
488 off = sizeof(*dst.vxlan_encap);
489 if (src.vxlan_encap->definition) {
491 (off, sizeof(*dst.vxlan_encap->definition));
493 (RTE_FLOW_CONV_OP_PATTERN,
494 (void *)((uintptr_t)dst.vxlan_encap + off),
495 size > off ? size - off : 0,
496 src.vxlan_encap->definition, NULL);
499 if (size >= off + ret)
500 dst.vxlan_encap->definition =
501 (void *)((uintptr_t)dst.vxlan_encap +
507 off = rte_flow_desc_action[action->type].size;
508 rte_memcpy(buf, action->conf, (size > off ? off : size));
515 * Copy a list of pattern items.
518 * Destination buffer. Can be NULL if @p size is zero.
520 * Size of @p dst in bytes.
522 * Source pattern items.
524 * Maximum number of pattern items to process from @p src or 0 to process
525 * the entire list. In both cases, processing stops after
526 * RTE_FLOW_ITEM_TYPE_END is encountered.
528 * Perform verbose error reporting if not NULL.
531 * A positive value representing the number of bytes needed to store
532 * pattern items regardless of @p size on success (@p buf contents are
533 * truncated to @p size if not large enough), a negative errno value
534 * otherwise and rte_errno is set.
537 rte_flow_conv_pattern(struct rte_flow_item *dst,
539 const struct rte_flow_item *src,
541 struct rte_flow_error *error)
543 uintptr_t data = (uintptr_t)dst;
548 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
549 if ((size_t)src->type >= RTE_DIM(rte_flow_desc_item) ||
550 !rte_flow_desc_item[src->type].name)
551 return rte_flow_error_set
552 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, src,
553 "cannot convert unknown item type");
554 if (size >= off + sizeof(*dst))
555 *dst = (struct rte_flow_item){
567 off = RTE_ALIGN_CEIL(off, sizeof(double));
568 ret = rte_flow_conv_item_spec
569 ((void *)(data + off),
570 size > off ? size - off : 0, src,
571 RTE_FLOW_CONV_ITEM_SPEC);
572 if (size && size >= off + ret)
573 dst->spec = (void *)(data + off);
578 off = RTE_ALIGN_CEIL(off, sizeof(double));
579 ret = rte_flow_conv_item_spec
580 ((void *)(data + off),
581 size > off ? size - off : 0, src,
582 RTE_FLOW_CONV_ITEM_LAST);
583 if (size && size >= off + ret)
584 dst->last = (void *)(data + off);
588 off = RTE_ALIGN_CEIL(off, sizeof(double));
589 ret = rte_flow_conv_item_spec
590 ((void *)(data + off),
591 size > off ? size - off : 0, src,
592 RTE_FLOW_CONV_ITEM_MASK);
593 if (size && size >= off + ret)
594 dst->mask = (void *)(data + off);
604 * Copy a list of actions.
607 * Destination buffer. Can be NULL if @p size is zero.
609 * Size of @p dst in bytes.
613 * Maximum number of actions to process from @p src or 0 to process the
614 * entire list. In both cases, processing stops after
615 * RTE_FLOW_ACTION_TYPE_END is encountered.
617 * Perform verbose error reporting if not NULL.
620 * A positive value representing the number of bytes needed to store
621 * actions regardless of @p size on success (@p buf contents are truncated
622 * to @p size if not large enough), a negative errno value otherwise and
626 rte_flow_conv_actions(struct rte_flow_action *dst,
628 const struct rte_flow_action *src,
630 struct rte_flow_error *error)
632 uintptr_t data = (uintptr_t)dst;
637 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
638 if ((size_t)src->type >= RTE_DIM(rte_flow_desc_action) ||
639 !rte_flow_desc_action[src->type].name)
640 return rte_flow_error_set
641 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
642 src, "cannot convert unknown action type");
643 if (size >= off + sizeof(*dst))
644 *dst = (struct rte_flow_action){
656 off = RTE_ALIGN_CEIL(off, sizeof(double));
657 ret = rte_flow_conv_action_conf
658 ((void *)(data + off),
659 size > off ? size - off : 0, src);
660 if (size && size >= off + ret)
661 dst->conf = (void *)(data + off);
671 * Copy flow rule components.
673 * This comprises the flow rule descriptor itself, attributes, pattern and
674 * actions list. NULL components in @p src are skipped.
677 * Destination buffer. Can be NULL if @p size is zero.
679 * Size of @p dst in bytes.
681 * Source flow rule descriptor.
683 * Perform verbose error reporting if not NULL.
686 * A positive value representing the number of bytes needed to store all
687 * components including the descriptor regardless of @p size on success
688 * (@p buf contents are truncated to @p size if not large enough), a
689 * negative errno value otherwise and rte_errno is set.
692 rte_flow_conv_rule(struct rte_flow_conv_rule *dst,
694 const struct rte_flow_conv_rule *src,
695 struct rte_flow_error *error)
701 (&(struct rte_flow_conv_rule){
706 size > sizeof(*dst) ? sizeof(*dst) : size);
709 off = RTE_ALIGN_CEIL(off, sizeof(double));
710 if (size && size >= off + sizeof(*dst->attr))
711 dst->attr = rte_memcpy
712 ((void *)((uintptr_t)dst + off),
713 src->attr_ro, sizeof(*dst->attr));
714 off += sizeof(*dst->attr);
716 if (src->pattern_ro) {
717 off = RTE_ALIGN_CEIL(off, sizeof(double));
718 ret = rte_flow_conv_pattern((void *)((uintptr_t)dst + off),
719 size > off ? size - off : 0,
720 src->pattern_ro, 0, error);
723 if (size && size >= off + (size_t)ret)
724 dst->pattern = (void *)((uintptr_t)dst + off);
727 if (src->actions_ro) {
728 off = RTE_ALIGN_CEIL(off, sizeof(double));
729 ret = rte_flow_conv_actions((void *)((uintptr_t)dst + off),
730 size > off ? size - off : 0,
731 src->actions_ro, 0, error);
734 if (size >= off + (size_t)ret)
735 dst->actions = (void *)((uintptr_t)dst + off);
742 * Retrieve the name of a pattern item/action type.
745 * Nonzero when @p src represents an action type instead of a pattern item
748 * Nonzero to write string address instead of contents into @p dst.
750 * Destination buffer. Can be NULL if @p size is zero.
752 * Size of @p dst in bytes.
754 * Depending on @p is_action, source pattern item or action type cast as a
757 * Perform verbose error reporting if not NULL.
760 * A positive value representing the number of bytes needed to store the
761 * name or its address regardless of @p size on success (@p buf contents
762 * are truncated to @p size if not large enough), a negative errno value
763 * otherwise and rte_errno is set.
766 rte_flow_conv_name(int is_action,
771 struct rte_flow_error *error)
774 const struct rte_flow_desc_data *data;
777 static const struct desc_info info_rep[2] = {
778 { rte_flow_desc_item, RTE_DIM(rte_flow_desc_item), },
779 { rte_flow_desc_action, RTE_DIM(rte_flow_desc_action), },
781 const struct desc_info *const info = &info_rep[!!is_action];
782 unsigned int type = (uintptr_t)src;
784 if (type >= info->num)
785 return rte_flow_error_set
786 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
787 "unknown object type to retrieve the name of");
789 return strlcpy(dst, info->data[type].name, size);
790 if (size >= sizeof(const char **))
791 *((const char **)dst) = info->data[type].name;
792 return sizeof(const char **);
795 /** Helper function to convert flow API objects. */
797 rte_flow_conv(enum rte_flow_conv_op op,
801 struct rte_flow_error *error)
804 const struct rte_flow_attr *attr;
806 case RTE_FLOW_CONV_OP_NONE:
808 case RTE_FLOW_CONV_OP_ATTR:
810 if (size > sizeof(*attr))
811 size = sizeof(*attr);
812 rte_memcpy(dst, attr, size);
813 return sizeof(*attr);
814 case RTE_FLOW_CONV_OP_ITEM:
815 return rte_flow_conv_pattern(dst, size, src, 1, error);
816 case RTE_FLOW_CONV_OP_ACTION:
817 return rte_flow_conv_actions(dst, size, src, 1, error);
818 case RTE_FLOW_CONV_OP_PATTERN:
819 return rte_flow_conv_pattern(dst, size, src, 0, error);
820 case RTE_FLOW_CONV_OP_ACTIONS:
821 return rte_flow_conv_actions(dst, size, src, 0, error);
822 case RTE_FLOW_CONV_OP_RULE:
823 return rte_flow_conv_rule(dst, size, src, error);
824 case RTE_FLOW_CONV_OP_ITEM_NAME:
825 return rte_flow_conv_name(0, 0, dst, size, src, error);
826 case RTE_FLOW_CONV_OP_ACTION_NAME:
827 return rte_flow_conv_name(1, 0, dst, size, src, error);
828 case RTE_FLOW_CONV_OP_ITEM_NAME_PTR:
829 return rte_flow_conv_name(0, 1, dst, size, src, error);
830 case RTE_FLOW_CONV_OP_ACTION_NAME_PTR:
831 return rte_flow_conv_name(1, 1, dst, size, src, error);
833 return rte_flow_error_set
834 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
835 "unknown object conversion operation");
838 /** Store a full rte_flow description. */
840 rte_flow_copy(struct rte_flow_desc *desc, size_t len,
841 const struct rte_flow_attr *attr,
842 const struct rte_flow_item *items,
843 const struct rte_flow_action *actions)
846 * Overlap struct rte_flow_conv with struct rte_flow_desc in order
847 * to convert the former to the latter without wasting space.
849 struct rte_flow_conv_rule *dst =
851 (void *)((uintptr_t)desc +
852 (offsetof(struct rte_flow_desc, actions) -
853 offsetof(struct rte_flow_conv_rule, actions))) :
856 len > sizeof(*desc) - sizeof(*dst) ?
857 len - (sizeof(*desc) - sizeof(*dst)) :
859 struct rte_flow_conv_rule src = {
862 .actions_ro = actions,
866 RTE_BUILD_BUG_ON(sizeof(struct rte_flow_desc) <
867 sizeof(struct rte_flow_conv_rule));
869 (&dst->pattern != &desc->items ||
870 &dst->actions != &desc->actions ||
871 (uintptr_t)(dst + 1) != (uintptr_t)(desc + 1))) {
875 ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, dst, dst_size, &src, NULL);
878 ret += sizeof(*desc) - sizeof(*dst);
880 (&(struct rte_flow_desc){
883 .items = dst_size ? dst->pattern : NULL,
884 .actions = dst_size ? dst->actions : NULL,
886 len > sizeof(*desc) ? sizeof(*desc) : len);
891 * Expand RSS flows into several possible flows according to the RSS hash
892 * fields requested and the driver capabilities.
894 int __rte_experimental
895 rte_flow_expand_rss(struct rte_flow_expand_rss *buf, size_t size,
896 const struct rte_flow_item *pattern, uint64_t types,
897 const struct rte_flow_expand_node graph[],
898 int graph_root_index)
901 const struct rte_flow_item *item;
902 const struct rte_flow_expand_node *node = &graph[graph_root_index];
903 const int *next_node;
904 const int *stack[elt_n];
906 struct rte_flow_item flow_items[elt_n];
909 size_t user_pattern_size = 0;
912 lsize = offsetof(struct rte_flow_expand_rss, entry) +
913 elt_n * sizeof(buf->entry[0]);
915 buf->entry[0].priority = 0;
916 buf->entry[0].pattern = (void *)&buf->entry[elt_n];
918 addr = buf->entry[0].pattern;
920 for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
921 const struct rte_flow_expand_node *next = NULL;
923 for (i = 0; node->next && node->next[i]; ++i) {
924 next = &graph[node->next[i]];
925 if (next->type == item->type)
930 user_pattern_size += sizeof(*item);
932 user_pattern_size += sizeof(*item); /* Handle END item. */
933 lsize += user_pattern_size;
934 /* Copy the user pattern in the first entry of the buffer. */
936 rte_memcpy(addr, pattern, user_pattern_size);
937 addr = (void *)(((uintptr_t)addr) + user_pattern_size);
940 /* Start expanding. */
941 memset(flow_items, 0, sizeof(flow_items));
942 user_pattern_size -= sizeof(*item);
943 next_node = node->next;
944 stack[stack_pos] = next_node;
945 node = next_node ? &graph[*next_node] : NULL;
947 flow_items[stack_pos].type = node->type;
948 if (node->rss_types & types) {
950 * compute the number of items to copy from the
951 * expansion and copy it.
952 * When the stack_pos is 0, there are 1 element in it,
953 * plus the addition END item.
955 int elt = stack_pos + 2;
957 flow_items[stack_pos + 1].type = RTE_FLOW_ITEM_TYPE_END;
958 lsize += elt * sizeof(*item) + user_pattern_size;
960 size_t n = elt * sizeof(*item);
962 buf->entry[buf->entries].priority =
964 buf->entry[buf->entries].pattern = addr;
966 rte_memcpy(addr, buf->entry[0].pattern,
968 addr = (void *)(((uintptr_t)addr) +
970 rte_memcpy(addr, flow_items, n);
971 addr = (void *)(((uintptr_t)addr) + n);
976 next_node = node->next;
977 if (stack_pos++ == elt_n) {
981 stack[stack_pos] = next_node;
982 } else if (*(next_node + 1)) {
983 /* Follow up with the next possibility. */
986 /* Move to the next path. */
988 next_node = stack[--stack_pos];
990 stack[stack_pos] = next_node;
992 node = *next_node ? &graph[*next_node] : NULL;