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(IPV6_FRAG_EXT, sizeof(struct rte_flow_item_ipv6_frag_ext)),
76 MK_FLOW_ITEM(ICMP6, sizeof(struct rte_flow_item_icmp6)),
77 MK_FLOW_ITEM(ICMP6_ND_NS, sizeof(struct rte_flow_item_icmp6_nd_ns)),
78 MK_FLOW_ITEM(ICMP6_ND_NA, sizeof(struct rte_flow_item_icmp6_nd_na)),
79 MK_FLOW_ITEM(ICMP6_ND_OPT, sizeof(struct rte_flow_item_icmp6_nd_opt)),
80 MK_FLOW_ITEM(ICMP6_ND_OPT_SLA_ETH,
81 sizeof(struct rte_flow_item_icmp6_nd_opt_sla_eth)),
82 MK_FLOW_ITEM(ICMP6_ND_OPT_TLA_ETH,
83 sizeof(struct rte_flow_item_icmp6_nd_opt_tla_eth)),
84 MK_FLOW_ITEM(MARK, sizeof(struct rte_flow_item_mark)),
85 MK_FLOW_ITEM(META, sizeof(struct rte_flow_item_meta)),
86 MK_FLOW_ITEM(TAG, sizeof(struct rte_flow_item_tag)),
87 MK_FLOW_ITEM(GRE_KEY, sizeof(rte_be32_t)),
88 MK_FLOW_ITEM(GTP_PSC, sizeof(struct rte_flow_item_gtp_psc)),
89 MK_FLOW_ITEM(PPPOES, sizeof(struct rte_flow_item_pppoe)),
90 MK_FLOW_ITEM(PPPOED, sizeof(struct rte_flow_item_pppoe)),
91 MK_FLOW_ITEM(PPPOE_PROTO_ID,
92 sizeof(struct rte_flow_item_pppoe_proto_id)),
93 MK_FLOW_ITEM(NSH, sizeof(struct rte_flow_item_nsh)),
94 MK_FLOW_ITEM(IGMP, sizeof(struct rte_flow_item_igmp)),
95 MK_FLOW_ITEM(AH, sizeof(struct rte_flow_item_ah)),
96 MK_FLOW_ITEM(HIGIG2, sizeof(struct rte_flow_item_higig2_hdr)),
97 MK_FLOW_ITEM(L2TPV3OIP, sizeof(struct rte_flow_item_l2tpv3oip)),
98 MK_FLOW_ITEM(PFCP, sizeof(struct rte_flow_item_pfcp)),
99 MK_FLOW_ITEM(ECPRI, sizeof(struct rte_flow_item_ecpri)),
100 MK_FLOW_ITEM(GENEVE_OPT, sizeof(struct rte_flow_item_geneve_opt)),
101 MK_FLOW_ITEM(INTEGRITY, sizeof(struct rte_flow_item_integrity)),
102 MK_FLOW_ITEM(CONNTRACK, sizeof(uint32_t)),
103 MK_FLOW_ITEM(PORT_REPRESENTOR, sizeof(struct rte_flow_item_ethdev)),
104 MK_FLOW_ITEM(REPRESENTED_PORT, sizeof(struct rte_flow_item_ethdev)),
107 /** Generate flow_action[] entry. */
108 #define MK_FLOW_ACTION(t, s) \
109 [RTE_FLOW_ACTION_TYPE_ ## t] = { \
114 /** Information about known flow actions. */
115 static const struct rte_flow_desc_data rte_flow_desc_action[] = {
116 MK_FLOW_ACTION(END, 0),
117 MK_FLOW_ACTION(VOID, 0),
118 MK_FLOW_ACTION(PASSTHRU, 0),
119 MK_FLOW_ACTION(JUMP, sizeof(struct rte_flow_action_jump)),
120 MK_FLOW_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
121 MK_FLOW_ACTION(FLAG, 0),
122 MK_FLOW_ACTION(QUEUE, sizeof(struct rte_flow_action_queue)),
123 MK_FLOW_ACTION(DROP, 0),
124 MK_FLOW_ACTION(COUNT, sizeof(struct rte_flow_action_count)),
125 MK_FLOW_ACTION(RSS, sizeof(struct rte_flow_action_rss)),
126 MK_FLOW_ACTION(PF, 0),
127 MK_FLOW_ACTION(VF, sizeof(struct rte_flow_action_vf)),
128 MK_FLOW_ACTION(PHY_PORT, sizeof(struct rte_flow_action_phy_port)),
129 MK_FLOW_ACTION(PORT_ID, sizeof(struct rte_flow_action_port_id)),
130 MK_FLOW_ACTION(METER, sizeof(struct rte_flow_action_meter)),
131 MK_FLOW_ACTION(SECURITY, sizeof(struct rte_flow_action_security)),
132 MK_FLOW_ACTION(OF_SET_MPLS_TTL,
133 sizeof(struct rte_flow_action_of_set_mpls_ttl)),
134 MK_FLOW_ACTION(OF_DEC_MPLS_TTL, 0),
135 MK_FLOW_ACTION(OF_SET_NW_TTL,
136 sizeof(struct rte_flow_action_of_set_nw_ttl)),
137 MK_FLOW_ACTION(OF_DEC_NW_TTL, 0),
138 MK_FLOW_ACTION(OF_COPY_TTL_OUT, 0),
139 MK_FLOW_ACTION(OF_COPY_TTL_IN, 0),
140 MK_FLOW_ACTION(OF_POP_VLAN, 0),
141 MK_FLOW_ACTION(OF_PUSH_VLAN,
142 sizeof(struct rte_flow_action_of_push_vlan)),
143 MK_FLOW_ACTION(OF_SET_VLAN_VID,
144 sizeof(struct rte_flow_action_of_set_vlan_vid)),
145 MK_FLOW_ACTION(OF_SET_VLAN_PCP,
146 sizeof(struct rte_flow_action_of_set_vlan_pcp)),
147 MK_FLOW_ACTION(OF_POP_MPLS,
148 sizeof(struct rte_flow_action_of_pop_mpls)),
149 MK_FLOW_ACTION(OF_PUSH_MPLS,
150 sizeof(struct rte_flow_action_of_push_mpls)),
151 MK_FLOW_ACTION(VXLAN_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
152 MK_FLOW_ACTION(VXLAN_DECAP, 0),
153 MK_FLOW_ACTION(NVGRE_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
154 MK_FLOW_ACTION(NVGRE_DECAP, 0),
155 MK_FLOW_ACTION(RAW_ENCAP, sizeof(struct rte_flow_action_raw_encap)),
156 MK_FLOW_ACTION(RAW_DECAP, sizeof(struct rte_flow_action_raw_decap)),
157 MK_FLOW_ACTION(SET_IPV4_SRC,
158 sizeof(struct rte_flow_action_set_ipv4)),
159 MK_FLOW_ACTION(SET_IPV4_DST,
160 sizeof(struct rte_flow_action_set_ipv4)),
161 MK_FLOW_ACTION(SET_IPV6_SRC,
162 sizeof(struct rte_flow_action_set_ipv6)),
163 MK_FLOW_ACTION(SET_IPV6_DST,
164 sizeof(struct rte_flow_action_set_ipv6)),
165 MK_FLOW_ACTION(SET_TP_SRC,
166 sizeof(struct rte_flow_action_set_tp)),
167 MK_FLOW_ACTION(SET_TP_DST,
168 sizeof(struct rte_flow_action_set_tp)),
169 MK_FLOW_ACTION(MAC_SWAP, 0),
170 MK_FLOW_ACTION(DEC_TTL, 0),
171 MK_FLOW_ACTION(SET_TTL, sizeof(struct rte_flow_action_set_ttl)),
172 MK_FLOW_ACTION(SET_MAC_SRC, sizeof(struct rte_flow_action_set_mac)),
173 MK_FLOW_ACTION(SET_MAC_DST, sizeof(struct rte_flow_action_set_mac)),
174 MK_FLOW_ACTION(INC_TCP_SEQ, sizeof(rte_be32_t)),
175 MK_FLOW_ACTION(DEC_TCP_SEQ, sizeof(rte_be32_t)),
176 MK_FLOW_ACTION(INC_TCP_ACK, sizeof(rte_be32_t)),
177 MK_FLOW_ACTION(DEC_TCP_ACK, sizeof(rte_be32_t)),
178 MK_FLOW_ACTION(SET_TAG, sizeof(struct rte_flow_action_set_tag)),
179 MK_FLOW_ACTION(SET_META, sizeof(struct rte_flow_action_set_meta)),
180 MK_FLOW_ACTION(SET_IPV4_DSCP, sizeof(struct rte_flow_action_set_dscp)),
181 MK_FLOW_ACTION(SET_IPV6_DSCP, sizeof(struct rte_flow_action_set_dscp)),
182 MK_FLOW_ACTION(AGE, sizeof(struct rte_flow_action_age)),
183 MK_FLOW_ACTION(SAMPLE, sizeof(struct rte_flow_action_sample)),
184 MK_FLOW_ACTION(MODIFY_FIELD,
185 sizeof(struct rte_flow_action_modify_field)),
187 * Indirect action represented as handle of type
188 * (struct rte_flow_action_handle *) stored in conf field (see
189 * struct rte_flow_action); no need for additional structure to * store
190 * indirect action handle.
192 MK_FLOW_ACTION(INDIRECT, 0),
193 MK_FLOW_ACTION(CONNTRACK, sizeof(struct rte_flow_action_conntrack)),
194 MK_FLOW_ACTION(PORT_REPRESENTOR, sizeof(struct rte_flow_action_ethdev)),
195 MK_FLOW_ACTION(REPRESENTED_PORT, sizeof(struct rte_flow_action_ethdev)),
199 rte_flow_dynf_metadata_register(void)
204 static const struct rte_mbuf_dynfield desc_offs = {
205 .name = RTE_MBUF_DYNFIELD_METADATA_NAME,
206 .size = sizeof(uint32_t),
207 .align = __alignof__(uint32_t),
209 static const struct rte_mbuf_dynflag desc_flag = {
210 .name = RTE_MBUF_DYNFLAG_METADATA_NAME,
213 offset = rte_mbuf_dynfield_register(&desc_offs);
216 flag = rte_mbuf_dynflag_register(&desc_flag);
219 rte_flow_dynf_metadata_offs = offset;
220 rte_flow_dynf_metadata_mask = (1ULL << flag);
224 rte_flow_dynf_metadata_offs = -1;
225 rte_flow_dynf_metadata_mask = 0ULL;
230 fts_enter(struct rte_eth_dev *dev)
232 if (!(dev->data->dev_flags & RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE))
233 pthread_mutex_lock(&dev->data->flow_ops_mutex);
237 fts_exit(struct rte_eth_dev *dev)
239 if (!(dev->data->dev_flags & RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE))
240 pthread_mutex_unlock(&dev->data->flow_ops_mutex);
244 flow_err(uint16_t port_id, int ret, struct rte_flow_error *error)
248 if (rte_eth_dev_is_removed(port_id))
249 return rte_flow_error_set(error, EIO,
250 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
251 NULL, rte_strerror(EIO));
255 /* Get generic flow operations structure from a port. */
256 const struct rte_flow_ops *
257 rte_flow_ops_get(uint16_t port_id, struct rte_flow_error *error)
259 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
260 const struct rte_flow_ops *ops;
263 if (unlikely(!rte_eth_dev_is_valid_port(port_id)))
265 else if (unlikely(dev->dev_ops->flow_ops_get == NULL))
266 /* flow API not supported with this driver dev_ops */
269 code = dev->dev_ops->flow_ops_get(dev, &ops);
270 if (code == 0 && ops == NULL)
271 /* flow API not supported with this device */
275 rte_flow_error_set(error, code, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
276 NULL, rte_strerror(code));
282 /* Check whether a flow rule can be created on a given port. */
284 rte_flow_validate(uint16_t port_id,
285 const struct rte_flow_attr *attr,
286 const struct rte_flow_item pattern[],
287 const struct rte_flow_action actions[],
288 struct rte_flow_error *error)
290 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
291 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
296 if (likely(!!ops->validate)) {
298 ret = ops->validate(dev, attr, pattern, actions, error);
300 return flow_err(port_id, ret, error);
302 return rte_flow_error_set(error, ENOSYS,
303 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
304 NULL, rte_strerror(ENOSYS));
307 /* Create a flow rule on a given port. */
309 rte_flow_create(uint16_t port_id,
310 const struct rte_flow_attr *attr,
311 const struct rte_flow_item pattern[],
312 const struct rte_flow_action actions[],
313 struct rte_flow_error *error)
315 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
316 struct rte_flow *flow;
317 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
321 if (likely(!!ops->create)) {
323 flow = ops->create(dev, attr, pattern, actions, error);
326 flow_err(port_id, -rte_errno, error);
329 rte_flow_error_set(error, ENOSYS, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
330 NULL, rte_strerror(ENOSYS));
334 /* Destroy a flow rule on a given port. */
336 rte_flow_destroy(uint16_t port_id,
337 struct rte_flow *flow,
338 struct rte_flow_error *error)
340 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
341 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
346 if (likely(!!ops->destroy)) {
348 ret = ops->destroy(dev, flow, error);
350 return flow_err(port_id, ret, error);
352 return rte_flow_error_set(error, ENOSYS,
353 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
354 NULL, rte_strerror(ENOSYS));
357 /* Destroy all flow rules associated with a port. */
359 rte_flow_flush(uint16_t port_id,
360 struct rte_flow_error *error)
362 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
363 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
368 if (likely(!!ops->flush)) {
370 ret = ops->flush(dev, error);
372 return flow_err(port_id, ret, error);
374 return rte_flow_error_set(error, ENOSYS,
375 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
376 NULL, rte_strerror(ENOSYS));
379 /* Query an existing flow rule. */
381 rte_flow_query(uint16_t port_id,
382 struct rte_flow *flow,
383 const struct rte_flow_action *action,
385 struct rte_flow_error *error)
387 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
388 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
393 if (likely(!!ops->query)) {
395 ret = ops->query(dev, flow, action, data, error);
397 return flow_err(port_id, ret, error);
399 return rte_flow_error_set(error, ENOSYS,
400 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
401 NULL, rte_strerror(ENOSYS));
404 /* Restrict ingress traffic to the defined flow rules. */
406 rte_flow_isolate(uint16_t port_id,
408 struct rte_flow_error *error)
410 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
411 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
416 if (likely(!!ops->isolate)) {
418 ret = ops->isolate(dev, set, error);
420 return flow_err(port_id, ret, error);
422 return rte_flow_error_set(error, ENOSYS,
423 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
424 NULL, rte_strerror(ENOSYS));
427 /* Initialize flow error structure. */
429 rte_flow_error_set(struct rte_flow_error *error,
431 enum rte_flow_error_type type,
436 *error = (struct rte_flow_error){
446 /** Pattern item specification types. */
447 enum rte_flow_conv_item_spec_type {
448 RTE_FLOW_CONV_ITEM_SPEC,
449 RTE_FLOW_CONV_ITEM_LAST,
450 RTE_FLOW_CONV_ITEM_MASK,
454 * Copy pattern item specification.
457 * Output buffer. Can be NULL if @p size is zero.
459 * Size of @p buf in bytes.
461 * Pattern item to copy specification from.
463 * Specification selector for either @p spec, @p last or @p mask.
466 * Number of bytes needed to store pattern item specification regardless
467 * of @p size. @p buf contents are truncated to @p size if not large
471 rte_flow_conv_item_spec(void *buf, const size_t size,
472 const struct rte_flow_item *item,
473 enum rte_flow_conv_item_spec_type type)
477 type == RTE_FLOW_CONV_ITEM_SPEC ? item->spec :
478 type == RTE_FLOW_CONV_ITEM_LAST ? item->last :
479 type == RTE_FLOW_CONV_ITEM_MASK ? item->mask :
482 switch (item->type) {
484 const struct rte_flow_item_raw *raw;
487 const struct rte_flow_item_raw *raw;
490 const struct rte_flow_item_raw *raw;
493 const struct rte_flow_item_raw *raw;
496 struct rte_flow_item_raw *raw;
500 case RTE_FLOW_ITEM_TYPE_RAW:
501 spec.raw = item->spec;
502 last.raw = item->last ? item->last : item->spec;
503 mask.raw = item->mask ? item->mask : &rte_flow_item_raw_mask;
507 (&(struct rte_flow_item_raw){
508 .relative = src.raw->relative,
509 .search = src.raw->search,
510 .reserved = src.raw->reserved,
511 .offset = src.raw->offset,
512 .limit = src.raw->limit,
513 .length = src.raw->length,
515 size > sizeof(*dst.raw) ? sizeof(*dst.raw) : size);
516 off = sizeof(*dst.raw);
517 if (type == RTE_FLOW_CONV_ITEM_SPEC ||
518 (type == RTE_FLOW_CONV_ITEM_MASK &&
519 ((spec.raw->length & mask.raw->length) >=
520 (last.raw->length & mask.raw->length))))
521 tmp = spec.raw->length & mask.raw->length;
523 tmp = last.raw->length & mask.raw->length;
525 off = RTE_ALIGN_CEIL(off, sizeof(*dst.raw->pattern));
526 if (size >= off + tmp)
527 dst.raw->pattern = rte_memcpy
528 ((void *)((uintptr_t)dst.raw + off),
529 src.raw->pattern, tmp);
535 * allow PMD private flow item
537 off = (int)item->type >= 0 ?
538 rte_flow_desc_item[item->type].size : sizeof(void *);
539 rte_memcpy(buf, data, (size > off ? off : size));
546 * Copy action configuration.
549 * Output buffer. Can be NULL if @p size is zero.
551 * Size of @p buf in bytes.
553 * Action to copy configuration from.
556 * Number of bytes needed to store pattern item specification regardless
557 * of @p size. @p buf contents are truncated to @p size if not large
561 rte_flow_conv_action_conf(void *buf, const size_t size,
562 const struct rte_flow_action *action)
566 switch (action->type) {
568 const struct rte_flow_action_rss *rss;
569 const struct rte_flow_action_vxlan_encap *vxlan_encap;
570 const struct rte_flow_action_nvgre_encap *nvgre_encap;
573 struct rte_flow_action_rss *rss;
574 struct rte_flow_action_vxlan_encap *vxlan_encap;
575 struct rte_flow_action_nvgre_encap *nvgre_encap;
580 case RTE_FLOW_ACTION_TYPE_RSS:
581 src.rss = action->conf;
584 (&(struct rte_flow_action_rss){
585 .func = src.rss->func,
586 .level = src.rss->level,
587 .types = src.rss->types,
588 .key_len = src.rss->key_len,
589 .queue_num = src.rss->queue_num,
591 size > sizeof(*dst.rss) ? sizeof(*dst.rss) : size);
592 off = sizeof(*dst.rss);
593 if (src.rss->key_len && src.rss->key) {
594 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->key));
595 tmp = sizeof(*src.rss->key) * src.rss->key_len;
596 if (size >= off + tmp)
597 dst.rss->key = rte_memcpy
598 ((void *)((uintptr_t)dst.rss + off),
602 if (src.rss->queue_num) {
603 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->queue));
604 tmp = sizeof(*src.rss->queue) * src.rss->queue_num;
605 if (size >= off + tmp)
606 dst.rss->queue = rte_memcpy
607 ((void *)((uintptr_t)dst.rss + off),
608 src.rss->queue, tmp);
612 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
613 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
614 src.vxlan_encap = action->conf;
615 dst.vxlan_encap = buf;
616 RTE_BUILD_BUG_ON(sizeof(*src.vxlan_encap) !=
617 sizeof(*src.nvgre_encap) ||
618 offsetof(struct rte_flow_action_vxlan_encap,
620 offsetof(struct rte_flow_action_nvgre_encap,
622 off = sizeof(*dst.vxlan_encap);
623 if (src.vxlan_encap->definition) {
625 (off, sizeof(*dst.vxlan_encap->definition));
627 (RTE_FLOW_CONV_OP_PATTERN,
628 (void *)((uintptr_t)dst.vxlan_encap + off),
629 size > off ? size - off : 0,
630 src.vxlan_encap->definition, NULL);
633 if (size >= off + ret)
634 dst.vxlan_encap->definition =
635 (void *)((uintptr_t)dst.vxlan_encap +
642 * allow PMD private flow action
644 off = (int)action->type >= 0 ?
645 rte_flow_desc_action[action->type].size : sizeof(void *);
646 rte_memcpy(buf, action->conf, (size > off ? off : size));
653 * Copy a list of pattern items.
656 * Destination buffer. Can be NULL if @p size is zero.
658 * Size of @p dst in bytes.
660 * Source pattern items.
662 * Maximum number of pattern items to process from @p src or 0 to process
663 * the entire list. In both cases, processing stops after
664 * RTE_FLOW_ITEM_TYPE_END is encountered.
666 * Perform verbose error reporting if not NULL.
669 * A positive value representing the number of bytes needed to store
670 * pattern items regardless of @p size on success (@p buf contents are
671 * truncated to @p size if not large enough), a negative errno value
672 * otherwise and rte_errno is set.
675 rte_flow_conv_pattern(struct rte_flow_item *dst,
677 const struct rte_flow_item *src,
679 struct rte_flow_error *error)
681 uintptr_t data = (uintptr_t)dst;
686 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
688 * allow PMD private flow item
690 if (((int)src->type >= 0) &&
691 ((size_t)src->type >= RTE_DIM(rte_flow_desc_item) ||
692 !rte_flow_desc_item[src->type].name))
693 return rte_flow_error_set
694 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, src,
695 "cannot convert unknown item type");
696 if (size >= off + sizeof(*dst))
697 *dst = (struct rte_flow_item){
709 off = RTE_ALIGN_CEIL(off, sizeof(double));
710 ret = rte_flow_conv_item_spec
711 ((void *)(data + off),
712 size > off ? size - off : 0, src,
713 RTE_FLOW_CONV_ITEM_SPEC);
714 if (size && size >= off + ret)
715 dst->spec = (void *)(data + off);
720 off = RTE_ALIGN_CEIL(off, sizeof(double));
721 ret = rte_flow_conv_item_spec
722 ((void *)(data + off),
723 size > off ? size - off : 0, src,
724 RTE_FLOW_CONV_ITEM_LAST);
725 if (size && size >= off + ret)
726 dst->last = (void *)(data + off);
730 off = RTE_ALIGN_CEIL(off, sizeof(double));
731 ret = rte_flow_conv_item_spec
732 ((void *)(data + off),
733 size > off ? size - off : 0, src,
734 RTE_FLOW_CONV_ITEM_MASK);
735 if (size && size >= off + ret)
736 dst->mask = (void *)(data + off);
746 * Copy a list of actions.
749 * Destination buffer. Can be NULL if @p size is zero.
751 * Size of @p dst in bytes.
755 * Maximum number of actions to process from @p src or 0 to process the
756 * entire list. In both cases, processing stops after
757 * RTE_FLOW_ACTION_TYPE_END is encountered.
759 * Perform verbose error reporting if not NULL.
762 * A positive value representing the number of bytes needed to store
763 * actions regardless of @p size on success (@p buf contents are truncated
764 * to @p size if not large enough), a negative errno value otherwise and
768 rte_flow_conv_actions(struct rte_flow_action *dst,
770 const struct rte_flow_action *src,
772 struct rte_flow_error *error)
774 uintptr_t data = (uintptr_t)dst;
779 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
781 * allow PMD private flow action
783 if (((int)src->type >= 0) &&
784 ((size_t)src->type >= RTE_DIM(rte_flow_desc_action) ||
785 !rte_flow_desc_action[src->type].name))
786 return rte_flow_error_set
787 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
788 src, "cannot convert unknown action type");
789 if (size >= off + sizeof(*dst))
790 *dst = (struct rte_flow_action){
802 off = RTE_ALIGN_CEIL(off, sizeof(double));
803 ret = rte_flow_conv_action_conf
804 ((void *)(data + off),
805 size > off ? size - off : 0, src);
806 if (size && size >= off + ret)
807 dst->conf = (void *)(data + off);
817 * Copy flow rule components.
819 * This comprises the flow rule descriptor itself, attributes, pattern and
820 * actions list. NULL components in @p src are skipped.
823 * Destination buffer. Can be NULL if @p size is zero.
825 * Size of @p dst in bytes.
827 * Source flow rule descriptor.
829 * Perform verbose error reporting if not NULL.
832 * A positive value representing the number of bytes needed to store all
833 * components including the descriptor regardless of @p size on success
834 * (@p buf contents are truncated to @p size if not large enough), a
835 * negative errno value otherwise and rte_errno is set.
838 rte_flow_conv_rule(struct rte_flow_conv_rule *dst,
840 const struct rte_flow_conv_rule *src,
841 struct rte_flow_error *error)
847 (&(struct rte_flow_conv_rule){
852 size > sizeof(*dst) ? sizeof(*dst) : size);
855 off = RTE_ALIGN_CEIL(off, sizeof(double));
856 if (size && size >= off + sizeof(*dst->attr))
857 dst->attr = rte_memcpy
858 ((void *)((uintptr_t)dst + off),
859 src->attr_ro, sizeof(*dst->attr));
860 off += sizeof(*dst->attr);
862 if (src->pattern_ro) {
863 off = RTE_ALIGN_CEIL(off, sizeof(double));
864 ret = rte_flow_conv_pattern((void *)((uintptr_t)dst + off),
865 size > off ? size - off : 0,
866 src->pattern_ro, 0, error);
869 if (size && size >= off + (size_t)ret)
870 dst->pattern = (void *)((uintptr_t)dst + off);
873 if (src->actions_ro) {
874 off = RTE_ALIGN_CEIL(off, sizeof(double));
875 ret = rte_flow_conv_actions((void *)((uintptr_t)dst + off),
876 size > off ? size - off : 0,
877 src->actions_ro, 0, error);
880 if (size >= off + (size_t)ret)
881 dst->actions = (void *)((uintptr_t)dst + off);
888 * Retrieve the name of a pattern item/action type.
891 * Nonzero when @p src represents an action type instead of a pattern item
894 * Nonzero to write string address instead of contents into @p dst.
896 * Destination buffer. Can be NULL if @p size is zero.
898 * Size of @p dst in bytes.
900 * Depending on @p is_action, source pattern item or action type cast as a
903 * Perform verbose error reporting if not NULL.
906 * A positive value representing the number of bytes needed to store the
907 * name or its address regardless of @p size on success (@p buf contents
908 * are truncated to @p size if not large enough), a negative errno value
909 * otherwise and rte_errno is set.
912 rte_flow_conv_name(int is_action,
917 struct rte_flow_error *error)
920 const struct rte_flow_desc_data *data;
923 static const struct desc_info info_rep[2] = {
924 { rte_flow_desc_item, RTE_DIM(rte_flow_desc_item), },
925 { rte_flow_desc_action, RTE_DIM(rte_flow_desc_action), },
927 const struct desc_info *const info = &info_rep[!!is_action];
928 unsigned int type = (uintptr_t)src;
930 if (type >= info->num)
931 return rte_flow_error_set
932 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
933 "unknown object type to retrieve the name of");
935 return strlcpy(dst, info->data[type].name, size);
936 if (size >= sizeof(const char **))
937 *((const char **)dst) = info->data[type].name;
938 return sizeof(const char **);
941 /** Helper function to convert flow API objects. */
943 rte_flow_conv(enum rte_flow_conv_op op,
947 struct rte_flow_error *error)
950 const struct rte_flow_attr *attr;
952 case RTE_FLOW_CONV_OP_NONE:
954 case RTE_FLOW_CONV_OP_ATTR:
956 if (size > sizeof(*attr))
957 size = sizeof(*attr);
958 rte_memcpy(dst, attr, size);
959 return sizeof(*attr);
960 case RTE_FLOW_CONV_OP_ITEM:
961 return rte_flow_conv_pattern(dst, size, src, 1, error);
962 case RTE_FLOW_CONV_OP_ACTION:
963 return rte_flow_conv_actions(dst, size, src, 1, error);
964 case RTE_FLOW_CONV_OP_PATTERN:
965 return rte_flow_conv_pattern(dst, size, src, 0, error);
966 case RTE_FLOW_CONV_OP_ACTIONS:
967 return rte_flow_conv_actions(dst, size, src, 0, error);
968 case RTE_FLOW_CONV_OP_RULE:
969 return rte_flow_conv_rule(dst, size, src, error);
970 case RTE_FLOW_CONV_OP_ITEM_NAME:
971 return rte_flow_conv_name(0, 0, dst, size, src, error);
972 case RTE_FLOW_CONV_OP_ACTION_NAME:
973 return rte_flow_conv_name(1, 0, dst, size, src, error);
974 case RTE_FLOW_CONV_OP_ITEM_NAME_PTR:
975 return rte_flow_conv_name(0, 1, dst, size, src, error);
976 case RTE_FLOW_CONV_OP_ACTION_NAME_PTR:
977 return rte_flow_conv_name(1, 1, dst, size, src, error);
979 return rte_flow_error_set
980 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
981 "unknown object conversion operation");
984 /** Store a full rte_flow description. */
986 rte_flow_copy(struct rte_flow_desc *desc, size_t len,
987 const struct rte_flow_attr *attr,
988 const struct rte_flow_item *items,
989 const struct rte_flow_action *actions)
992 * Overlap struct rte_flow_conv with struct rte_flow_desc in order
993 * to convert the former to the latter without wasting space.
995 struct rte_flow_conv_rule *dst =
997 (void *)((uintptr_t)desc +
998 (offsetof(struct rte_flow_desc, actions) -
999 offsetof(struct rte_flow_conv_rule, actions))) :
1002 len > sizeof(*desc) - sizeof(*dst) ?
1003 len - (sizeof(*desc) - sizeof(*dst)) :
1005 struct rte_flow_conv_rule src = {
1007 .pattern_ro = items,
1008 .actions_ro = actions,
1012 RTE_BUILD_BUG_ON(sizeof(struct rte_flow_desc) <
1013 sizeof(struct rte_flow_conv_rule));
1015 (&dst->pattern != &desc->items ||
1016 &dst->actions != &desc->actions ||
1017 (uintptr_t)(dst + 1) != (uintptr_t)(desc + 1))) {
1021 ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, dst, dst_size, &src, NULL);
1024 ret += sizeof(*desc) - sizeof(*dst);
1026 (&(struct rte_flow_desc){
1029 .items = dst_size ? dst->pattern : NULL,
1030 .actions = dst_size ? dst->actions : NULL,
1032 len > sizeof(*desc) ? sizeof(*desc) : len);
1037 rte_flow_dev_dump(uint16_t port_id, struct rte_flow *flow,
1038 FILE *file, struct rte_flow_error *error)
1040 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1041 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1046 if (likely(!!ops->dev_dump)) {
1048 ret = ops->dev_dump(dev, flow, file, error);
1050 return flow_err(port_id, ret, error);
1052 return rte_flow_error_set(error, ENOSYS,
1053 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1054 NULL, rte_strerror(ENOSYS));
1058 rte_flow_get_aged_flows(uint16_t port_id, void **contexts,
1059 uint32_t nb_contexts, struct rte_flow_error *error)
1061 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1062 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1067 if (likely(!!ops->get_aged_flows)) {
1069 ret = ops->get_aged_flows(dev, contexts, nb_contexts, error);
1071 return flow_err(port_id, ret, error);
1073 return rte_flow_error_set(error, ENOTSUP,
1074 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1075 NULL, rte_strerror(ENOTSUP));
1078 struct rte_flow_action_handle *
1079 rte_flow_action_handle_create(uint16_t port_id,
1080 const struct rte_flow_indir_action_conf *conf,
1081 const struct rte_flow_action *action,
1082 struct rte_flow_error *error)
1084 struct rte_flow_action_handle *handle;
1085 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1089 if (unlikely(!ops->action_handle_create)) {
1090 rte_flow_error_set(error, ENOSYS,
1091 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1092 rte_strerror(ENOSYS));
1095 handle = ops->action_handle_create(&rte_eth_devices[port_id],
1096 conf, action, error);
1098 flow_err(port_id, -rte_errno, error);
1103 rte_flow_action_handle_destroy(uint16_t port_id,
1104 struct rte_flow_action_handle *handle,
1105 struct rte_flow_error *error)
1108 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1112 if (unlikely(!ops->action_handle_destroy))
1113 return rte_flow_error_set(error, ENOSYS,
1114 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1115 NULL, rte_strerror(ENOSYS));
1116 ret = ops->action_handle_destroy(&rte_eth_devices[port_id],
1118 return flow_err(port_id, ret, error);
1122 rte_flow_action_handle_update(uint16_t port_id,
1123 struct rte_flow_action_handle *handle,
1125 struct rte_flow_error *error)
1128 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1132 if (unlikely(!ops->action_handle_update))
1133 return rte_flow_error_set(error, ENOSYS,
1134 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1135 NULL, rte_strerror(ENOSYS));
1136 ret = ops->action_handle_update(&rte_eth_devices[port_id], handle,
1138 return flow_err(port_id, ret, error);
1142 rte_flow_action_handle_query(uint16_t port_id,
1143 const struct rte_flow_action_handle *handle,
1145 struct rte_flow_error *error)
1148 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1152 if (unlikely(!ops->action_handle_query))
1153 return rte_flow_error_set(error, ENOSYS,
1154 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1155 NULL, rte_strerror(ENOSYS));
1156 ret = ops->action_handle_query(&rte_eth_devices[port_id], handle,
1158 return flow_err(port_id, ret, error);
1162 rte_flow_tunnel_decap_set(uint16_t port_id,
1163 struct rte_flow_tunnel *tunnel,
1164 struct rte_flow_action **actions,
1165 uint32_t *num_of_actions,
1166 struct rte_flow_error *error)
1168 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1169 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1173 if (likely(!!ops->tunnel_decap_set)) {
1174 return flow_err(port_id,
1175 ops->tunnel_decap_set(dev, tunnel, actions,
1176 num_of_actions, error),
1179 return rte_flow_error_set(error, ENOTSUP,
1180 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1181 NULL, rte_strerror(ENOTSUP));
1185 rte_flow_tunnel_match(uint16_t port_id,
1186 struct rte_flow_tunnel *tunnel,
1187 struct rte_flow_item **items,
1188 uint32_t *num_of_items,
1189 struct rte_flow_error *error)
1191 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1192 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1196 if (likely(!!ops->tunnel_match)) {
1197 return flow_err(port_id,
1198 ops->tunnel_match(dev, tunnel, items,
1199 num_of_items, error),
1202 return rte_flow_error_set(error, ENOTSUP,
1203 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1204 NULL, rte_strerror(ENOTSUP));
1208 rte_flow_get_restore_info(uint16_t port_id,
1210 struct rte_flow_restore_info *restore_info,
1211 struct rte_flow_error *error)
1213 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1214 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1218 if (likely(!!ops->get_restore_info)) {
1219 return flow_err(port_id,
1220 ops->get_restore_info(dev, m, restore_info,
1224 return rte_flow_error_set(error, ENOTSUP,
1225 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1226 NULL, rte_strerror(ENOTSUP));
1230 rte_flow_tunnel_action_decap_release(uint16_t port_id,
1231 struct rte_flow_action *actions,
1232 uint32_t num_of_actions,
1233 struct rte_flow_error *error)
1235 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1236 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1240 if (likely(!!ops->tunnel_action_decap_release)) {
1241 return flow_err(port_id,
1242 ops->tunnel_action_decap_release(dev, actions,
1247 return rte_flow_error_set(error, ENOTSUP,
1248 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1249 NULL, rte_strerror(ENOTSUP));
1253 rte_flow_tunnel_item_release(uint16_t port_id,
1254 struct rte_flow_item *items,
1255 uint32_t num_of_items,
1256 struct rte_flow_error *error)
1258 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1259 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1263 if (likely(!!ops->tunnel_item_release)) {
1264 return flow_err(port_id,
1265 ops->tunnel_item_release(dev, items,
1266 num_of_items, error),
1269 return rte_flow_error_set(error, ENOTSUP,
1270 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1271 NULL, rte_strerror(ENOTSUP));
1275 rte_flow_pick_transfer_proxy(uint16_t port_id, uint16_t *proxy_port_id,
1276 struct rte_flow_error *error)
1278 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1279 struct rte_eth_dev *dev;
1281 if (unlikely(ops == NULL))
1284 if (ops->pick_transfer_proxy == NULL) {
1285 *proxy_port_id = port_id;
1289 dev = &rte_eth_devices[port_id];
1291 return flow_err(port_id,
1292 ops->pick_transfer_proxy(dev, proxy_port_id, error),