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),
129 flow_err(uint16_t port_id, int ret, struct rte_flow_error *error)
133 if (rte_eth_dev_is_removed(port_id))
134 return rte_flow_error_set(error, EIO,
135 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
136 NULL, rte_strerror(EIO));
140 /* Get generic flow operations structure from a port. */
141 const struct rte_flow_ops *
142 rte_flow_ops_get(uint16_t port_id, struct rte_flow_error *error)
144 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
145 const struct rte_flow_ops *ops;
148 if (unlikely(!rte_eth_dev_is_valid_port(port_id)))
150 else if (unlikely(!dev->dev_ops->filter_ctrl ||
151 dev->dev_ops->filter_ctrl(dev,
152 RTE_ETH_FILTER_GENERIC,
159 rte_flow_error_set(error, code, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
160 NULL, rte_strerror(code));
164 /* Check whether a flow rule can be created on a given port. */
166 rte_flow_validate(uint16_t port_id,
167 const struct rte_flow_attr *attr,
168 const struct rte_flow_item pattern[],
169 const struct rte_flow_action actions[],
170 struct rte_flow_error *error)
172 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
173 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
177 if (likely(!!ops->validate))
178 return flow_err(port_id, ops->validate(dev, attr, pattern,
179 actions, error), error);
180 return rte_flow_error_set(error, ENOSYS,
181 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
182 NULL, rte_strerror(ENOSYS));
185 /* Create a flow rule on a given port. */
187 rte_flow_create(uint16_t port_id,
188 const struct rte_flow_attr *attr,
189 const struct rte_flow_item pattern[],
190 const struct rte_flow_action actions[],
191 struct rte_flow_error *error)
193 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
194 struct rte_flow *flow;
195 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
199 if (likely(!!ops->create)) {
200 flow = ops->create(dev, attr, pattern, actions, error);
202 flow_err(port_id, -rte_errno, error);
205 rte_flow_error_set(error, ENOSYS, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
206 NULL, rte_strerror(ENOSYS));
210 /* Destroy a flow rule on a given port. */
212 rte_flow_destroy(uint16_t port_id,
213 struct rte_flow *flow,
214 struct rte_flow_error *error)
216 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
217 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
221 if (likely(!!ops->destroy))
222 return flow_err(port_id, ops->destroy(dev, flow, error),
224 return rte_flow_error_set(error, ENOSYS,
225 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
226 NULL, rte_strerror(ENOSYS));
229 /* Destroy all flow rules associated with a port. */
231 rte_flow_flush(uint16_t port_id,
232 struct rte_flow_error *error)
234 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
235 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
239 if (likely(!!ops->flush))
240 return flow_err(port_id, ops->flush(dev, error), error);
241 return rte_flow_error_set(error, ENOSYS,
242 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
243 NULL, rte_strerror(ENOSYS));
246 /* Query an existing flow rule. */
248 rte_flow_query(uint16_t port_id,
249 struct rte_flow *flow,
250 const struct rte_flow_action *action,
252 struct rte_flow_error *error)
254 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
255 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
259 if (likely(!!ops->query))
260 return flow_err(port_id, ops->query(dev, flow, action, data,
262 return rte_flow_error_set(error, ENOSYS,
263 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
264 NULL, rte_strerror(ENOSYS));
267 /* Restrict ingress traffic to the defined flow rules. */
269 rte_flow_isolate(uint16_t port_id,
271 struct rte_flow_error *error)
273 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
274 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
278 if (likely(!!ops->isolate))
279 return flow_err(port_id, ops->isolate(dev, set, error), error);
280 return rte_flow_error_set(error, ENOSYS,
281 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
282 NULL, rte_strerror(ENOSYS));
285 /* Initialize flow error structure. */
287 rte_flow_error_set(struct rte_flow_error *error,
289 enum rte_flow_error_type type,
294 *error = (struct rte_flow_error){
304 /** Pattern item specification types. */
305 enum rte_flow_conv_item_spec_type {
306 RTE_FLOW_CONV_ITEM_SPEC,
307 RTE_FLOW_CONV_ITEM_LAST,
308 RTE_FLOW_CONV_ITEM_MASK,
312 * Copy pattern item specification.
315 * Output buffer. Can be NULL if @p size is zero.
317 * Size of @p buf in bytes.
319 * Pattern item to copy specification from.
321 * Specification selector for either @p spec, @p last or @p mask.
324 * Number of bytes needed to store pattern item specification regardless
325 * of @p size. @p buf contents are truncated to @p size if not large
329 rte_flow_conv_item_spec(void *buf, const size_t size,
330 const struct rte_flow_item *item,
331 enum rte_flow_conv_item_spec_type type)
335 type == RTE_FLOW_CONV_ITEM_SPEC ? item->spec :
336 type == RTE_FLOW_CONV_ITEM_LAST ? item->last :
337 type == RTE_FLOW_CONV_ITEM_MASK ? item->mask :
340 switch (item->type) {
342 const struct rte_flow_item_raw *raw;
345 const struct rte_flow_item_raw *raw;
348 const struct rte_flow_item_raw *raw;
351 const struct rte_flow_item_raw *raw;
354 struct rte_flow_item_raw *raw;
358 case RTE_FLOW_ITEM_TYPE_RAW:
359 spec.raw = item->spec;
360 last.raw = item->last ? item->last : item->spec;
361 mask.raw = item->mask ? item->mask : &rte_flow_item_raw_mask;
365 (&(struct rte_flow_item_raw){
366 .relative = src.raw->relative,
367 .search = src.raw->search,
368 .reserved = src.raw->reserved,
369 .offset = src.raw->offset,
370 .limit = src.raw->limit,
371 .length = src.raw->length,
373 size > sizeof(*dst.raw) ? sizeof(*dst.raw) : size);
374 off = sizeof(*dst.raw);
375 if (type == RTE_FLOW_CONV_ITEM_SPEC ||
376 (type == RTE_FLOW_CONV_ITEM_MASK &&
377 ((spec.raw->length & mask.raw->length) >=
378 (last.raw->length & mask.raw->length))))
379 tmp = spec.raw->length & mask.raw->length;
381 tmp = last.raw->length & mask.raw->length;
383 off = RTE_ALIGN_CEIL(off, sizeof(*dst.raw->pattern));
384 if (size >= off + tmp)
385 dst.raw->pattern = rte_memcpy
386 ((void *)((uintptr_t)dst.raw + off),
387 src.raw->pattern, tmp);
392 off = rte_flow_desc_item[item->type].size;
393 rte_memcpy(buf, data, (size > off ? off : size));
400 * Copy action configuration.
403 * Output buffer. Can be NULL if @p size is zero.
405 * Size of @p buf in bytes.
407 * Action to copy configuration from.
410 * Number of bytes needed to store pattern item specification regardless
411 * of @p size. @p buf contents are truncated to @p size if not large
415 rte_flow_conv_action_conf(void *buf, const size_t size,
416 const struct rte_flow_action *action)
420 switch (action->type) {
422 const struct rte_flow_action_rss *rss;
423 const struct rte_flow_action_vxlan_encap *vxlan_encap;
424 const struct rte_flow_action_nvgre_encap *nvgre_encap;
427 struct rte_flow_action_rss *rss;
428 struct rte_flow_action_vxlan_encap *vxlan_encap;
429 struct rte_flow_action_nvgre_encap *nvgre_encap;
434 case RTE_FLOW_ACTION_TYPE_RSS:
435 src.rss = action->conf;
438 (&(struct rte_flow_action_rss){
439 .func = src.rss->func,
440 .level = src.rss->level,
441 .types = src.rss->types,
442 .key_len = src.rss->key_len,
443 .queue_num = src.rss->queue_num,
445 size > sizeof(*dst.rss) ? sizeof(*dst.rss) : size);
446 off = sizeof(*dst.rss);
447 if (src.rss->key_len) {
448 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->key));
449 tmp = sizeof(*src.rss->key) * src.rss->key_len;
450 if (size >= off + tmp)
451 dst.rss->key = rte_memcpy
452 ((void *)((uintptr_t)dst.rss + off),
456 if (src.rss->queue_num) {
457 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->queue));
458 tmp = sizeof(*src.rss->queue) * src.rss->queue_num;
459 if (size >= off + tmp)
460 dst.rss->queue = rte_memcpy
461 ((void *)((uintptr_t)dst.rss + off),
462 src.rss->queue, tmp);
466 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
467 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
468 src.vxlan_encap = action->conf;
469 dst.vxlan_encap = buf;
470 RTE_BUILD_BUG_ON(sizeof(*src.vxlan_encap) !=
471 sizeof(*src.nvgre_encap) ||
472 offsetof(struct rte_flow_action_vxlan_encap,
474 offsetof(struct rte_flow_action_nvgre_encap,
476 off = sizeof(*dst.vxlan_encap);
477 if (src.vxlan_encap->definition) {
479 (off, sizeof(*dst.vxlan_encap->definition));
481 (RTE_FLOW_CONV_OP_PATTERN,
482 (void *)((uintptr_t)dst.vxlan_encap + off),
483 size > off ? size - off : 0,
484 src.vxlan_encap->definition, NULL);
487 if (size >= off + ret)
488 dst.vxlan_encap->definition =
489 (void *)((uintptr_t)dst.vxlan_encap +
495 off = rte_flow_desc_action[action->type].size;
496 rte_memcpy(buf, action->conf, (size > off ? off : size));
503 * Copy a list of pattern items.
506 * Destination buffer. Can be NULL if @p size is zero.
508 * Size of @p dst in bytes.
510 * Source pattern items.
512 * Maximum number of pattern items to process from @p src or 0 to process
513 * the entire list. In both cases, processing stops after
514 * RTE_FLOW_ITEM_TYPE_END is encountered.
516 * Perform verbose error reporting if not NULL.
519 * A positive value representing the number of bytes needed to store
520 * pattern items regardless of @p size on success (@p buf contents are
521 * truncated to @p size if not large enough), a negative errno value
522 * otherwise and rte_errno is set.
525 rte_flow_conv_pattern(struct rte_flow_item *dst,
527 const struct rte_flow_item *src,
529 struct rte_flow_error *error)
531 uintptr_t data = (uintptr_t)dst;
536 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
537 if ((size_t)src->type >= RTE_DIM(rte_flow_desc_item) ||
538 !rte_flow_desc_item[src->type].name)
539 return rte_flow_error_set
540 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, src,
541 "cannot convert unknown item type");
542 if (size >= off + sizeof(*dst))
543 *dst = (struct rte_flow_item){
555 off = RTE_ALIGN_CEIL(off, sizeof(double));
556 ret = rte_flow_conv_item_spec
557 ((void *)(data + off),
558 size > off ? size - off : 0, src,
559 RTE_FLOW_CONV_ITEM_SPEC);
560 if (size && size >= off + ret)
561 dst->spec = (void *)(data + off);
566 off = RTE_ALIGN_CEIL(off, sizeof(double));
567 ret = rte_flow_conv_item_spec
568 ((void *)(data + off),
569 size > off ? size - off : 0, src,
570 RTE_FLOW_CONV_ITEM_LAST);
571 if (size && size >= off + ret)
572 dst->last = (void *)(data + off);
576 off = RTE_ALIGN_CEIL(off, sizeof(double));
577 ret = rte_flow_conv_item_spec
578 ((void *)(data + off),
579 size > off ? size - off : 0, src,
580 RTE_FLOW_CONV_ITEM_MASK);
581 if (size && size >= off + ret)
582 dst->mask = (void *)(data + off);
592 * Copy a list of actions.
595 * Destination buffer. Can be NULL if @p size is zero.
597 * Size of @p dst in bytes.
601 * Maximum number of actions to process from @p src or 0 to process the
602 * entire list. In both cases, processing stops after
603 * RTE_FLOW_ACTION_TYPE_END is encountered.
605 * Perform verbose error reporting if not NULL.
608 * A positive value representing the number of bytes needed to store
609 * actions regardless of @p size on success (@p buf contents are truncated
610 * to @p size if not large enough), a negative errno value otherwise and
614 rte_flow_conv_actions(struct rte_flow_action *dst,
616 const struct rte_flow_action *src,
618 struct rte_flow_error *error)
620 uintptr_t data = (uintptr_t)dst;
625 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
626 if ((size_t)src->type >= RTE_DIM(rte_flow_desc_action) ||
627 !rte_flow_desc_action[src->type].name)
628 return rte_flow_error_set
629 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
630 src, "cannot convert unknown action type");
631 if (size >= off + sizeof(*dst))
632 *dst = (struct rte_flow_action){
644 off = RTE_ALIGN_CEIL(off, sizeof(double));
645 ret = rte_flow_conv_action_conf
646 ((void *)(data + off),
647 size > off ? size - off : 0, src);
648 if (size && size >= off + ret)
649 dst->conf = (void *)(data + off);
659 * Copy flow rule components.
661 * This comprises the flow rule descriptor itself, attributes, pattern and
662 * actions list. NULL components in @p src are skipped.
665 * Destination buffer. Can be NULL if @p size is zero.
667 * Size of @p dst in bytes.
669 * Source flow rule descriptor.
671 * Perform verbose error reporting if not NULL.
674 * A positive value representing the number of bytes needed to store all
675 * components including the descriptor regardless of @p size on success
676 * (@p buf contents are truncated to @p size if not large enough), a
677 * negative errno value otherwise and rte_errno is set.
680 rte_flow_conv_rule(struct rte_flow_conv_rule *dst,
682 const struct rte_flow_conv_rule *src,
683 struct rte_flow_error *error)
689 (&(struct rte_flow_conv_rule){
694 size > sizeof(*dst) ? sizeof(*dst) : size);
697 off = RTE_ALIGN_CEIL(off, sizeof(double));
698 if (size && size >= off + sizeof(*dst->attr))
699 dst->attr = rte_memcpy
700 ((void *)((uintptr_t)dst + off),
701 src->attr_ro, sizeof(*dst->attr));
702 off += sizeof(*dst->attr);
704 if (src->pattern_ro) {
705 off = RTE_ALIGN_CEIL(off, sizeof(double));
706 ret = rte_flow_conv_pattern((void *)((uintptr_t)dst + off),
707 size > off ? size - off : 0,
708 src->pattern_ro, 0, error);
711 if (size && size >= off + (size_t)ret)
712 dst->pattern = (void *)((uintptr_t)dst + off);
715 if (src->actions_ro) {
716 off = RTE_ALIGN_CEIL(off, sizeof(double));
717 ret = rte_flow_conv_actions((void *)((uintptr_t)dst + off),
718 size > off ? size - off : 0,
719 src->actions_ro, 0, error);
722 if (size >= off + (size_t)ret)
723 dst->actions = (void *)((uintptr_t)dst + off);
730 * Retrieve the name of a pattern item/action type.
733 * Nonzero when @p src represents an action type instead of a pattern item
736 * Nonzero to write string address instead of contents into @p dst.
738 * Destination buffer. Can be NULL if @p size is zero.
740 * Size of @p dst in bytes.
742 * Depending on @p is_action, source pattern item or action type cast as a
745 * Perform verbose error reporting if not NULL.
748 * A positive value representing the number of bytes needed to store the
749 * name or its address regardless of @p size on success (@p buf contents
750 * are truncated to @p size if not large enough), a negative errno value
751 * otherwise and rte_errno is set.
754 rte_flow_conv_name(int is_action,
759 struct rte_flow_error *error)
762 const struct rte_flow_desc_data *data;
765 static const struct desc_info info_rep[2] = {
766 { rte_flow_desc_item, RTE_DIM(rte_flow_desc_item), },
767 { rte_flow_desc_action, RTE_DIM(rte_flow_desc_action), },
769 const struct desc_info *const info = &info_rep[!!is_action];
770 unsigned int type = (uintptr_t)src;
772 if (type >= info->num)
773 return rte_flow_error_set
774 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
775 "unknown object type to retrieve the name of");
777 return strlcpy(dst, info->data[type].name, size);
778 if (size >= sizeof(const char **))
779 *((const char **)dst) = info->data[type].name;
780 return sizeof(const char **);
783 /** Helper function to convert flow API objects. */
785 rte_flow_conv(enum rte_flow_conv_op op,
789 struct rte_flow_error *error)
792 const struct rte_flow_attr *attr;
794 case RTE_FLOW_CONV_OP_NONE:
796 case RTE_FLOW_CONV_OP_ATTR:
798 if (size > sizeof(*attr))
799 size = sizeof(*attr);
800 rte_memcpy(dst, attr, size);
801 return sizeof(*attr);
802 case RTE_FLOW_CONV_OP_ITEM:
803 return rte_flow_conv_pattern(dst, size, src, 1, error);
804 case RTE_FLOW_CONV_OP_ACTION:
805 return rte_flow_conv_actions(dst, size, src, 1, error);
806 case RTE_FLOW_CONV_OP_PATTERN:
807 return rte_flow_conv_pattern(dst, size, src, 0, error);
808 case RTE_FLOW_CONV_OP_ACTIONS:
809 return rte_flow_conv_actions(dst, size, src, 0, error);
810 case RTE_FLOW_CONV_OP_RULE:
811 return rte_flow_conv_rule(dst, size, src, error);
812 case RTE_FLOW_CONV_OP_ITEM_NAME:
813 return rte_flow_conv_name(0, 0, dst, size, src, error);
814 case RTE_FLOW_CONV_OP_ACTION_NAME:
815 return rte_flow_conv_name(1, 0, dst, size, src, error);
816 case RTE_FLOW_CONV_OP_ITEM_NAME_PTR:
817 return rte_flow_conv_name(0, 1, dst, size, src, error);
818 case RTE_FLOW_CONV_OP_ACTION_NAME_PTR:
819 return rte_flow_conv_name(1, 1, dst, size, src, error);
821 return rte_flow_error_set
822 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
823 "unknown object conversion operation");
826 /** Store a full rte_flow description. */
828 rte_flow_copy(struct rte_flow_desc *desc, size_t len,
829 const struct rte_flow_attr *attr,
830 const struct rte_flow_item *items,
831 const struct rte_flow_action *actions)
834 * Overlap struct rte_flow_conv with struct rte_flow_desc in order
835 * to convert the former to the latter without wasting space.
837 struct rte_flow_conv_rule *dst =
839 (void *)((uintptr_t)desc +
840 (offsetof(struct rte_flow_desc, actions) -
841 offsetof(struct rte_flow_conv_rule, actions))) :
844 len > sizeof(*desc) - sizeof(*dst) ?
845 len - (sizeof(*desc) - sizeof(*dst)) :
847 struct rte_flow_conv_rule src = {
850 .actions_ro = actions,
854 RTE_BUILD_BUG_ON(sizeof(struct rte_flow_desc) <
855 sizeof(struct rte_flow_conv_rule));
857 (&dst->pattern != &desc->items ||
858 &dst->actions != &desc->actions ||
859 (uintptr_t)(dst + 1) != (uintptr_t)(desc + 1))) {
863 ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, dst, dst_size, &src, NULL);
866 ret += sizeof(*desc) - sizeof(*dst);
868 (&(struct rte_flow_desc){
871 .items = dst_size ? dst->pattern : NULL,
872 .actions = dst_size ? dst->actions : NULL,
874 len > sizeof(*desc) ? sizeof(*desc) : len);
879 * Expand RSS flows into several possible flows according to the RSS hash
880 * fields requested and the driver capabilities.
882 int __rte_experimental
883 rte_flow_expand_rss(struct rte_flow_expand_rss *buf, size_t size,
884 const struct rte_flow_item *pattern, uint64_t types,
885 const struct rte_flow_expand_node graph[],
886 int graph_root_index)
889 const struct rte_flow_item *item;
890 const struct rte_flow_expand_node *node = &graph[graph_root_index];
891 const int *next_node;
892 const int *stack[elt_n];
894 struct rte_flow_item flow_items[elt_n];
897 size_t user_pattern_size = 0;
900 lsize = offsetof(struct rte_flow_expand_rss, entry) +
901 elt_n * sizeof(buf->entry[0]);
903 buf->entry[0].priority = 0;
904 buf->entry[0].pattern = (void *)&buf->entry[elt_n];
906 addr = buf->entry[0].pattern;
908 for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
909 const struct rte_flow_expand_node *next = NULL;
911 for (i = 0; node->next && node->next[i]; ++i) {
912 next = &graph[node->next[i]];
913 if (next->type == item->type)
918 user_pattern_size += sizeof(*item);
920 user_pattern_size += sizeof(*item); /* Handle END item. */
921 lsize += user_pattern_size;
922 /* Copy the user pattern in the first entry of the buffer. */
924 rte_memcpy(addr, pattern, user_pattern_size);
925 addr = (void *)(((uintptr_t)addr) + user_pattern_size);
928 /* Start expanding. */
929 memset(flow_items, 0, sizeof(flow_items));
930 user_pattern_size -= sizeof(*item);
931 next_node = node->next;
932 stack[stack_pos] = next_node;
933 node = next_node ? &graph[*next_node] : NULL;
935 flow_items[stack_pos].type = node->type;
936 if (node->rss_types & types) {
938 * compute the number of items to copy from the
939 * expansion and copy it.
940 * When the stack_pos is 0, there are 1 element in it,
941 * plus the addition END item.
943 int elt = stack_pos + 2;
945 flow_items[stack_pos + 1].type = RTE_FLOW_ITEM_TYPE_END;
946 lsize += elt * sizeof(*item) + user_pattern_size;
948 size_t n = elt * sizeof(*item);
950 buf->entry[buf->entries].priority =
952 buf->entry[buf->entries].pattern = addr;
954 rte_memcpy(addr, buf->entry[0].pattern,
956 addr = (void *)(((uintptr_t)addr) +
958 rte_memcpy(addr, flow_items, n);
959 addr = (void *)(((uintptr_t)addr) + n);
964 next_node = node->next;
965 if (stack_pos++ == elt_n) {
969 stack[stack_pos] = next_node;
970 } else if (*(next_node + 1)) {
971 /* Follow up with the next possibility. */
974 /* Move to the next path. */
976 next_node = stack[--stack_pos];
978 stack[stack_pos] = next_node;
980 node = *next_node ? &graph[*next_node] : NULL;