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)),
76 MK_FLOW_ITEM(META, sizeof(struct rte_flow_item_meta)),
77 MK_FLOW_ITEM(GRE_KEY, sizeof(rte_be32_t)),
78 MK_FLOW_ITEM(GTP_PSC, sizeof(struct rte_flow_item_gtp_psc)),
79 MK_FLOW_ITEM(PPPOES, sizeof(struct rte_flow_item_pppoe)),
80 MK_FLOW_ITEM(PPPOED, sizeof(struct rte_flow_item_pppoe)),
81 MK_FLOW_ITEM(PPPOE_PROTO_ID,
82 sizeof(struct rte_flow_item_pppoe_proto_id)),
83 MK_FLOW_ITEM(NSH, sizeof(struct rte_flow_item_nsh)),
86 /** Generate flow_action[] entry. */
87 #define MK_FLOW_ACTION(t, s) \
88 [RTE_FLOW_ACTION_TYPE_ ## t] = { \
93 /** Information about known flow actions. */
94 static const struct rte_flow_desc_data rte_flow_desc_action[] = {
95 MK_FLOW_ACTION(END, 0),
96 MK_FLOW_ACTION(VOID, 0),
97 MK_FLOW_ACTION(PASSTHRU, 0),
98 MK_FLOW_ACTION(JUMP, sizeof(struct rte_flow_action_jump)),
99 MK_FLOW_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
100 MK_FLOW_ACTION(FLAG, 0),
101 MK_FLOW_ACTION(QUEUE, sizeof(struct rte_flow_action_queue)),
102 MK_FLOW_ACTION(DROP, 0),
103 MK_FLOW_ACTION(COUNT, sizeof(struct rte_flow_action_count)),
104 MK_FLOW_ACTION(RSS, sizeof(struct rte_flow_action_rss)),
105 MK_FLOW_ACTION(PF, 0),
106 MK_FLOW_ACTION(VF, sizeof(struct rte_flow_action_vf)),
107 MK_FLOW_ACTION(PHY_PORT, sizeof(struct rte_flow_action_phy_port)),
108 MK_FLOW_ACTION(PORT_ID, sizeof(struct rte_flow_action_port_id)),
109 MK_FLOW_ACTION(METER, sizeof(struct rte_flow_action_meter)),
110 MK_FLOW_ACTION(SECURITY, sizeof(struct rte_flow_action_security)),
111 MK_FLOW_ACTION(OF_SET_MPLS_TTL,
112 sizeof(struct rte_flow_action_of_set_mpls_ttl)),
113 MK_FLOW_ACTION(OF_DEC_MPLS_TTL, 0),
114 MK_FLOW_ACTION(OF_SET_NW_TTL,
115 sizeof(struct rte_flow_action_of_set_nw_ttl)),
116 MK_FLOW_ACTION(OF_DEC_NW_TTL, 0),
117 MK_FLOW_ACTION(OF_COPY_TTL_OUT, 0),
118 MK_FLOW_ACTION(OF_COPY_TTL_IN, 0),
119 MK_FLOW_ACTION(OF_POP_VLAN, 0),
120 MK_FLOW_ACTION(OF_PUSH_VLAN,
121 sizeof(struct rte_flow_action_of_push_vlan)),
122 MK_FLOW_ACTION(OF_SET_VLAN_VID,
123 sizeof(struct rte_flow_action_of_set_vlan_vid)),
124 MK_FLOW_ACTION(OF_SET_VLAN_PCP,
125 sizeof(struct rte_flow_action_of_set_vlan_pcp)),
126 MK_FLOW_ACTION(OF_POP_MPLS,
127 sizeof(struct rte_flow_action_of_pop_mpls)),
128 MK_FLOW_ACTION(OF_PUSH_MPLS,
129 sizeof(struct rte_flow_action_of_push_mpls)),
130 MK_FLOW_ACTION(VXLAN_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
131 MK_FLOW_ACTION(VXLAN_DECAP, 0),
132 MK_FLOW_ACTION(NVGRE_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
133 MK_FLOW_ACTION(NVGRE_DECAP, 0),
134 MK_FLOW_ACTION(RAW_ENCAP, sizeof(struct rte_flow_action_raw_encap)),
135 MK_FLOW_ACTION(RAW_DECAP, sizeof(struct rte_flow_action_raw_decap)),
136 MK_FLOW_ACTION(SET_IPV4_SRC,
137 sizeof(struct rte_flow_action_set_ipv4)),
138 MK_FLOW_ACTION(SET_IPV4_DST,
139 sizeof(struct rte_flow_action_set_ipv4)),
140 MK_FLOW_ACTION(SET_IPV6_SRC,
141 sizeof(struct rte_flow_action_set_ipv6)),
142 MK_FLOW_ACTION(SET_IPV6_DST,
143 sizeof(struct rte_flow_action_set_ipv6)),
144 MK_FLOW_ACTION(SET_TP_SRC,
145 sizeof(struct rte_flow_action_set_tp)),
146 MK_FLOW_ACTION(SET_TP_DST,
147 sizeof(struct rte_flow_action_set_tp)),
148 MK_FLOW_ACTION(MAC_SWAP, 0),
149 MK_FLOW_ACTION(DEC_TTL, 0),
150 MK_FLOW_ACTION(SET_TTL, sizeof(struct rte_flow_action_set_ttl)),
151 MK_FLOW_ACTION(SET_MAC_SRC, sizeof(struct rte_flow_action_set_mac)),
152 MK_FLOW_ACTION(SET_MAC_DST, sizeof(struct rte_flow_action_set_mac)),
153 MK_FLOW_ACTION(INC_TCP_SEQ, sizeof(rte_be32_t)),
154 MK_FLOW_ACTION(DEC_TCP_SEQ, sizeof(rte_be32_t)),
155 MK_FLOW_ACTION(INC_TCP_ACK, sizeof(rte_be32_t)),
156 MK_FLOW_ACTION(DEC_TCP_ACK, sizeof(rte_be32_t)),
160 flow_err(uint16_t port_id, int ret, struct rte_flow_error *error)
164 if (rte_eth_dev_is_removed(port_id))
165 return rte_flow_error_set(error, EIO,
166 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
167 NULL, rte_strerror(EIO));
171 /* Get generic flow operations structure from a port. */
172 const struct rte_flow_ops *
173 rte_flow_ops_get(uint16_t port_id, struct rte_flow_error *error)
175 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
176 const struct rte_flow_ops *ops;
179 if (unlikely(!rte_eth_dev_is_valid_port(port_id)))
181 else if (unlikely(!dev->dev_ops->filter_ctrl ||
182 dev->dev_ops->filter_ctrl(dev,
183 RTE_ETH_FILTER_GENERIC,
190 rte_flow_error_set(error, code, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
191 NULL, rte_strerror(code));
195 /* Check whether a flow rule can be created on a given port. */
197 rte_flow_validate(uint16_t port_id,
198 const struct rte_flow_attr *attr,
199 const struct rte_flow_item pattern[],
200 const struct rte_flow_action actions[],
201 struct rte_flow_error *error)
203 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
204 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
208 if (likely(!!ops->validate))
209 return flow_err(port_id, ops->validate(dev, attr, pattern,
210 actions, error), error);
211 return rte_flow_error_set(error, ENOSYS,
212 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
213 NULL, rte_strerror(ENOSYS));
216 /* Create a flow rule on a given port. */
218 rte_flow_create(uint16_t port_id,
219 const struct rte_flow_attr *attr,
220 const struct rte_flow_item pattern[],
221 const struct rte_flow_action actions[],
222 struct rte_flow_error *error)
224 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
225 struct rte_flow *flow;
226 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
230 if (likely(!!ops->create)) {
231 flow = ops->create(dev, attr, pattern, actions, error);
233 flow_err(port_id, -rte_errno, error);
236 rte_flow_error_set(error, ENOSYS, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
237 NULL, rte_strerror(ENOSYS));
241 /* Destroy a flow rule on a given port. */
243 rte_flow_destroy(uint16_t port_id,
244 struct rte_flow *flow,
245 struct rte_flow_error *error)
247 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
248 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
252 if (likely(!!ops->destroy))
253 return flow_err(port_id, ops->destroy(dev, flow, error),
255 return rte_flow_error_set(error, ENOSYS,
256 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
257 NULL, rte_strerror(ENOSYS));
260 /* Destroy all flow rules associated with a port. */
262 rte_flow_flush(uint16_t port_id,
263 struct rte_flow_error *error)
265 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
266 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
270 if (likely(!!ops->flush))
271 return flow_err(port_id, ops->flush(dev, error), error);
272 return rte_flow_error_set(error, ENOSYS,
273 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
274 NULL, rte_strerror(ENOSYS));
277 /* Query an existing flow rule. */
279 rte_flow_query(uint16_t port_id,
280 struct rte_flow *flow,
281 const struct rte_flow_action *action,
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->query))
291 return flow_err(port_id, ops->query(dev, flow, action, data,
293 return rte_flow_error_set(error, ENOSYS,
294 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
295 NULL, rte_strerror(ENOSYS));
298 /* Restrict ingress traffic to the defined flow rules. */
300 rte_flow_isolate(uint16_t port_id,
302 struct rte_flow_error *error)
304 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
305 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
309 if (likely(!!ops->isolate))
310 return flow_err(port_id, ops->isolate(dev, set, error), error);
311 return rte_flow_error_set(error, ENOSYS,
312 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
313 NULL, rte_strerror(ENOSYS));
316 /* Initialize flow error structure. */
318 rte_flow_error_set(struct rte_flow_error *error,
320 enum rte_flow_error_type type,
325 *error = (struct rte_flow_error){
335 /** Pattern item specification types. */
336 enum rte_flow_conv_item_spec_type {
337 RTE_FLOW_CONV_ITEM_SPEC,
338 RTE_FLOW_CONV_ITEM_LAST,
339 RTE_FLOW_CONV_ITEM_MASK,
343 * Copy pattern item specification.
346 * Output buffer. Can be NULL if @p size is zero.
348 * Size of @p buf in bytes.
350 * Pattern item to copy specification from.
352 * Specification selector for either @p spec, @p last or @p mask.
355 * Number of bytes needed to store pattern item specification regardless
356 * of @p size. @p buf contents are truncated to @p size if not large
360 rte_flow_conv_item_spec(void *buf, const size_t size,
361 const struct rte_flow_item *item,
362 enum rte_flow_conv_item_spec_type type)
366 type == RTE_FLOW_CONV_ITEM_SPEC ? item->spec :
367 type == RTE_FLOW_CONV_ITEM_LAST ? item->last :
368 type == RTE_FLOW_CONV_ITEM_MASK ? item->mask :
371 switch (item->type) {
373 const struct rte_flow_item_raw *raw;
376 const struct rte_flow_item_raw *raw;
379 const struct rte_flow_item_raw *raw;
382 const struct rte_flow_item_raw *raw;
385 struct rte_flow_item_raw *raw;
389 case RTE_FLOW_ITEM_TYPE_RAW:
390 spec.raw = item->spec;
391 last.raw = item->last ? item->last : item->spec;
392 mask.raw = item->mask ? item->mask : &rte_flow_item_raw_mask;
396 (&(struct rte_flow_item_raw){
397 .relative = src.raw->relative,
398 .search = src.raw->search,
399 .reserved = src.raw->reserved,
400 .offset = src.raw->offset,
401 .limit = src.raw->limit,
402 .length = src.raw->length,
404 size > sizeof(*dst.raw) ? sizeof(*dst.raw) : size);
405 off = sizeof(*dst.raw);
406 if (type == RTE_FLOW_CONV_ITEM_SPEC ||
407 (type == RTE_FLOW_CONV_ITEM_MASK &&
408 ((spec.raw->length & mask.raw->length) >=
409 (last.raw->length & mask.raw->length))))
410 tmp = spec.raw->length & mask.raw->length;
412 tmp = last.raw->length & mask.raw->length;
414 off = RTE_ALIGN_CEIL(off, sizeof(*dst.raw->pattern));
415 if (size >= off + tmp)
416 dst.raw->pattern = rte_memcpy
417 ((void *)((uintptr_t)dst.raw + off),
418 src.raw->pattern, tmp);
423 off = rte_flow_desc_item[item->type].size;
424 rte_memcpy(buf, data, (size > off ? off : size));
431 * Copy action configuration.
434 * Output buffer. Can be NULL if @p size is zero.
436 * Size of @p buf in bytes.
438 * Action to copy configuration from.
441 * Number of bytes needed to store pattern item specification regardless
442 * of @p size. @p buf contents are truncated to @p size if not large
446 rte_flow_conv_action_conf(void *buf, const size_t size,
447 const struct rte_flow_action *action)
451 switch (action->type) {
453 const struct rte_flow_action_rss *rss;
454 const struct rte_flow_action_vxlan_encap *vxlan_encap;
455 const struct rte_flow_action_nvgre_encap *nvgre_encap;
458 struct rte_flow_action_rss *rss;
459 struct rte_flow_action_vxlan_encap *vxlan_encap;
460 struct rte_flow_action_nvgre_encap *nvgre_encap;
465 case RTE_FLOW_ACTION_TYPE_RSS:
466 src.rss = action->conf;
469 (&(struct rte_flow_action_rss){
470 .func = src.rss->func,
471 .level = src.rss->level,
472 .types = src.rss->types,
473 .key_len = src.rss->key_len,
474 .queue_num = src.rss->queue_num,
476 size > sizeof(*dst.rss) ? sizeof(*dst.rss) : size);
477 off = sizeof(*dst.rss);
478 if (src.rss->key_len) {
479 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->key));
480 tmp = sizeof(*src.rss->key) * src.rss->key_len;
481 if (size >= off + tmp)
482 dst.rss->key = rte_memcpy
483 ((void *)((uintptr_t)dst.rss + off),
487 if (src.rss->queue_num) {
488 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->queue));
489 tmp = sizeof(*src.rss->queue) * src.rss->queue_num;
490 if (size >= off + tmp)
491 dst.rss->queue = rte_memcpy
492 ((void *)((uintptr_t)dst.rss + off),
493 src.rss->queue, tmp);
497 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
498 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
499 src.vxlan_encap = action->conf;
500 dst.vxlan_encap = buf;
501 RTE_BUILD_BUG_ON(sizeof(*src.vxlan_encap) !=
502 sizeof(*src.nvgre_encap) ||
503 offsetof(struct rte_flow_action_vxlan_encap,
505 offsetof(struct rte_flow_action_nvgre_encap,
507 off = sizeof(*dst.vxlan_encap);
508 if (src.vxlan_encap->definition) {
510 (off, sizeof(*dst.vxlan_encap->definition));
512 (RTE_FLOW_CONV_OP_PATTERN,
513 (void *)((uintptr_t)dst.vxlan_encap + off),
514 size > off ? size - off : 0,
515 src.vxlan_encap->definition, NULL);
518 if (size >= off + ret)
519 dst.vxlan_encap->definition =
520 (void *)((uintptr_t)dst.vxlan_encap +
526 off = rte_flow_desc_action[action->type].size;
527 rte_memcpy(buf, action->conf, (size > off ? off : size));
534 * Copy a list of pattern items.
537 * Destination buffer. Can be NULL if @p size is zero.
539 * Size of @p dst in bytes.
541 * Source pattern items.
543 * Maximum number of pattern items to process from @p src or 0 to process
544 * the entire list. In both cases, processing stops after
545 * RTE_FLOW_ITEM_TYPE_END is encountered.
547 * Perform verbose error reporting if not NULL.
550 * A positive value representing the number of bytes needed to store
551 * pattern items regardless of @p size on success (@p buf contents are
552 * truncated to @p size if not large enough), a negative errno value
553 * otherwise and rte_errno is set.
556 rte_flow_conv_pattern(struct rte_flow_item *dst,
558 const struct rte_flow_item *src,
560 struct rte_flow_error *error)
562 uintptr_t data = (uintptr_t)dst;
567 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
568 if ((size_t)src->type >= RTE_DIM(rte_flow_desc_item) ||
569 !rte_flow_desc_item[src->type].name)
570 return rte_flow_error_set
571 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, src,
572 "cannot convert unknown item type");
573 if (size >= off + sizeof(*dst))
574 *dst = (struct rte_flow_item){
586 off = RTE_ALIGN_CEIL(off, sizeof(double));
587 ret = rte_flow_conv_item_spec
588 ((void *)(data + off),
589 size > off ? size - off : 0, src,
590 RTE_FLOW_CONV_ITEM_SPEC);
591 if (size && size >= off + ret)
592 dst->spec = (void *)(data + off);
597 off = RTE_ALIGN_CEIL(off, sizeof(double));
598 ret = rte_flow_conv_item_spec
599 ((void *)(data + off),
600 size > off ? size - off : 0, src,
601 RTE_FLOW_CONV_ITEM_LAST);
602 if (size && size >= off + ret)
603 dst->last = (void *)(data + off);
607 off = RTE_ALIGN_CEIL(off, sizeof(double));
608 ret = rte_flow_conv_item_spec
609 ((void *)(data + off),
610 size > off ? size - off : 0, src,
611 RTE_FLOW_CONV_ITEM_MASK);
612 if (size && size >= off + ret)
613 dst->mask = (void *)(data + off);
623 * Copy a list of actions.
626 * Destination buffer. Can be NULL if @p size is zero.
628 * Size of @p dst in bytes.
632 * Maximum number of actions to process from @p src or 0 to process the
633 * entire list. In both cases, processing stops after
634 * RTE_FLOW_ACTION_TYPE_END is encountered.
636 * Perform verbose error reporting if not NULL.
639 * A positive value representing the number of bytes needed to store
640 * actions regardless of @p size on success (@p buf contents are truncated
641 * to @p size if not large enough), a negative errno value otherwise and
645 rte_flow_conv_actions(struct rte_flow_action *dst,
647 const struct rte_flow_action *src,
649 struct rte_flow_error *error)
651 uintptr_t data = (uintptr_t)dst;
656 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
657 if ((size_t)src->type >= RTE_DIM(rte_flow_desc_action) ||
658 !rte_flow_desc_action[src->type].name)
659 return rte_flow_error_set
660 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
661 src, "cannot convert unknown action type");
662 if (size >= off + sizeof(*dst))
663 *dst = (struct rte_flow_action){
675 off = RTE_ALIGN_CEIL(off, sizeof(double));
676 ret = rte_flow_conv_action_conf
677 ((void *)(data + off),
678 size > off ? size - off : 0, src);
679 if (size && size >= off + ret)
680 dst->conf = (void *)(data + off);
690 * Copy flow rule components.
692 * This comprises the flow rule descriptor itself, attributes, pattern and
693 * actions list. NULL components in @p src are skipped.
696 * Destination buffer. Can be NULL if @p size is zero.
698 * Size of @p dst in bytes.
700 * Source flow rule descriptor.
702 * Perform verbose error reporting if not NULL.
705 * A positive value representing the number of bytes needed to store all
706 * components including the descriptor regardless of @p size on success
707 * (@p buf contents are truncated to @p size if not large enough), a
708 * negative errno value otherwise and rte_errno is set.
711 rte_flow_conv_rule(struct rte_flow_conv_rule *dst,
713 const struct rte_flow_conv_rule *src,
714 struct rte_flow_error *error)
720 (&(struct rte_flow_conv_rule){
725 size > sizeof(*dst) ? sizeof(*dst) : size);
728 off = RTE_ALIGN_CEIL(off, sizeof(double));
729 if (size && size >= off + sizeof(*dst->attr))
730 dst->attr = rte_memcpy
731 ((void *)((uintptr_t)dst + off),
732 src->attr_ro, sizeof(*dst->attr));
733 off += sizeof(*dst->attr);
735 if (src->pattern_ro) {
736 off = RTE_ALIGN_CEIL(off, sizeof(double));
737 ret = rte_flow_conv_pattern((void *)((uintptr_t)dst + off),
738 size > off ? size - off : 0,
739 src->pattern_ro, 0, error);
742 if (size && size >= off + (size_t)ret)
743 dst->pattern = (void *)((uintptr_t)dst + off);
746 if (src->actions_ro) {
747 off = RTE_ALIGN_CEIL(off, sizeof(double));
748 ret = rte_flow_conv_actions((void *)((uintptr_t)dst + off),
749 size > off ? size - off : 0,
750 src->actions_ro, 0, error);
753 if (size >= off + (size_t)ret)
754 dst->actions = (void *)((uintptr_t)dst + off);
761 * Retrieve the name of a pattern item/action type.
764 * Nonzero when @p src represents an action type instead of a pattern item
767 * Nonzero to write string address instead of contents into @p dst.
769 * Destination buffer. Can be NULL if @p size is zero.
771 * Size of @p dst in bytes.
773 * Depending on @p is_action, source pattern item or action type cast as a
776 * Perform verbose error reporting if not NULL.
779 * A positive value representing the number of bytes needed to store the
780 * name or its address regardless of @p size on success (@p buf contents
781 * are truncated to @p size if not large enough), a negative errno value
782 * otherwise and rte_errno is set.
785 rte_flow_conv_name(int is_action,
790 struct rte_flow_error *error)
793 const struct rte_flow_desc_data *data;
796 static const struct desc_info info_rep[2] = {
797 { rte_flow_desc_item, RTE_DIM(rte_flow_desc_item), },
798 { rte_flow_desc_action, RTE_DIM(rte_flow_desc_action), },
800 const struct desc_info *const info = &info_rep[!!is_action];
801 unsigned int type = (uintptr_t)src;
803 if (type >= info->num)
804 return rte_flow_error_set
805 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
806 "unknown object type to retrieve the name of");
808 return strlcpy(dst, info->data[type].name, size);
809 if (size >= sizeof(const char **))
810 *((const char **)dst) = info->data[type].name;
811 return sizeof(const char **);
814 /** Helper function to convert flow API objects. */
816 rte_flow_conv(enum rte_flow_conv_op op,
820 struct rte_flow_error *error)
823 const struct rte_flow_attr *attr;
825 case RTE_FLOW_CONV_OP_NONE:
827 case RTE_FLOW_CONV_OP_ATTR:
829 if (size > sizeof(*attr))
830 size = sizeof(*attr);
831 rte_memcpy(dst, attr, size);
832 return sizeof(*attr);
833 case RTE_FLOW_CONV_OP_ITEM:
834 return rte_flow_conv_pattern(dst, size, src, 1, error);
835 case RTE_FLOW_CONV_OP_ACTION:
836 return rte_flow_conv_actions(dst, size, src, 1, error);
837 case RTE_FLOW_CONV_OP_PATTERN:
838 return rte_flow_conv_pattern(dst, size, src, 0, error);
839 case RTE_FLOW_CONV_OP_ACTIONS:
840 return rte_flow_conv_actions(dst, size, src, 0, error);
841 case RTE_FLOW_CONV_OP_RULE:
842 return rte_flow_conv_rule(dst, size, src, error);
843 case RTE_FLOW_CONV_OP_ITEM_NAME:
844 return rte_flow_conv_name(0, 0, dst, size, src, error);
845 case RTE_FLOW_CONV_OP_ACTION_NAME:
846 return rte_flow_conv_name(1, 0, dst, size, src, error);
847 case RTE_FLOW_CONV_OP_ITEM_NAME_PTR:
848 return rte_flow_conv_name(0, 1, dst, size, src, error);
849 case RTE_FLOW_CONV_OP_ACTION_NAME_PTR:
850 return rte_flow_conv_name(1, 1, dst, size, src, error);
852 return rte_flow_error_set
853 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
854 "unknown object conversion operation");
857 /** Store a full rte_flow description. */
859 rte_flow_copy(struct rte_flow_desc *desc, size_t len,
860 const struct rte_flow_attr *attr,
861 const struct rte_flow_item *items,
862 const struct rte_flow_action *actions)
865 * Overlap struct rte_flow_conv with struct rte_flow_desc in order
866 * to convert the former to the latter without wasting space.
868 struct rte_flow_conv_rule *dst =
870 (void *)((uintptr_t)desc +
871 (offsetof(struct rte_flow_desc, actions) -
872 offsetof(struct rte_flow_conv_rule, actions))) :
875 len > sizeof(*desc) - sizeof(*dst) ?
876 len - (sizeof(*desc) - sizeof(*dst)) :
878 struct rte_flow_conv_rule src = {
881 .actions_ro = actions,
885 RTE_BUILD_BUG_ON(sizeof(struct rte_flow_desc) <
886 sizeof(struct rte_flow_conv_rule));
888 (&dst->pattern != &desc->items ||
889 &dst->actions != &desc->actions ||
890 (uintptr_t)(dst + 1) != (uintptr_t)(desc + 1))) {
894 ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, dst, dst_size, &src, NULL);
897 ret += sizeof(*desc) - sizeof(*dst);
899 (&(struct rte_flow_desc){
902 .items = dst_size ? dst->pattern : NULL,
903 .actions = dst_size ? dst->actions : NULL,
905 len > sizeof(*desc) ? sizeof(*desc) : len);
910 * Expand RSS flows into several possible flows according to the RSS hash
911 * fields requested and the driver capabilities.
914 rte_flow_expand_rss(struct rte_flow_expand_rss *buf, size_t size,
915 const struct rte_flow_item *pattern, uint64_t types,
916 const struct rte_flow_expand_node graph[],
917 int graph_root_index)
920 const struct rte_flow_item *item;
921 const struct rte_flow_expand_node *node = &graph[graph_root_index];
922 const int *next_node;
923 const int *stack[elt_n];
925 struct rte_flow_item flow_items[elt_n];
928 size_t user_pattern_size = 0;
931 lsize = offsetof(struct rte_flow_expand_rss, entry) +
932 elt_n * sizeof(buf->entry[0]);
934 buf->entry[0].priority = 0;
935 buf->entry[0].pattern = (void *)&buf->entry[elt_n];
937 addr = buf->entry[0].pattern;
939 for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
940 const struct rte_flow_expand_node *next = NULL;
942 for (i = 0; node->next && node->next[i]; ++i) {
943 next = &graph[node->next[i]];
944 if (next->type == item->type)
949 user_pattern_size += sizeof(*item);
951 user_pattern_size += sizeof(*item); /* Handle END item. */
952 lsize += user_pattern_size;
953 /* Copy the user pattern in the first entry of the buffer. */
955 rte_memcpy(addr, pattern, user_pattern_size);
956 addr = (void *)(((uintptr_t)addr) + user_pattern_size);
959 /* Start expanding. */
960 memset(flow_items, 0, sizeof(flow_items));
961 user_pattern_size -= sizeof(*item);
962 next_node = node->next;
963 stack[stack_pos] = next_node;
964 node = next_node ? &graph[*next_node] : NULL;
966 flow_items[stack_pos].type = node->type;
967 if (node->rss_types & types) {
969 * compute the number of items to copy from the
970 * expansion and copy it.
971 * When the stack_pos is 0, there are 1 element in it,
972 * plus the addition END item.
974 int elt = stack_pos + 2;
976 flow_items[stack_pos + 1].type = RTE_FLOW_ITEM_TYPE_END;
977 lsize += elt * sizeof(*item) + user_pattern_size;
979 size_t n = elt * sizeof(*item);
981 buf->entry[buf->entries].priority =
983 buf->entry[buf->entries].pattern = addr;
985 rte_memcpy(addr, buf->entry[0].pattern,
987 addr = (void *)(((uintptr_t)addr) +
989 rte_memcpy(addr, flow_items, n);
990 addr = (void *)(((uintptr_t)addr) + n);
995 next_node = node->next;
996 if (stack_pos++ == elt_n) {
1000 stack[stack_pos] = next_node;
1001 } else if (*(next_node + 1)) {
1002 /* Follow up with the next possibility. */
1005 /* Move to the next path. */
1007 next_node = stack[--stack_pos];
1009 stack[stack_pos] = next_node;
1011 node = *next_node ? &graph[*next_node] : NULL;