1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016 6WIND S.A.
3 * Copyright 2016 Mellanox Technologies, Ltd
11 #include <rte_common.h>
12 #include <rte_errno.h>
13 #include <rte_branch_prediction.h>
14 #include <rte_string_fns.h>
16 #include <rte_mbuf_dyn.h>
17 #include "rte_ethdev.h"
18 #include "rte_flow_driver.h"
21 /* Mbuf dynamic field name for metadata. */
22 int rte_flow_dynf_metadata_offs = -1;
24 /* Mbuf dynamic field flag bit number for metadata. */
25 uint64_t rte_flow_dynf_metadata_mask;
28 * Flow elements description tables.
30 struct rte_flow_desc_data {
35 /** Generate flow_item[] entry. */
36 #define MK_FLOW_ITEM(t, s) \
37 [RTE_FLOW_ITEM_TYPE_ ## t] = { \
42 /** Information about known flow pattern items. */
43 static const struct rte_flow_desc_data rte_flow_desc_item[] = {
45 MK_FLOW_ITEM(VOID, 0),
46 MK_FLOW_ITEM(INVERT, 0),
47 MK_FLOW_ITEM(ANY, sizeof(struct rte_flow_item_any)),
49 MK_FLOW_ITEM(VF, sizeof(struct rte_flow_item_vf)),
50 MK_FLOW_ITEM(PHY_PORT, sizeof(struct rte_flow_item_phy_port)),
51 MK_FLOW_ITEM(PORT_ID, sizeof(struct rte_flow_item_port_id)),
52 MK_FLOW_ITEM(RAW, sizeof(struct rte_flow_item_raw)),
53 MK_FLOW_ITEM(ETH, sizeof(struct rte_flow_item_eth)),
54 MK_FLOW_ITEM(VLAN, sizeof(struct rte_flow_item_vlan)),
55 MK_FLOW_ITEM(IPV4, sizeof(struct rte_flow_item_ipv4)),
56 MK_FLOW_ITEM(IPV6, sizeof(struct rte_flow_item_ipv6)),
57 MK_FLOW_ITEM(ICMP, sizeof(struct rte_flow_item_icmp)),
58 MK_FLOW_ITEM(UDP, sizeof(struct rte_flow_item_udp)),
59 MK_FLOW_ITEM(TCP, sizeof(struct rte_flow_item_tcp)),
60 MK_FLOW_ITEM(SCTP, sizeof(struct rte_flow_item_sctp)),
61 MK_FLOW_ITEM(VXLAN, sizeof(struct rte_flow_item_vxlan)),
62 MK_FLOW_ITEM(E_TAG, sizeof(struct rte_flow_item_e_tag)),
63 MK_FLOW_ITEM(NVGRE, sizeof(struct rte_flow_item_nvgre)),
64 MK_FLOW_ITEM(MPLS, sizeof(struct rte_flow_item_mpls)),
65 MK_FLOW_ITEM(GRE, sizeof(struct rte_flow_item_gre)),
66 MK_FLOW_ITEM(FUZZY, sizeof(struct rte_flow_item_fuzzy)),
67 MK_FLOW_ITEM(GTP, sizeof(struct rte_flow_item_gtp)),
68 MK_FLOW_ITEM(GTPC, sizeof(struct rte_flow_item_gtp)),
69 MK_FLOW_ITEM(GTPU, sizeof(struct rte_flow_item_gtp)),
70 MK_FLOW_ITEM(ESP, sizeof(struct rte_flow_item_esp)),
71 MK_FLOW_ITEM(GENEVE, sizeof(struct rte_flow_item_geneve)),
72 MK_FLOW_ITEM(VXLAN_GPE, sizeof(struct rte_flow_item_vxlan_gpe)),
73 MK_FLOW_ITEM(ARP_ETH_IPV4, sizeof(struct rte_flow_item_arp_eth_ipv4)),
74 MK_FLOW_ITEM(IPV6_EXT, sizeof(struct rte_flow_item_ipv6_ext)),
75 MK_FLOW_ITEM(ICMP6, sizeof(struct rte_flow_item_icmp6)),
76 MK_FLOW_ITEM(ICMP6_ND_NS, sizeof(struct rte_flow_item_icmp6_nd_ns)),
77 MK_FLOW_ITEM(ICMP6_ND_NA, sizeof(struct rte_flow_item_icmp6_nd_na)),
78 MK_FLOW_ITEM(ICMP6_ND_OPT, sizeof(struct rte_flow_item_icmp6_nd_opt)),
79 MK_FLOW_ITEM(ICMP6_ND_OPT_SLA_ETH,
80 sizeof(struct rte_flow_item_icmp6_nd_opt_sla_eth)),
81 MK_FLOW_ITEM(ICMP6_ND_OPT_TLA_ETH,
82 sizeof(struct rte_flow_item_icmp6_nd_opt_tla_eth)),
83 MK_FLOW_ITEM(MARK, sizeof(struct rte_flow_item_mark)),
84 MK_FLOW_ITEM(META, sizeof(struct rte_flow_item_meta)),
85 MK_FLOW_ITEM(TAG, sizeof(struct rte_flow_item_tag)),
86 MK_FLOW_ITEM(GRE_KEY, sizeof(rte_be32_t)),
87 MK_FLOW_ITEM(GTP_PSC, sizeof(struct rte_flow_item_gtp_psc)),
88 MK_FLOW_ITEM(PPPOES, sizeof(struct rte_flow_item_pppoe)),
89 MK_FLOW_ITEM(PPPOED, sizeof(struct rte_flow_item_pppoe)),
90 MK_FLOW_ITEM(PPPOE_PROTO_ID,
91 sizeof(struct rte_flow_item_pppoe_proto_id)),
92 MK_FLOW_ITEM(NSH, sizeof(struct rte_flow_item_nsh)),
93 MK_FLOW_ITEM(IGMP, sizeof(struct rte_flow_item_igmp)),
94 MK_FLOW_ITEM(AH, sizeof(struct rte_flow_item_ah)),
95 MK_FLOW_ITEM(HIGIG2, sizeof(struct rte_flow_item_higig2_hdr)),
98 /** Generate flow_action[] entry. */
99 #define MK_FLOW_ACTION(t, s) \
100 [RTE_FLOW_ACTION_TYPE_ ## t] = { \
105 /** Information about known flow actions. */
106 static const struct rte_flow_desc_data rte_flow_desc_action[] = {
107 MK_FLOW_ACTION(END, 0),
108 MK_FLOW_ACTION(VOID, 0),
109 MK_FLOW_ACTION(PASSTHRU, 0),
110 MK_FLOW_ACTION(JUMP, sizeof(struct rte_flow_action_jump)),
111 MK_FLOW_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
112 MK_FLOW_ACTION(FLAG, 0),
113 MK_FLOW_ACTION(QUEUE, sizeof(struct rte_flow_action_queue)),
114 MK_FLOW_ACTION(DROP, 0),
115 MK_FLOW_ACTION(COUNT, sizeof(struct rte_flow_action_count)),
116 MK_FLOW_ACTION(RSS, sizeof(struct rte_flow_action_rss)),
117 MK_FLOW_ACTION(PF, 0),
118 MK_FLOW_ACTION(VF, sizeof(struct rte_flow_action_vf)),
119 MK_FLOW_ACTION(PHY_PORT, sizeof(struct rte_flow_action_phy_port)),
120 MK_FLOW_ACTION(PORT_ID, sizeof(struct rte_flow_action_port_id)),
121 MK_FLOW_ACTION(METER, sizeof(struct rte_flow_action_meter)),
122 MK_FLOW_ACTION(SECURITY, sizeof(struct rte_flow_action_security)),
123 MK_FLOW_ACTION(OF_SET_MPLS_TTL,
124 sizeof(struct rte_flow_action_of_set_mpls_ttl)),
125 MK_FLOW_ACTION(OF_DEC_MPLS_TTL, 0),
126 MK_FLOW_ACTION(OF_SET_NW_TTL,
127 sizeof(struct rte_flow_action_of_set_nw_ttl)),
128 MK_FLOW_ACTION(OF_DEC_NW_TTL, 0),
129 MK_FLOW_ACTION(OF_COPY_TTL_OUT, 0),
130 MK_FLOW_ACTION(OF_COPY_TTL_IN, 0),
131 MK_FLOW_ACTION(OF_POP_VLAN, 0),
132 MK_FLOW_ACTION(OF_PUSH_VLAN,
133 sizeof(struct rte_flow_action_of_push_vlan)),
134 MK_FLOW_ACTION(OF_SET_VLAN_VID,
135 sizeof(struct rte_flow_action_of_set_vlan_vid)),
136 MK_FLOW_ACTION(OF_SET_VLAN_PCP,
137 sizeof(struct rte_flow_action_of_set_vlan_pcp)),
138 MK_FLOW_ACTION(OF_POP_MPLS,
139 sizeof(struct rte_flow_action_of_pop_mpls)),
140 MK_FLOW_ACTION(OF_PUSH_MPLS,
141 sizeof(struct rte_flow_action_of_push_mpls)),
142 MK_FLOW_ACTION(VXLAN_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
143 MK_FLOW_ACTION(VXLAN_DECAP, 0),
144 MK_FLOW_ACTION(NVGRE_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
145 MK_FLOW_ACTION(NVGRE_DECAP, 0),
146 MK_FLOW_ACTION(RAW_ENCAP, sizeof(struct rte_flow_action_raw_encap)),
147 MK_FLOW_ACTION(RAW_DECAP, sizeof(struct rte_flow_action_raw_decap)),
148 MK_FLOW_ACTION(SET_IPV4_SRC,
149 sizeof(struct rte_flow_action_set_ipv4)),
150 MK_FLOW_ACTION(SET_IPV4_DST,
151 sizeof(struct rte_flow_action_set_ipv4)),
152 MK_FLOW_ACTION(SET_IPV6_SRC,
153 sizeof(struct rte_flow_action_set_ipv6)),
154 MK_FLOW_ACTION(SET_IPV6_DST,
155 sizeof(struct rte_flow_action_set_ipv6)),
156 MK_FLOW_ACTION(SET_TP_SRC,
157 sizeof(struct rte_flow_action_set_tp)),
158 MK_FLOW_ACTION(SET_TP_DST,
159 sizeof(struct rte_flow_action_set_tp)),
160 MK_FLOW_ACTION(MAC_SWAP, 0),
161 MK_FLOW_ACTION(DEC_TTL, 0),
162 MK_FLOW_ACTION(SET_TTL, sizeof(struct rte_flow_action_set_ttl)),
163 MK_FLOW_ACTION(SET_MAC_SRC, sizeof(struct rte_flow_action_set_mac)),
164 MK_FLOW_ACTION(SET_MAC_DST, sizeof(struct rte_flow_action_set_mac)),
165 MK_FLOW_ACTION(INC_TCP_SEQ, sizeof(rte_be32_t)),
166 MK_FLOW_ACTION(DEC_TCP_SEQ, sizeof(rte_be32_t)),
167 MK_FLOW_ACTION(INC_TCP_ACK, sizeof(rte_be32_t)),
168 MK_FLOW_ACTION(DEC_TCP_ACK, sizeof(rte_be32_t)),
169 MK_FLOW_ACTION(SET_TAG, sizeof(struct rte_flow_action_set_tag)),
170 MK_FLOW_ACTION(SET_META, sizeof(struct rte_flow_action_set_meta)),
174 rte_flow_dynf_metadata_register(void)
179 static const struct rte_mbuf_dynfield desc_offs = {
180 .name = RTE_MBUF_DYNFIELD_METADATA_NAME,
181 .size = sizeof(uint32_t),
182 .align = __alignof__(uint32_t),
184 static const struct rte_mbuf_dynflag desc_flag = {
185 .name = RTE_MBUF_DYNFLAG_METADATA_NAME,
188 offset = rte_mbuf_dynfield_register(&desc_offs);
191 flag = rte_mbuf_dynflag_register(&desc_flag);
194 rte_flow_dynf_metadata_offs = offset;
195 rte_flow_dynf_metadata_mask = (1ULL << flag);
199 rte_flow_dynf_metadata_offs = -1;
200 rte_flow_dynf_metadata_mask = 0ULL;
205 flow_err(uint16_t port_id, int ret, struct rte_flow_error *error)
209 if (rte_eth_dev_is_removed(port_id))
210 return rte_flow_error_set(error, EIO,
211 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
212 NULL, rte_strerror(EIO));
216 /* Get generic flow operations structure from a port. */
217 const struct rte_flow_ops *
218 rte_flow_ops_get(uint16_t port_id, struct rte_flow_error *error)
220 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
221 const struct rte_flow_ops *ops;
224 if (unlikely(!rte_eth_dev_is_valid_port(port_id)))
226 else if (unlikely(!dev->dev_ops->filter_ctrl ||
227 dev->dev_ops->filter_ctrl(dev,
228 RTE_ETH_FILTER_GENERIC,
235 rte_flow_error_set(error, code, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
236 NULL, rte_strerror(code));
240 /* Check whether a flow rule can be created on a given port. */
242 rte_flow_validate(uint16_t port_id,
243 const struct rte_flow_attr *attr,
244 const struct rte_flow_item pattern[],
245 const struct rte_flow_action actions[],
246 struct rte_flow_error *error)
248 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
249 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
253 if (likely(!!ops->validate))
254 return flow_err(port_id, ops->validate(dev, attr, pattern,
255 actions, error), error);
256 return rte_flow_error_set(error, ENOSYS,
257 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
258 NULL, rte_strerror(ENOSYS));
261 /* Create a flow rule on a given port. */
263 rte_flow_create(uint16_t port_id,
264 const struct rte_flow_attr *attr,
265 const struct rte_flow_item pattern[],
266 const struct rte_flow_action actions[],
267 struct rte_flow_error *error)
269 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
270 struct rte_flow *flow;
271 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
275 if (likely(!!ops->create)) {
276 flow = ops->create(dev, attr, pattern, actions, error);
278 flow_err(port_id, -rte_errno, error);
281 rte_flow_error_set(error, ENOSYS, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
282 NULL, rte_strerror(ENOSYS));
286 /* Destroy a flow rule on a given port. */
288 rte_flow_destroy(uint16_t port_id,
289 struct rte_flow *flow,
290 struct rte_flow_error *error)
292 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
293 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
297 if (likely(!!ops->destroy))
298 return flow_err(port_id, ops->destroy(dev, flow, error),
300 return rte_flow_error_set(error, ENOSYS,
301 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
302 NULL, rte_strerror(ENOSYS));
305 /* Destroy all flow rules associated with a port. */
307 rte_flow_flush(uint16_t port_id,
308 struct rte_flow_error *error)
310 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
311 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
315 if (likely(!!ops->flush))
316 return flow_err(port_id, ops->flush(dev, error), error);
317 return rte_flow_error_set(error, ENOSYS,
318 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
319 NULL, rte_strerror(ENOSYS));
322 /* Query an existing flow rule. */
324 rte_flow_query(uint16_t port_id,
325 struct rte_flow *flow,
326 const struct rte_flow_action *action,
328 struct rte_flow_error *error)
330 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
331 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
335 if (likely(!!ops->query))
336 return flow_err(port_id, ops->query(dev, flow, action, data,
338 return rte_flow_error_set(error, ENOSYS,
339 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
340 NULL, rte_strerror(ENOSYS));
343 /* Restrict ingress traffic to the defined flow rules. */
345 rte_flow_isolate(uint16_t port_id,
347 struct rte_flow_error *error)
349 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
350 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
354 if (likely(!!ops->isolate))
355 return flow_err(port_id, ops->isolate(dev, set, error), error);
356 return rte_flow_error_set(error, ENOSYS,
357 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
358 NULL, rte_strerror(ENOSYS));
361 /* Initialize flow error structure. */
363 rte_flow_error_set(struct rte_flow_error *error,
365 enum rte_flow_error_type type,
370 *error = (struct rte_flow_error){
380 /** Pattern item specification types. */
381 enum rte_flow_conv_item_spec_type {
382 RTE_FLOW_CONV_ITEM_SPEC,
383 RTE_FLOW_CONV_ITEM_LAST,
384 RTE_FLOW_CONV_ITEM_MASK,
388 * Copy pattern item specification.
391 * Output buffer. Can be NULL if @p size is zero.
393 * Size of @p buf in bytes.
395 * Pattern item to copy specification from.
397 * Specification selector for either @p spec, @p last or @p mask.
400 * Number of bytes needed to store pattern item specification regardless
401 * of @p size. @p buf contents are truncated to @p size if not large
405 rte_flow_conv_item_spec(void *buf, const size_t size,
406 const struct rte_flow_item *item,
407 enum rte_flow_conv_item_spec_type type)
411 type == RTE_FLOW_CONV_ITEM_SPEC ? item->spec :
412 type == RTE_FLOW_CONV_ITEM_LAST ? item->last :
413 type == RTE_FLOW_CONV_ITEM_MASK ? item->mask :
416 switch (item->type) {
418 const struct rte_flow_item_raw *raw;
421 const struct rte_flow_item_raw *raw;
424 const struct rte_flow_item_raw *raw;
427 const struct rte_flow_item_raw *raw;
430 struct rte_flow_item_raw *raw;
434 case RTE_FLOW_ITEM_TYPE_RAW:
435 spec.raw = item->spec;
436 last.raw = item->last ? item->last : item->spec;
437 mask.raw = item->mask ? item->mask : &rte_flow_item_raw_mask;
441 (&(struct rte_flow_item_raw){
442 .relative = src.raw->relative,
443 .search = src.raw->search,
444 .reserved = src.raw->reserved,
445 .offset = src.raw->offset,
446 .limit = src.raw->limit,
447 .length = src.raw->length,
449 size > sizeof(*dst.raw) ? sizeof(*dst.raw) : size);
450 off = sizeof(*dst.raw);
451 if (type == RTE_FLOW_CONV_ITEM_SPEC ||
452 (type == RTE_FLOW_CONV_ITEM_MASK &&
453 ((spec.raw->length & mask.raw->length) >=
454 (last.raw->length & mask.raw->length))))
455 tmp = spec.raw->length & mask.raw->length;
457 tmp = last.raw->length & mask.raw->length;
459 off = RTE_ALIGN_CEIL(off, sizeof(*dst.raw->pattern));
460 if (size >= off + tmp)
461 dst.raw->pattern = rte_memcpy
462 ((void *)((uintptr_t)dst.raw + off),
463 src.raw->pattern, tmp);
468 off = rte_flow_desc_item[item->type].size;
469 rte_memcpy(buf, data, (size > off ? off : size));
476 * Copy action configuration.
479 * Output buffer. Can be NULL if @p size is zero.
481 * Size of @p buf in bytes.
483 * Action to copy configuration from.
486 * Number of bytes needed to store pattern item specification regardless
487 * of @p size. @p buf contents are truncated to @p size if not large
491 rte_flow_conv_action_conf(void *buf, const size_t size,
492 const struct rte_flow_action *action)
496 switch (action->type) {
498 const struct rte_flow_action_rss *rss;
499 const struct rte_flow_action_vxlan_encap *vxlan_encap;
500 const struct rte_flow_action_nvgre_encap *nvgre_encap;
503 struct rte_flow_action_rss *rss;
504 struct rte_flow_action_vxlan_encap *vxlan_encap;
505 struct rte_flow_action_nvgre_encap *nvgre_encap;
510 case RTE_FLOW_ACTION_TYPE_RSS:
511 src.rss = action->conf;
514 (&(struct rte_flow_action_rss){
515 .func = src.rss->func,
516 .level = src.rss->level,
517 .types = src.rss->types,
518 .key_len = src.rss->key_len,
519 .queue_num = src.rss->queue_num,
521 size > sizeof(*dst.rss) ? sizeof(*dst.rss) : size);
522 off = sizeof(*dst.rss);
523 if (src.rss->key_len) {
524 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->key));
525 tmp = sizeof(*src.rss->key) * src.rss->key_len;
526 if (size >= off + tmp)
527 dst.rss->key = rte_memcpy
528 ((void *)((uintptr_t)dst.rss + off),
532 if (src.rss->queue_num) {
533 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->queue));
534 tmp = sizeof(*src.rss->queue) * src.rss->queue_num;
535 if (size >= off + tmp)
536 dst.rss->queue = rte_memcpy
537 ((void *)((uintptr_t)dst.rss + off),
538 src.rss->queue, tmp);
542 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
543 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
544 src.vxlan_encap = action->conf;
545 dst.vxlan_encap = buf;
546 RTE_BUILD_BUG_ON(sizeof(*src.vxlan_encap) !=
547 sizeof(*src.nvgre_encap) ||
548 offsetof(struct rte_flow_action_vxlan_encap,
550 offsetof(struct rte_flow_action_nvgre_encap,
552 off = sizeof(*dst.vxlan_encap);
553 if (src.vxlan_encap->definition) {
555 (off, sizeof(*dst.vxlan_encap->definition));
557 (RTE_FLOW_CONV_OP_PATTERN,
558 (void *)((uintptr_t)dst.vxlan_encap + off),
559 size > off ? size - off : 0,
560 src.vxlan_encap->definition, NULL);
563 if (size >= off + ret)
564 dst.vxlan_encap->definition =
565 (void *)((uintptr_t)dst.vxlan_encap +
571 off = rte_flow_desc_action[action->type].size;
572 rte_memcpy(buf, action->conf, (size > off ? off : size));
579 * Copy a list of pattern items.
582 * Destination buffer. Can be NULL if @p size is zero.
584 * Size of @p dst in bytes.
586 * Source pattern items.
588 * Maximum number of pattern items to process from @p src or 0 to process
589 * the entire list. In both cases, processing stops after
590 * RTE_FLOW_ITEM_TYPE_END is encountered.
592 * Perform verbose error reporting if not NULL.
595 * A positive value representing the number of bytes needed to store
596 * pattern items regardless of @p size on success (@p buf contents are
597 * truncated to @p size if not large enough), a negative errno value
598 * otherwise and rte_errno is set.
601 rte_flow_conv_pattern(struct rte_flow_item *dst,
603 const struct rte_flow_item *src,
605 struct rte_flow_error *error)
607 uintptr_t data = (uintptr_t)dst;
612 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
613 if ((size_t)src->type >= RTE_DIM(rte_flow_desc_item) ||
614 !rte_flow_desc_item[src->type].name)
615 return rte_flow_error_set
616 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, src,
617 "cannot convert unknown item type");
618 if (size >= off + sizeof(*dst))
619 *dst = (struct rte_flow_item){
631 off = RTE_ALIGN_CEIL(off, sizeof(double));
632 ret = rte_flow_conv_item_spec
633 ((void *)(data + off),
634 size > off ? size - off : 0, src,
635 RTE_FLOW_CONV_ITEM_SPEC);
636 if (size && size >= off + ret)
637 dst->spec = (void *)(data + off);
642 off = RTE_ALIGN_CEIL(off, sizeof(double));
643 ret = rte_flow_conv_item_spec
644 ((void *)(data + off),
645 size > off ? size - off : 0, src,
646 RTE_FLOW_CONV_ITEM_LAST);
647 if (size && size >= off + ret)
648 dst->last = (void *)(data + off);
652 off = RTE_ALIGN_CEIL(off, sizeof(double));
653 ret = rte_flow_conv_item_spec
654 ((void *)(data + off),
655 size > off ? size - off : 0, src,
656 RTE_FLOW_CONV_ITEM_MASK);
657 if (size && size >= off + ret)
658 dst->mask = (void *)(data + off);
668 * Copy a list of actions.
671 * Destination buffer. Can be NULL if @p size is zero.
673 * Size of @p dst in bytes.
677 * Maximum number of actions to process from @p src or 0 to process the
678 * entire list. In both cases, processing stops after
679 * RTE_FLOW_ACTION_TYPE_END is encountered.
681 * Perform verbose error reporting if not NULL.
684 * A positive value representing the number of bytes needed to store
685 * actions regardless of @p size on success (@p buf contents are truncated
686 * to @p size if not large enough), a negative errno value otherwise and
690 rte_flow_conv_actions(struct rte_flow_action *dst,
692 const struct rte_flow_action *src,
694 struct rte_flow_error *error)
696 uintptr_t data = (uintptr_t)dst;
701 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
702 if ((size_t)src->type >= RTE_DIM(rte_flow_desc_action) ||
703 !rte_flow_desc_action[src->type].name)
704 return rte_flow_error_set
705 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
706 src, "cannot convert unknown action type");
707 if (size >= off + sizeof(*dst))
708 *dst = (struct rte_flow_action){
720 off = RTE_ALIGN_CEIL(off, sizeof(double));
721 ret = rte_flow_conv_action_conf
722 ((void *)(data + off),
723 size > off ? size - off : 0, src);
724 if (size && size >= off + ret)
725 dst->conf = (void *)(data + off);
735 * Copy flow rule components.
737 * This comprises the flow rule descriptor itself, attributes, pattern and
738 * actions list. NULL components in @p src are skipped.
741 * Destination buffer. Can be NULL if @p size is zero.
743 * Size of @p dst in bytes.
745 * Source flow rule descriptor.
747 * Perform verbose error reporting if not NULL.
750 * A positive value representing the number of bytes needed to store all
751 * components including the descriptor regardless of @p size on success
752 * (@p buf contents are truncated to @p size if not large enough), a
753 * negative errno value otherwise and rte_errno is set.
756 rte_flow_conv_rule(struct rte_flow_conv_rule *dst,
758 const struct rte_flow_conv_rule *src,
759 struct rte_flow_error *error)
765 (&(struct rte_flow_conv_rule){
770 size > sizeof(*dst) ? sizeof(*dst) : size);
773 off = RTE_ALIGN_CEIL(off, sizeof(double));
774 if (size && size >= off + sizeof(*dst->attr))
775 dst->attr = rte_memcpy
776 ((void *)((uintptr_t)dst + off),
777 src->attr_ro, sizeof(*dst->attr));
778 off += sizeof(*dst->attr);
780 if (src->pattern_ro) {
781 off = RTE_ALIGN_CEIL(off, sizeof(double));
782 ret = rte_flow_conv_pattern((void *)((uintptr_t)dst + off),
783 size > off ? size - off : 0,
784 src->pattern_ro, 0, error);
787 if (size && size >= off + (size_t)ret)
788 dst->pattern = (void *)((uintptr_t)dst + off);
791 if (src->actions_ro) {
792 off = RTE_ALIGN_CEIL(off, sizeof(double));
793 ret = rte_flow_conv_actions((void *)((uintptr_t)dst + off),
794 size > off ? size - off : 0,
795 src->actions_ro, 0, error);
798 if (size >= off + (size_t)ret)
799 dst->actions = (void *)((uintptr_t)dst + off);
806 * Retrieve the name of a pattern item/action type.
809 * Nonzero when @p src represents an action type instead of a pattern item
812 * Nonzero to write string address instead of contents into @p dst.
814 * Destination buffer. Can be NULL if @p size is zero.
816 * Size of @p dst in bytes.
818 * Depending on @p is_action, source pattern item or action type cast as a
821 * Perform verbose error reporting if not NULL.
824 * A positive value representing the number of bytes needed to store the
825 * name or its address regardless of @p size on success (@p buf contents
826 * are truncated to @p size if not large enough), a negative errno value
827 * otherwise and rte_errno is set.
830 rte_flow_conv_name(int is_action,
835 struct rte_flow_error *error)
838 const struct rte_flow_desc_data *data;
841 static const struct desc_info info_rep[2] = {
842 { rte_flow_desc_item, RTE_DIM(rte_flow_desc_item), },
843 { rte_flow_desc_action, RTE_DIM(rte_flow_desc_action), },
845 const struct desc_info *const info = &info_rep[!!is_action];
846 unsigned int type = (uintptr_t)src;
848 if (type >= info->num)
849 return rte_flow_error_set
850 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
851 "unknown object type to retrieve the name of");
853 return strlcpy(dst, info->data[type].name, size);
854 if (size >= sizeof(const char **))
855 *((const char **)dst) = info->data[type].name;
856 return sizeof(const char **);
859 /** Helper function to convert flow API objects. */
861 rte_flow_conv(enum rte_flow_conv_op op,
865 struct rte_flow_error *error)
868 const struct rte_flow_attr *attr;
870 case RTE_FLOW_CONV_OP_NONE:
872 case RTE_FLOW_CONV_OP_ATTR:
874 if (size > sizeof(*attr))
875 size = sizeof(*attr);
876 rte_memcpy(dst, attr, size);
877 return sizeof(*attr);
878 case RTE_FLOW_CONV_OP_ITEM:
879 return rte_flow_conv_pattern(dst, size, src, 1, error);
880 case RTE_FLOW_CONV_OP_ACTION:
881 return rte_flow_conv_actions(dst, size, src, 1, error);
882 case RTE_FLOW_CONV_OP_PATTERN:
883 return rte_flow_conv_pattern(dst, size, src, 0, error);
884 case RTE_FLOW_CONV_OP_ACTIONS:
885 return rte_flow_conv_actions(dst, size, src, 0, error);
886 case RTE_FLOW_CONV_OP_RULE:
887 return rte_flow_conv_rule(dst, size, src, error);
888 case RTE_FLOW_CONV_OP_ITEM_NAME:
889 return rte_flow_conv_name(0, 0, dst, size, src, error);
890 case RTE_FLOW_CONV_OP_ACTION_NAME:
891 return rte_flow_conv_name(1, 0, dst, size, src, error);
892 case RTE_FLOW_CONV_OP_ITEM_NAME_PTR:
893 return rte_flow_conv_name(0, 1, dst, size, src, error);
894 case RTE_FLOW_CONV_OP_ACTION_NAME_PTR:
895 return rte_flow_conv_name(1, 1, dst, size, src, error);
897 return rte_flow_error_set
898 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
899 "unknown object conversion operation");
902 /** Store a full rte_flow description. */
904 rte_flow_copy(struct rte_flow_desc *desc, size_t len,
905 const struct rte_flow_attr *attr,
906 const struct rte_flow_item *items,
907 const struct rte_flow_action *actions)
910 * Overlap struct rte_flow_conv with struct rte_flow_desc in order
911 * to convert the former to the latter without wasting space.
913 struct rte_flow_conv_rule *dst =
915 (void *)((uintptr_t)desc +
916 (offsetof(struct rte_flow_desc, actions) -
917 offsetof(struct rte_flow_conv_rule, actions))) :
920 len > sizeof(*desc) - sizeof(*dst) ?
921 len - (sizeof(*desc) - sizeof(*dst)) :
923 struct rte_flow_conv_rule src = {
926 .actions_ro = actions,
930 RTE_BUILD_BUG_ON(sizeof(struct rte_flow_desc) <
931 sizeof(struct rte_flow_conv_rule));
933 (&dst->pattern != &desc->items ||
934 &dst->actions != &desc->actions ||
935 (uintptr_t)(dst + 1) != (uintptr_t)(desc + 1))) {
939 ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, dst, dst_size, &src, NULL);
942 ret += sizeof(*desc) - sizeof(*dst);
944 (&(struct rte_flow_desc){
947 .items = dst_size ? dst->pattern : NULL,
948 .actions = dst_size ? dst->actions : NULL,
950 len > sizeof(*desc) ? sizeof(*desc) : len);
955 * Expand RSS flows into several possible flows according to the RSS hash
956 * fields requested and the driver capabilities.
959 rte_flow_expand_rss(struct rte_flow_expand_rss *buf, size_t size,
960 const struct rte_flow_item *pattern, uint64_t types,
961 const struct rte_flow_expand_node graph[],
962 int graph_root_index)
965 const struct rte_flow_item *item;
966 const struct rte_flow_expand_node *node = &graph[graph_root_index];
967 const int *next_node;
968 const int *stack[elt_n];
970 struct rte_flow_item flow_items[elt_n];
973 size_t user_pattern_size = 0;
976 lsize = offsetof(struct rte_flow_expand_rss, entry) +
977 elt_n * sizeof(buf->entry[0]);
979 buf->entry[0].priority = 0;
980 buf->entry[0].pattern = (void *)&buf->entry[elt_n];
982 addr = buf->entry[0].pattern;
984 for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
985 const struct rte_flow_expand_node *next = NULL;
987 for (i = 0; node->next && node->next[i]; ++i) {
988 next = &graph[node->next[i]];
989 if (next->type == item->type)
994 user_pattern_size += sizeof(*item);
996 user_pattern_size += sizeof(*item); /* Handle END item. */
997 lsize += user_pattern_size;
998 /* Copy the user pattern in the first entry of the buffer. */
1000 rte_memcpy(addr, pattern, user_pattern_size);
1001 addr = (void *)(((uintptr_t)addr) + user_pattern_size);
1004 /* Start expanding. */
1005 memset(flow_items, 0, sizeof(flow_items));
1006 user_pattern_size -= sizeof(*item);
1007 next_node = node->next;
1008 stack[stack_pos] = next_node;
1009 node = next_node ? &graph[*next_node] : NULL;
1011 flow_items[stack_pos].type = node->type;
1012 if (node->rss_types & types) {
1014 * compute the number of items to copy from the
1015 * expansion and copy it.
1016 * When the stack_pos is 0, there are 1 element in it,
1017 * plus the addition END item.
1019 int elt = stack_pos + 2;
1021 flow_items[stack_pos + 1].type = RTE_FLOW_ITEM_TYPE_END;
1022 lsize += elt * sizeof(*item) + user_pattern_size;
1023 if (lsize <= size) {
1024 size_t n = elt * sizeof(*item);
1026 buf->entry[buf->entries].priority =
1028 buf->entry[buf->entries].pattern = addr;
1030 rte_memcpy(addr, buf->entry[0].pattern,
1032 addr = (void *)(((uintptr_t)addr) +
1034 rte_memcpy(addr, flow_items, n);
1035 addr = (void *)(((uintptr_t)addr) + n);
1040 next_node = node->next;
1041 if (stack_pos++ == elt_n) {
1045 stack[stack_pos] = next_node;
1046 } else if (*(next_node + 1)) {
1047 /* Follow up with the next possibility. */
1050 /* Move to the next path. */
1052 next_node = stack[--stack_pos];
1054 stack[stack_pos] = next_node;
1056 node = *next_node ? &graph[*next_node] : NULL;