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 int32_t 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)),
96 MK_FLOW_ITEM(L2TPV3OIP, sizeof(struct rte_flow_item_l2tpv3oip)),
97 MK_FLOW_ITEM(PFCP, sizeof(struct rte_flow_item_pfcp)),
98 MK_FLOW_ITEM(ECPRI, sizeof(struct rte_flow_item_ecpri)),
101 /** Generate flow_action[] entry. */
102 #define MK_FLOW_ACTION(t, s) \
103 [RTE_FLOW_ACTION_TYPE_ ## t] = { \
108 /** Information about known flow actions. */
109 static const struct rte_flow_desc_data rte_flow_desc_action[] = {
110 MK_FLOW_ACTION(END, 0),
111 MK_FLOW_ACTION(VOID, 0),
112 MK_FLOW_ACTION(PASSTHRU, 0),
113 MK_FLOW_ACTION(JUMP, sizeof(struct rte_flow_action_jump)),
114 MK_FLOW_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
115 MK_FLOW_ACTION(FLAG, 0),
116 MK_FLOW_ACTION(QUEUE, sizeof(struct rte_flow_action_queue)),
117 MK_FLOW_ACTION(DROP, 0),
118 MK_FLOW_ACTION(COUNT, sizeof(struct rte_flow_action_count)),
119 MK_FLOW_ACTION(RSS, sizeof(struct rte_flow_action_rss)),
120 MK_FLOW_ACTION(PF, 0),
121 MK_FLOW_ACTION(VF, sizeof(struct rte_flow_action_vf)),
122 MK_FLOW_ACTION(PHY_PORT, sizeof(struct rte_flow_action_phy_port)),
123 MK_FLOW_ACTION(PORT_ID, sizeof(struct rte_flow_action_port_id)),
124 MK_FLOW_ACTION(METER, sizeof(struct rte_flow_action_meter)),
125 MK_FLOW_ACTION(SECURITY, sizeof(struct rte_flow_action_security)),
126 MK_FLOW_ACTION(OF_SET_MPLS_TTL,
127 sizeof(struct rte_flow_action_of_set_mpls_ttl)),
128 MK_FLOW_ACTION(OF_DEC_MPLS_TTL, 0),
129 MK_FLOW_ACTION(OF_SET_NW_TTL,
130 sizeof(struct rte_flow_action_of_set_nw_ttl)),
131 MK_FLOW_ACTION(OF_DEC_NW_TTL, 0),
132 MK_FLOW_ACTION(OF_COPY_TTL_OUT, 0),
133 MK_FLOW_ACTION(OF_COPY_TTL_IN, 0),
134 MK_FLOW_ACTION(OF_POP_VLAN, 0),
135 MK_FLOW_ACTION(OF_PUSH_VLAN,
136 sizeof(struct rte_flow_action_of_push_vlan)),
137 MK_FLOW_ACTION(OF_SET_VLAN_VID,
138 sizeof(struct rte_flow_action_of_set_vlan_vid)),
139 MK_FLOW_ACTION(OF_SET_VLAN_PCP,
140 sizeof(struct rte_flow_action_of_set_vlan_pcp)),
141 MK_FLOW_ACTION(OF_POP_MPLS,
142 sizeof(struct rte_flow_action_of_pop_mpls)),
143 MK_FLOW_ACTION(OF_PUSH_MPLS,
144 sizeof(struct rte_flow_action_of_push_mpls)),
145 MK_FLOW_ACTION(VXLAN_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
146 MK_FLOW_ACTION(VXLAN_DECAP, 0),
147 MK_FLOW_ACTION(NVGRE_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
148 MK_FLOW_ACTION(NVGRE_DECAP, 0),
149 MK_FLOW_ACTION(RAW_ENCAP, sizeof(struct rte_flow_action_raw_encap)),
150 MK_FLOW_ACTION(RAW_DECAP, sizeof(struct rte_flow_action_raw_decap)),
151 MK_FLOW_ACTION(SET_IPV4_SRC,
152 sizeof(struct rte_flow_action_set_ipv4)),
153 MK_FLOW_ACTION(SET_IPV4_DST,
154 sizeof(struct rte_flow_action_set_ipv4)),
155 MK_FLOW_ACTION(SET_IPV6_SRC,
156 sizeof(struct rte_flow_action_set_ipv6)),
157 MK_FLOW_ACTION(SET_IPV6_DST,
158 sizeof(struct rte_flow_action_set_ipv6)),
159 MK_FLOW_ACTION(SET_TP_SRC,
160 sizeof(struct rte_flow_action_set_tp)),
161 MK_FLOW_ACTION(SET_TP_DST,
162 sizeof(struct rte_flow_action_set_tp)),
163 MK_FLOW_ACTION(MAC_SWAP, 0),
164 MK_FLOW_ACTION(DEC_TTL, 0),
165 MK_FLOW_ACTION(SET_TTL, sizeof(struct rte_flow_action_set_ttl)),
166 MK_FLOW_ACTION(SET_MAC_SRC, sizeof(struct rte_flow_action_set_mac)),
167 MK_FLOW_ACTION(SET_MAC_DST, sizeof(struct rte_flow_action_set_mac)),
168 MK_FLOW_ACTION(INC_TCP_SEQ, sizeof(rte_be32_t)),
169 MK_FLOW_ACTION(DEC_TCP_SEQ, sizeof(rte_be32_t)),
170 MK_FLOW_ACTION(INC_TCP_ACK, sizeof(rte_be32_t)),
171 MK_FLOW_ACTION(DEC_TCP_ACK, sizeof(rte_be32_t)),
172 MK_FLOW_ACTION(SET_TAG, sizeof(struct rte_flow_action_set_tag)),
173 MK_FLOW_ACTION(SET_META, sizeof(struct rte_flow_action_set_meta)),
174 MK_FLOW_ACTION(SET_IPV4_DSCP, sizeof(struct rte_flow_action_set_dscp)),
175 MK_FLOW_ACTION(SET_IPV6_DSCP, sizeof(struct rte_flow_action_set_dscp)),
176 MK_FLOW_ACTION(AGE, sizeof(struct rte_flow_action_age)),
177 MK_FLOW_ACTION(SAMPLE, sizeof(struct rte_flow_action_sample)),
181 rte_flow_dynf_metadata_register(void)
186 static const struct rte_mbuf_dynfield desc_offs = {
187 .name = RTE_MBUF_DYNFIELD_METADATA_NAME,
188 .size = sizeof(uint32_t),
189 .align = __alignof__(uint32_t),
191 static const struct rte_mbuf_dynflag desc_flag = {
192 .name = RTE_MBUF_DYNFLAG_METADATA_NAME,
195 offset = rte_mbuf_dynfield_register(&desc_offs);
198 flag = rte_mbuf_dynflag_register(&desc_flag);
201 rte_flow_dynf_metadata_offs = offset;
202 rte_flow_dynf_metadata_mask = (1ULL << flag);
206 rte_flow_dynf_metadata_offs = -1;
207 rte_flow_dynf_metadata_mask = 0ULL;
212 fts_enter(struct rte_eth_dev *dev)
214 if (!(dev->data->dev_flags & RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE))
215 pthread_mutex_lock(&dev->data->flow_ops_mutex);
219 fts_exit(struct rte_eth_dev *dev)
221 if (!(dev->data->dev_flags & RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE))
222 pthread_mutex_unlock(&dev->data->flow_ops_mutex);
226 flow_err(uint16_t port_id, int ret, struct rte_flow_error *error)
230 if (rte_eth_dev_is_removed(port_id))
231 return rte_flow_error_set(error, EIO,
232 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
233 NULL, rte_strerror(EIO));
237 /* Get generic flow operations structure from a port. */
238 const struct rte_flow_ops *
239 rte_flow_ops_get(uint16_t port_id, struct rte_flow_error *error)
241 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
242 const struct rte_flow_ops *ops;
245 if (unlikely(!rte_eth_dev_is_valid_port(port_id)))
247 else if (unlikely(!dev->dev_ops->filter_ctrl ||
248 dev->dev_ops->filter_ctrl(dev,
249 RTE_ETH_FILTER_GENERIC,
256 rte_flow_error_set(error, code, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
257 NULL, rte_strerror(code));
261 /* Check whether a flow rule can be created on a given port. */
263 rte_flow_validate(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 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
270 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
275 if (likely(!!ops->validate)) {
277 ret = ops->validate(dev, attr, pattern, actions, error);
279 return flow_err(port_id, ret, error);
281 return rte_flow_error_set(error, ENOSYS,
282 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
283 NULL, rte_strerror(ENOSYS));
286 /* Create a flow rule on a given port. */
288 rte_flow_create(uint16_t port_id,
289 const struct rte_flow_attr *attr,
290 const struct rte_flow_item pattern[],
291 const struct rte_flow_action actions[],
292 struct rte_flow_error *error)
294 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
295 struct rte_flow *flow;
296 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
300 if (likely(!!ops->create)) {
302 flow = ops->create(dev, attr, pattern, actions, error);
305 flow_err(port_id, -rte_errno, error);
308 rte_flow_error_set(error, ENOSYS, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
309 NULL, rte_strerror(ENOSYS));
313 /* Destroy a flow rule on a given port. */
315 rte_flow_destroy(uint16_t port_id,
316 struct rte_flow *flow,
317 struct rte_flow_error *error)
319 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
320 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
325 if (likely(!!ops->destroy)) {
327 ret = ops->destroy(dev, flow, error);
329 return flow_err(port_id, ret, error);
331 return rte_flow_error_set(error, ENOSYS,
332 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
333 NULL, rte_strerror(ENOSYS));
336 /* Destroy all flow rules associated with a port. */
338 rte_flow_flush(uint16_t port_id,
339 struct rte_flow_error *error)
341 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
342 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
347 if (likely(!!ops->flush)) {
349 ret = ops->flush(dev, error);
351 return flow_err(port_id, ret, error);
353 return rte_flow_error_set(error, ENOSYS,
354 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
355 NULL, rte_strerror(ENOSYS));
358 /* Query an existing flow rule. */
360 rte_flow_query(uint16_t port_id,
361 struct rte_flow *flow,
362 const struct rte_flow_action *action,
364 struct rte_flow_error *error)
366 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
367 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
372 if (likely(!!ops->query)) {
374 ret = ops->query(dev, flow, action, data, error);
376 return flow_err(port_id, ret, error);
378 return rte_flow_error_set(error, ENOSYS,
379 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
380 NULL, rte_strerror(ENOSYS));
383 /* Restrict ingress traffic to the defined flow rules. */
385 rte_flow_isolate(uint16_t port_id,
387 struct rte_flow_error *error)
389 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
390 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
395 if (likely(!!ops->isolate)) {
397 ret = ops->isolate(dev, set, error);
399 return flow_err(port_id, ret, error);
401 return rte_flow_error_set(error, ENOSYS,
402 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
403 NULL, rte_strerror(ENOSYS));
406 /* Initialize flow error structure. */
408 rte_flow_error_set(struct rte_flow_error *error,
410 enum rte_flow_error_type type,
415 *error = (struct rte_flow_error){
425 /** Pattern item specification types. */
426 enum rte_flow_conv_item_spec_type {
427 RTE_FLOW_CONV_ITEM_SPEC,
428 RTE_FLOW_CONV_ITEM_LAST,
429 RTE_FLOW_CONV_ITEM_MASK,
433 * Copy pattern item specification.
436 * Output buffer. Can be NULL if @p size is zero.
438 * Size of @p buf in bytes.
440 * Pattern item to copy specification from.
442 * Specification selector for either @p spec, @p last or @p mask.
445 * Number of bytes needed to store pattern item specification regardless
446 * of @p size. @p buf contents are truncated to @p size if not large
450 rte_flow_conv_item_spec(void *buf, const size_t size,
451 const struct rte_flow_item *item,
452 enum rte_flow_conv_item_spec_type type)
456 type == RTE_FLOW_CONV_ITEM_SPEC ? item->spec :
457 type == RTE_FLOW_CONV_ITEM_LAST ? item->last :
458 type == RTE_FLOW_CONV_ITEM_MASK ? item->mask :
461 switch (item->type) {
463 const struct rte_flow_item_raw *raw;
466 const struct rte_flow_item_raw *raw;
469 const struct rte_flow_item_raw *raw;
472 const struct rte_flow_item_raw *raw;
475 struct rte_flow_item_raw *raw;
479 case RTE_FLOW_ITEM_TYPE_RAW:
480 spec.raw = item->spec;
481 last.raw = item->last ? item->last : item->spec;
482 mask.raw = item->mask ? item->mask : &rte_flow_item_raw_mask;
486 (&(struct rte_flow_item_raw){
487 .relative = src.raw->relative,
488 .search = src.raw->search,
489 .reserved = src.raw->reserved,
490 .offset = src.raw->offset,
491 .limit = src.raw->limit,
492 .length = src.raw->length,
494 size > sizeof(*dst.raw) ? sizeof(*dst.raw) : size);
495 off = sizeof(*dst.raw);
496 if (type == RTE_FLOW_CONV_ITEM_SPEC ||
497 (type == RTE_FLOW_CONV_ITEM_MASK &&
498 ((spec.raw->length & mask.raw->length) >=
499 (last.raw->length & mask.raw->length))))
500 tmp = spec.raw->length & mask.raw->length;
502 tmp = last.raw->length & mask.raw->length;
504 off = RTE_ALIGN_CEIL(off, sizeof(*dst.raw->pattern));
505 if (size >= off + tmp)
506 dst.raw->pattern = rte_memcpy
507 ((void *)((uintptr_t)dst.raw + off),
508 src.raw->pattern, tmp);
513 off = rte_flow_desc_item[item->type].size;
514 rte_memcpy(buf, data, (size > off ? off : size));
521 * Copy action configuration.
524 * Output buffer. Can be NULL if @p size is zero.
526 * Size of @p buf in bytes.
528 * Action to copy configuration from.
531 * Number of bytes needed to store pattern item specification regardless
532 * of @p size. @p buf contents are truncated to @p size if not large
536 rte_flow_conv_action_conf(void *buf, const size_t size,
537 const struct rte_flow_action *action)
541 switch (action->type) {
543 const struct rte_flow_action_rss *rss;
544 const struct rte_flow_action_vxlan_encap *vxlan_encap;
545 const struct rte_flow_action_nvgre_encap *nvgre_encap;
548 struct rte_flow_action_rss *rss;
549 struct rte_flow_action_vxlan_encap *vxlan_encap;
550 struct rte_flow_action_nvgre_encap *nvgre_encap;
555 case RTE_FLOW_ACTION_TYPE_RSS:
556 src.rss = action->conf;
559 (&(struct rte_flow_action_rss){
560 .func = src.rss->func,
561 .level = src.rss->level,
562 .types = src.rss->types,
563 .key_len = src.rss->key_len,
564 .queue_num = src.rss->queue_num,
566 size > sizeof(*dst.rss) ? sizeof(*dst.rss) : size);
567 off = sizeof(*dst.rss);
568 if (src.rss->key_len) {
569 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->key));
570 tmp = sizeof(*src.rss->key) * src.rss->key_len;
571 if (size >= off + tmp)
572 dst.rss->key = rte_memcpy
573 ((void *)((uintptr_t)dst.rss + off),
577 if (src.rss->queue_num) {
578 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->queue));
579 tmp = sizeof(*src.rss->queue) * src.rss->queue_num;
580 if (size >= off + tmp)
581 dst.rss->queue = rte_memcpy
582 ((void *)((uintptr_t)dst.rss + off),
583 src.rss->queue, tmp);
587 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
588 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
589 src.vxlan_encap = action->conf;
590 dst.vxlan_encap = buf;
591 RTE_BUILD_BUG_ON(sizeof(*src.vxlan_encap) !=
592 sizeof(*src.nvgre_encap) ||
593 offsetof(struct rte_flow_action_vxlan_encap,
595 offsetof(struct rte_flow_action_nvgre_encap,
597 off = sizeof(*dst.vxlan_encap);
598 if (src.vxlan_encap->definition) {
600 (off, sizeof(*dst.vxlan_encap->definition));
602 (RTE_FLOW_CONV_OP_PATTERN,
603 (void *)((uintptr_t)dst.vxlan_encap + off),
604 size > off ? size - off : 0,
605 src.vxlan_encap->definition, NULL);
608 if (size >= off + ret)
609 dst.vxlan_encap->definition =
610 (void *)((uintptr_t)dst.vxlan_encap +
616 off = rte_flow_desc_action[action->type].size;
617 rte_memcpy(buf, action->conf, (size > off ? off : size));
624 * Copy a list of pattern items.
627 * Destination buffer. Can be NULL if @p size is zero.
629 * Size of @p dst in bytes.
631 * Source pattern items.
633 * Maximum number of pattern items to process from @p src or 0 to process
634 * the entire list. In both cases, processing stops after
635 * RTE_FLOW_ITEM_TYPE_END is encountered.
637 * Perform verbose error reporting if not NULL.
640 * A positive value representing the number of bytes needed to store
641 * pattern items regardless of @p size on success (@p buf contents are
642 * truncated to @p size if not large enough), a negative errno value
643 * otherwise and rte_errno is set.
646 rte_flow_conv_pattern(struct rte_flow_item *dst,
648 const struct rte_flow_item *src,
650 struct rte_flow_error *error)
652 uintptr_t data = (uintptr_t)dst;
657 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
658 if ((size_t)src->type >= RTE_DIM(rte_flow_desc_item) ||
659 !rte_flow_desc_item[src->type].name)
660 return rte_flow_error_set
661 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, src,
662 "cannot convert unknown item type");
663 if (size >= off + sizeof(*dst))
664 *dst = (struct rte_flow_item){
676 off = RTE_ALIGN_CEIL(off, sizeof(double));
677 ret = rte_flow_conv_item_spec
678 ((void *)(data + off),
679 size > off ? size - off : 0, src,
680 RTE_FLOW_CONV_ITEM_SPEC);
681 if (size && size >= off + ret)
682 dst->spec = (void *)(data + off);
687 off = RTE_ALIGN_CEIL(off, sizeof(double));
688 ret = rte_flow_conv_item_spec
689 ((void *)(data + off),
690 size > off ? size - off : 0, src,
691 RTE_FLOW_CONV_ITEM_LAST);
692 if (size && size >= off + ret)
693 dst->last = (void *)(data + off);
697 off = RTE_ALIGN_CEIL(off, sizeof(double));
698 ret = rte_flow_conv_item_spec
699 ((void *)(data + off),
700 size > off ? size - off : 0, src,
701 RTE_FLOW_CONV_ITEM_MASK);
702 if (size && size >= off + ret)
703 dst->mask = (void *)(data + off);
713 * Copy a list of actions.
716 * Destination buffer. Can be NULL if @p size is zero.
718 * Size of @p dst in bytes.
722 * Maximum number of actions to process from @p src or 0 to process the
723 * entire list. In both cases, processing stops after
724 * RTE_FLOW_ACTION_TYPE_END is encountered.
726 * Perform verbose error reporting if not NULL.
729 * A positive value representing the number of bytes needed to store
730 * actions regardless of @p size on success (@p buf contents are truncated
731 * to @p size if not large enough), a negative errno value otherwise and
735 rte_flow_conv_actions(struct rte_flow_action *dst,
737 const struct rte_flow_action *src,
739 struct rte_flow_error *error)
741 uintptr_t data = (uintptr_t)dst;
746 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
747 if ((size_t)src->type >= RTE_DIM(rte_flow_desc_action) ||
748 !rte_flow_desc_action[src->type].name)
749 return rte_flow_error_set
750 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
751 src, "cannot convert unknown action type");
752 if (size >= off + sizeof(*dst))
753 *dst = (struct rte_flow_action){
765 off = RTE_ALIGN_CEIL(off, sizeof(double));
766 ret = rte_flow_conv_action_conf
767 ((void *)(data + off),
768 size > off ? size - off : 0, src);
769 if (size && size >= off + ret)
770 dst->conf = (void *)(data + off);
780 * Copy flow rule components.
782 * This comprises the flow rule descriptor itself, attributes, pattern and
783 * actions list. NULL components in @p src are skipped.
786 * Destination buffer. Can be NULL if @p size is zero.
788 * Size of @p dst in bytes.
790 * Source flow rule descriptor.
792 * Perform verbose error reporting if not NULL.
795 * A positive value representing the number of bytes needed to store all
796 * components including the descriptor regardless of @p size on success
797 * (@p buf contents are truncated to @p size if not large enough), a
798 * negative errno value otherwise and rte_errno is set.
801 rte_flow_conv_rule(struct rte_flow_conv_rule *dst,
803 const struct rte_flow_conv_rule *src,
804 struct rte_flow_error *error)
810 (&(struct rte_flow_conv_rule){
815 size > sizeof(*dst) ? sizeof(*dst) : size);
818 off = RTE_ALIGN_CEIL(off, sizeof(double));
819 if (size && size >= off + sizeof(*dst->attr))
820 dst->attr = rte_memcpy
821 ((void *)((uintptr_t)dst + off),
822 src->attr_ro, sizeof(*dst->attr));
823 off += sizeof(*dst->attr);
825 if (src->pattern_ro) {
826 off = RTE_ALIGN_CEIL(off, sizeof(double));
827 ret = rte_flow_conv_pattern((void *)((uintptr_t)dst + off),
828 size > off ? size - off : 0,
829 src->pattern_ro, 0, error);
832 if (size && size >= off + (size_t)ret)
833 dst->pattern = (void *)((uintptr_t)dst + off);
836 if (src->actions_ro) {
837 off = RTE_ALIGN_CEIL(off, sizeof(double));
838 ret = rte_flow_conv_actions((void *)((uintptr_t)dst + off),
839 size > off ? size - off : 0,
840 src->actions_ro, 0, error);
843 if (size >= off + (size_t)ret)
844 dst->actions = (void *)((uintptr_t)dst + off);
851 * Retrieve the name of a pattern item/action type.
854 * Nonzero when @p src represents an action type instead of a pattern item
857 * Nonzero to write string address instead of contents into @p dst.
859 * Destination buffer. Can be NULL if @p size is zero.
861 * Size of @p dst in bytes.
863 * Depending on @p is_action, source pattern item or action type cast as a
866 * Perform verbose error reporting if not NULL.
869 * A positive value representing the number of bytes needed to store the
870 * name or its address regardless of @p size on success (@p buf contents
871 * are truncated to @p size if not large enough), a negative errno value
872 * otherwise and rte_errno is set.
875 rte_flow_conv_name(int is_action,
880 struct rte_flow_error *error)
883 const struct rte_flow_desc_data *data;
886 static const struct desc_info info_rep[2] = {
887 { rte_flow_desc_item, RTE_DIM(rte_flow_desc_item), },
888 { rte_flow_desc_action, RTE_DIM(rte_flow_desc_action), },
890 const struct desc_info *const info = &info_rep[!!is_action];
891 unsigned int type = (uintptr_t)src;
893 if (type >= info->num)
894 return rte_flow_error_set
895 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
896 "unknown object type to retrieve the name of");
898 return strlcpy(dst, info->data[type].name, size);
899 if (size >= sizeof(const char **))
900 *((const char **)dst) = info->data[type].name;
901 return sizeof(const char **);
904 /** Helper function to convert flow API objects. */
906 rte_flow_conv(enum rte_flow_conv_op op,
910 struct rte_flow_error *error)
913 const struct rte_flow_attr *attr;
915 case RTE_FLOW_CONV_OP_NONE:
917 case RTE_FLOW_CONV_OP_ATTR:
919 if (size > sizeof(*attr))
920 size = sizeof(*attr);
921 rte_memcpy(dst, attr, size);
922 return sizeof(*attr);
923 case RTE_FLOW_CONV_OP_ITEM:
924 return rte_flow_conv_pattern(dst, size, src, 1, error);
925 case RTE_FLOW_CONV_OP_ACTION:
926 return rte_flow_conv_actions(dst, size, src, 1, error);
927 case RTE_FLOW_CONV_OP_PATTERN:
928 return rte_flow_conv_pattern(dst, size, src, 0, error);
929 case RTE_FLOW_CONV_OP_ACTIONS:
930 return rte_flow_conv_actions(dst, size, src, 0, error);
931 case RTE_FLOW_CONV_OP_RULE:
932 return rte_flow_conv_rule(dst, size, src, error);
933 case RTE_FLOW_CONV_OP_ITEM_NAME:
934 return rte_flow_conv_name(0, 0, dst, size, src, error);
935 case RTE_FLOW_CONV_OP_ACTION_NAME:
936 return rte_flow_conv_name(1, 0, dst, size, src, error);
937 case RTE_FLOW_CONV_OP_ITEM_NAME_PTR:
938 return rte_flow_conv_name(0, 1, dst, size, src, error);
939 case RTE_FLOW_CONV_OP_ACTION_NAME_PTR:
940 return rte_flow_conv_name(1, 1, dst, size, src, error);
942 return rte_flow_error_set
943 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
944 "unknown object conversion operation");
947 /** Store a full rte_flow description. */
949 rte_flow_copy(struct rte_flow_desc *desc, size_t len,
950 const struct rte_flow_attr *attr,
951 const struct rte_flow_item *items,
952 const struct rte_flow_action *actions)
955 * Overlap struct rte_flow_conv with struct rte_flow_desc in order
956 * to convert the former to the latter without wasting space.
958 struct rte_flow_conv_rule *dst =
960 (void *)((uintptr_t)desc +
961 (offsetof(struct rte_flow_desc, actions) -
962 offsetof(struct rte_flow_conv_rule, actions))) :
965 len > sizeof(*desc) - sizeof(*dst) ?
966 len - (sizeof(*desc) - sizeof(*dst)) :
968 struct rte_flow_conv_rule src = {
971 .actions_ro = actions,
975 RTE_BUILD_BUG_ON(sizeof(struct rte_flow_desc) <
976 sizeof(struct rte_flow_conv_rule));
978 (&dst->pattern != &desc->items ||
979 &dst->actions != &desc->actions ||
980 (uintptr_t)(dst + 1) != (uintptr_t)(desc + 1))) {
984 ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, dst, dst_size, &src, NULL);
987 ret += sizeof(*desc) - sizeof(*dst);
989 (&(struct rte_flow_desc){
992 .items = dst_size ? dst->pattern : NULL,
993 .actions = dst_size ? dst->actions : NULL,
995 len > sizeof(*desc) ? sizeof(*desc) : len);
1000 rte_flow_dev_dump(uint16_t port_id, FILE *file, struct rte_flow_error *error)
1002 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1003 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1008 if (likely(!!ops->dev_dump)) {
1010 ret = ops->dev_dump(dev, file, error);
1012 return flow_err(port_id, ret, error);
1014 return rte_flow_error_set(error, ENOSYS,
1015 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1016 NULL, rte_strerror(ENOSYS));
1020 rte_flow_get_aged_flows(uint16_t port_id, void **contexts,
1021 uint32_t nb_contexts, struct rte_flow_error *error)
1023 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1024 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1029 if (likely(!!ops->get_aged_flows)) {
1031 ret = ops->get_aged_flows(dev, contexts, nb_contexts, error);
1033 return flow_err(port_id, ret, error);
1035 return rte_flow_error_set(error, ENOTSUP,
1036 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1037 NULL, rte_strerror(ENOTSUP));