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 {
33 size_t (*desc_fn)(void *dst, const void *src);
45 * rte_flow_desc_item - for flow item conversion.
46 * rte_flow_desc_action - for flow action conversion.
48 * Offset into the desc param or negative value for private flow elements.
51 rte_flow_conv_copy(void *buf, const void *data, const size_t size,
52 const struct rte_flow_desc_data *desc, int type)
55 * Allow PMD private flow item
57 size_t sz = type >= 0 ? desc[type].size : sizeof(void *);
58 if (buf == NULL || data == NULL)
60 rte_memcpy(buf, data, (size > sz ? sz : size));
61 if (desc[type].desc_fn)
62 sz += desc[type].desc_fn(size > 0 ? buf : NULL, data);
67 rte_flow_item_flex_conv(void *buf, const void *data)
69 struct rte_flow_item_flex *dst = buf;
70 const struct rte_flow_item_flex *src = data;
72 dst->pattern = rte_memcpy
73 ((void *)((uintptr_t)(dst + 1)), src->pattern,
79 /** Generate flow_item[] entry. */
80 #define MK_FLOW_ITEM(t, s) \
81 [RTE_FLOW_ITEM_TYPE_ ## t] = { \
87 #define MK_FLOW_ITEM_FN(t, s, fn) \
88 [RTE_FLOW_ITEM_TYPE_ ## t] = {\
94 /** Information about known flow pattern items. */
95 static const struct rte_flow_desc_data rte_flow_desc_item[] = {
97 MK_FLOW_ITEM(VOID, 0),
98 MK_FLOW_ITEM(INVERT, 0),
99 MK_FLOW_ITEM(ANY, sizeof(struct rte_flow_item_any)),
101 MK_FLOW_ITEM(VF, sizeof(struct rte_flow_item_vf)),
102 MK_FLOW_ITEM(PHY_PORT, sizeof(struct rte_flow_item_phy_port)),
103 MK_FLOW_ITEM(PORT_ID, sizeof(struct rte_flow_item_port_id)),
104 MK_FLOW_ITEM(RAW, sizeof(struct rte_flow_item_raw)),
105 MK_FLOW_ITEM(ETH, sizeof(struct rte_flow_item_eth)),
106 MK_FLOW_ITEM(VLAN, sizeof(struct rte_flow_item_vlan)),
107 MK_FLOW_ITEM(IPV4, sizeof(struct rte_flow_item_ipv4)),
108 MK_FLOW_ITEM(IPV6, sizeof(struct rte_flow_item_ipv6)),
109 MK_FLOW_ITEM(ICMP, sizeof(struct rte_flow_item_icmp)),
110 MK_FLOW_ITEM(UDP, sizeof(struct rte_flow_item_udp)),
111 MK_FLOW_ITEM(TCP, sizeof(struct rte_flow_item_tcp)),
112 MK_FLOW_ITEM(SCTP, sizeof(struct rte_flow_item_sctp)),
113 MK_FLOW_ITEM(VXLAN, sizeof(struct rte_flow_item_vxlan)),
114 MK_FLOW_ITEM(E_TAG, sizeof(struct rte_flow_item_e_tag)),
115 MK_FLOW_ITEM(NVGRE, sizeof(struct rte_flow_item_nvgre)),
116 MK_FLOW_ITEM(MPLS, sizeof(struct rte_flow_item_mpls)),
117 MK_FLOW_ITEM(GRE, sizeof(struct rte_flow_item_gre)),
118 MK_FLOW_ITEM(FUZZY, sizeof(struct rte_flow_item_fuzzy)),
119 MK_FLOW_ITEM(GTP, sizeof(struct rte_flow_item_gtp)),
120 MK_FLOW_ITEM(GTPC, sizeof(struct rte_flow_item_gtp)),
121 MK_FLOW_ITEM(GTPU, sizeof(struct rte_flow_item_gtp)),
122 MK_FLOW_ITEM(ESP, sizeof(struct rte_flow_item_esp)),
123 MK_FLOW_ITEM(GENEVE, sizeof(struct rte_flow_item_geneve)),
124 MK_FLOW_ITEM(VXLAN_GPE, sizeof(struct rte_flow_item_vxlan_gpe)),
125 MK_FLOW_ITEM(ARP_ETH_IPV4, sizeof(struct rte_flow_item_arp_eth_ipv4)),
126 MK_FLOW_ITEM(IPV6_EXT, sizeof(struct rte_flow_item_ipv6_ext)),
127 MK_FLOW_ITEM(IPV6_FRAG_EXT, sizeof(struct rte_flow_item_ipv6_frag_ext)),
128 MK_FLOW_ITEM(ICMP6, sizeof(struct rte_flow_item_icmp6)),
129 MK_FLOW_ITEM(ICMP6_ND_NS, sizeof(struct rte_flow_item_icmp6_nd_ns)),
130 MK_FLOW_ITEM(ICMP6_ND_NA, sizeof(struct rte_flow_item_icmp6_nd_na)),
131 MK_FLOW_ITEM(ICMP6_ND_OPT, sizeof(struct rte_flow_item_icmp6_nd_opt)),
132 MK_FLOW_ITEM(ICMP6_ND_OPT_SLA_ETH,
133 sizeof(struct rte_flow_item_icmp6_nd_opt_sla_eth)),
134 MK_FLOW_ITEM(ICMP6_ND_OPT_TLA_ETH,
135 sizeof(struct rte_flow_item_icmp6_nd_opt_tla_eth)),
136 MK_FLOW_ITEM(MARK, sizeof(struct rte_flow_item_mark)),
137 MK_FLOW_ITEM(META, sizeof(struct rte_flow_item_meta)),
138 MK_FLOW_ITEM(TAG, sizeof(struct rte_flow_item_tag)),
139 MK_FLOW_ITEM(GRE_KEY, sizeof(rte_be32_t)),
140 MK_FLOW_ITEM(GTP_PSC, sizeof(struct rte_flow_item_gtp_psc)),
141 MK_FLOW_ITEM(PPPOES, sizeof(struct rte_flow_item_pppoe)),
142 MK_FLOW_ITEM(PPPOED, sizeof(struct rte_flow_item_pppoe)),
143 MK_FLOW_ITEM(PPPOE_PROTO_ID,
144 sizeof(struct rte_flow_item_pppoe_proto_id)),
145 MK_FLOW_ITEM(NSH, sizeof(struct rte_flow_item_nsh)),
146 MK_FLOW_ITEM(IGMP, sizeof(struct rte_flow_item_igmp)),
147 MK_FLOW_ITEM(AH, sizeof(struct rte_flow_item_ah)),
148 MK_FLOW_ITEM(HIGIG2, sizeof(struct rte_flow_item_higig2_hdr)),
149 MK_FLOW_ITEM(L2TPV3OIP, sizeof(struct rte_flow_item_l2tpv3oip)),
150 MK_FLOW_ITEM(PFCP, sizeof(struct rte_flow_item_pfcp)),
151 MK_FLOW_ITEM(ECPRI, sizeof(struct rte_flow_item_ecpri)),
152 MK_FLOW_ITEM(GENEVE_OPT, sizeof(struct rte_flow_item_geneve_opt)),
153 MK_FLOW_ITEM(INTEGRITY, sizeof(struct rte_flow_item_integrity)),
154 MK_FLOW_ITEM(CONNTRACK, sizeof(uint32_t)),
155 MK_FLOW_ITEM(PORT_REPRESENTOR, sizeof(struct rte_flow_item_ethdev)),
156 MK_FLOW_ITEM(REPRESENTED_PORT, sizeof(struct rte_flow_item_ethdev)),
157 MK_FLOW_ITEM_FN(FLEX, sizeof(struct rte_flow_item_flex),
158 rte_flow_item_flex_conv),
159 MK_FLOW_ITEM(L2TPV2, sizeof(struct rte_flow_item_l2tpv2)),
160 MK_FLOW_ITEM(PPP, sizeof(struct rte_flow_item_ppp)),
163 /** Generate flow_action[] entry. */
164 #define MK_FLOW_ACTION(t, s) \
165 [RTE_FLOW_ACTION_TYPE_ ## t] = { \
171 #define MK_FLOW_ACTION_FN(t, fn) \
172 [RTE_FLOW_ACTION_TYPE_ ## t] = { \
179 /** Information about known flow actions. */
180 static const struct rte_flow_desc_data rte_flow_desc_action[] = {
181 MK_FLOW_ACTION(END, 0),
182 MK_FLOW_ACTION(VOID, 0),
183 MK_FLOW_ACTION(PASSTHRU, 0),
184 MK_FLOW_ACTION(JUMP, sizeof(struct rte_flow_action_jump)),
185 MK_FLOW_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
186 MK_FLOW_ACTION(FLAG, 0),
187 MK_FLOW_ACTION(QUEUE, sizeof(struct rte_flow_action_queue)),
188 MK_FLOW_ACTION(DROP, 0),
189 MK_FLOW_ACTION(COUNT, sizeof(struct rte_flow_action_count)),
190 MK_FLOW_ACTION(RSS, sizeof(struct rte_flow_action_rss)),
191 MK_FLOW_ACTION(PF, 0),
192 MK_FLOW_ACTION(VF, sizeof(struct rte_flow_action_vf)),
193 MK_FLOW_ACTION(PHY_PORT, sizeof(struct rte_flow_action_phy_port)),
194 MK_FLOW_ACTION(PORT_ID, sizeof(struct rte_flow_action_port_id)),
195 MK_FLOW_ACTION(METER, sizeof(struct rte_flow_action_meter)),
196 MK_FLOW_ACTION(SECURITY, sizeof(struct rte_flow_action_security)),
197 MK_FLOW_ACTION(OF_SET_MPLS_TTL,
198 sizeof(struct rte_flow_action_of_set_mpls_ttl)),
199 MK_FLOW_ACTION(OF_DEC_MPLS_TTL, 0),
200 MK_FLOW_ACTION(OF_SET_NW_TTL,
201 sizeof(struct rte_flow_action_of_set_nw_ttl)),
202 MK_FLOW_ACTION(OF_DEC_NW_TTL, 0),
203 MK_FLOW_ACTION(OF_COPY_TTL_OUT, 0),
204 MK_FLOW_ACTION(OF_COPY_TTL_IN, 0),
205 MK_FLOW_ACTION(OF_POP_VLAN, 0),
206 MK_FLOW_ACTION(OF_PUSH_VLAN,
207 sizeof(struct rte_flow_action_of_push_vlan)),
208 MK_FLOW_ACTION(OF_SET_VLAN_VID,
209 sizeof(struct rte_flow_action_of_set_vlan_vid)),
210 MK_FLOW_ACTION(OF_SET_VLAN_PCP,
211 sizeof(struct rte_flow_action_of_set_vlan_pcp)),
212 MK_FLOW_ACTION(OF_POP_MPLS,
213 sizeof(struct rte_flow_action_of_pop_mpls)),
214 MK_FLOW_ACTION(OF_PUSH_MPLS,
215 sizeof(struct rte_flow_action_of_push_mpls)),
216 MK_FLOW_ACTION(VXLAN_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
217 MK_FLOW_ACTION(VXLAN_DECAP, 0),
218 MK_FLOW_ACTION(NVGRE_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
219 MK_FLOW_ACTION(NVGRE_DECAP, 0),
220 MK_FLOW_ACTION(RAW_ENCAP, sizeof(struct rte_flow_action_raw_encap)),
221 MK_FLOW_ACTION(RAW_DECAP, sizeof(struct rte_flow_action_raw_decap)),
222 MK_FLOW_ACTION(SET_IPV4_SRC,
223 sizeof(struct rte_flow_action_set_ipv4)),
224 MK_FLOW_ACTION(SET_IPV4_DST,
225 sizeof(struct rte_flow_action_set_ipv4)),
226 MK_FLOW_ACTION(SET_IPV6_SRC,
227 sizeof(struct rte_flow_action_set_ipv6)),
228 MK_FLOW_ACTION(SET_IPV6_DST,
229 sizeof(struct rte_flow_action_set_ipv6)),
230 MK_FLOW_ACTION(SET_TP_SRC,
231 sizeof(struct rte_flow_action_set_tp)),
232 MK_FLOW_ACTION(SET_TP_DST,
233 sizeof(struct rte_flow_action_set_tp)),
234 MK_FLOW_ACTION(MAC_SWAP, 0),
235 MK_FLOW_ACTION(DEC_TTL, 0),
236 MK_FLOW_ACTION(SET_TTL, sizeof(struct rte_flow_action_set_ttl)),
237 MK_FLOW_ACTION(SET_MAC_SRC, sizeof(struct rte_flow_action_set_mac)),
238 MK_FLOW_ACTION(SET_MAC_DST, sizeof(struct rte_flow_action_set_mac)),
239 MK_FLOW_ACTION(INC_TCP_SEQ, sizeof(rte_be32_t)),
240 MK_FLOW_ACTION(DEC_TCP_SEQ, sizeof(rte_be32_t)),
241 MK_FLOW_ACTION(INC_TCP_ACK, sizeof(rte_be32_t)),
242 MK_FLOW_ACTION(DEC_TCP_ACK, sizeof(rte_be32_t)),
243 MK_FLOW_ACTION(SET_TAG, sizeof(struct rte_flow_action_set_tag)),
244 MK_FLOW_ACTION(SET_META, sizeof(struct rte_flow_action_set_meta)),
245 MK_FLOW_ACTION(SET_IPV4_DSCP, sizeof(struct rte_flow_action_set_dscp)),
246 MK_FLOW_ACTION(SET_IPV6_DSCP, sizeof(struct rte_flow_action_set_dscp)),
247 MK_FLOW_ACTION(AGE, sizeof(struct rte_flow_action_age)),
248 MK_FLOW_ACTION(SAMPLE, sizeof(struct rte_flow_action_sample)),
249 MK_FLOW_ACTION(MODIFY_FIELD,
250 sizeof(struct rte_flow_action_modify_field)),
252 * Indirect action represented as handle of type
253 * (struct rte_flow_action_handle *) stored in conf field (see
254 * struct rte_flow_action); no need for additional structure to * store
255 * indirect action handle.
257 MK_FLOW_ACTION(INDIRECT, 0),
258 MK_FLOW_ACTION(CONNTRACK, sizeof(struct rte_flow_action_conntrack)),
259 MK_FLOW_ACTION(PORT_REPRESENTOR, sizeof(struct rte_flow_action_ethdev)),
260 MK_FLOW_ACTION(REPRESENTED_PORT, sizeof(struct rte_flow_action_ethdev)),
264 rte_flow_dynf_metadata_register(void)
269 static const struct rte_mbuf_dynfield desc_offs = {
270 .name = RTE_MBUF_DYNFIELD_METADATA_NAME,
271 .size = sizeof(uint32_t),
272 .align = __alignof__(uint32_t),
274 static const struct rte_mbuf_dynflag desc_flag = {
275 .name = RTE_MBUF_DYNFLAG_METADATA_NAME,
278 offset = rte_mbuf_dynfield_register(&desc_offs);
281 flag = rte_mbuf_dynflag_register(&desc_flag);
284 rte_flow_dynf_metadata_offs = offset;
285 rte_flow_dynf_metadata_mask = RTE_BIT64(flag);
289 rte_flow_dynf_metadata_offs = -1;
290 rte_flow_dynf_metadata_mask = UINT64_C(0);
295 fts_enter(struct rte_eth_dev *dev)
297 if (!(dev->data->dev_flags & RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE))
298 pthread_mutex_lock(&dev->data->flow_ops_mutex);
302 fts_exit(struct rte_eth_dev *dev)
304 if (!(dev->data->dev_flags & RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE))
305 pthread_mutex_unlock(&dev->data->flow_ops_mutex);
309 flow_err(uint16_t port_id, int ret, struct rte_flow_error *error)
313 if (rte_eth_dev_is_removed(port_id))
314 return rte_flow_error_set(error, EIO,
315 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
316 NULL, rte_strerror(EIO));
320 /* Get generic flow operations structure from a port. */
321 const struct rte_flow_ops *
322 rte_flow_ops_get(uint16_t port_id, struct rte_flow_error *error)
324 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
325 const struct rte_flow_ops *ops;
328 if (unlikely(!rte_eth_dev_is_valid_port(port_id)))
330 else if (unlikely(dev->dev_ops->flow_ops_get == NULL))
331 /* flow API not supported with this driver dev_ops */
334 code = dev->dev_ops->flow_ops_get(dev, &ops);
335 if (code == 0 && ops == NULL)
336 /* flow API not supported with this device */
340 rte_flow_error_set(error, code, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
341 NULL, rte_strerror(code));
347 /* Check whether a flow rule can be created on a given port. */
349 rte_flow_validate(uint16_t port_id,
350 const struct rte_flow_attr *attr,
351 const struct rte_flow_item pattern[],
352 const struct rte_flow_action actions[],
353 struct rte_flow_error *error)
355 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
356 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
361 if (likely(!!ops->validate)) {
363 ret = ops->validate(dev, attr, pattern, actions, error);
365 return flow_err(port_id, ret, error);
367 return rte_flow_error_set(error, ENOSYS,
368 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
369 NULL, rte_strerror(ENOSYS));
372 /* Create a flow rule on a given port. */
374 rte_flow_create(uint16_t port_id,
375 const struct rte_flow_attr *attr,
376 const struct rte_flow_item pattern[],
377 const struct rte_flow_action actions[],
378 struct rte_flow_error *error)
380 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
381 struct rte_flow *flow;
382 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
386 if (likely(!!ops->create)) {
388 flow = ops->create(dev, attr, pattern, actions, error);
391 flow_err(port_id, -rte_errno, error);
394 rte_flow_error_set(error, ENOSYS, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
395 NULL, rte_strerror(ENOSYS));
399 /* Destroy a flow rule on a given port. */
401 rte_flow_destroy(uint16_t port_id,
402 struct rte_flow *flow,
403 struct rte_flow_error *error)
405 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
406 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
411 if (likely(!!ops->destroy)) {
413 ret = ops->destroy(dev, flow, error);
415 return flow_err(port_id, ret, error);
417 return rte_flow_error_set(error, ENOSYS,
418 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
419 NULL, rte_strerror(ENOSYS));
422 /* Destroy all flow rules associated with a port. */
424 rte_flow_flush(uint16_t port_id,
425 struct rte_flow_error *error)
427 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
428 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
433 if (likely(!!ops->flush)) {
435 ret = ops->flush(dev, error);
437 return flow_err(port_id, ret, error);
439 return rte_flow_error_set(error, ENOSYS,
440 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
441 NULL, rte_strerror(ENOSYS));
444 /* Query an existing flow rule. */
446 rte_flow_query(uint16_t port_id,
447 struct rte_flow *flow,
448 const struct rte_flow_action *action,
450 struct rte_flow_error *error)
452 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
453 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
458 if (likely(!!ops->query)) {
460 ret = ops->query(dev, flow, action, data, error);
462 return flow_err(port_id, ret, error);
464 return rte_flow_error_set(error, ENOSYS,
465 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
466 NULL, rte_strerror(ENOSYS));
469 /* Restrict ingress traffic to the defined flow rules. */
471 rte_flow_isolate(uint16_t port_id,
473 struct rte_flow_error *error)
475 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
476 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
481 if (likely(!!ops->isolate)) {
483 ret = ops->isolate(dev, set, error);
485 return flow_err(port_id, ret, error);
487 return rte_flow_error_set(error, ENOSYS,
488 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
489 NULL, rte_strerror(ENOSYS));
492 /* Initialize flow error structure. */
494 rte_flow_error_set(struct rte_flow_error *error,
496 enum rte_flow_error_type type,
501 *error = (struct rte_flow_error){
511 /** Pattern item specification types. */
512 enum rte_flow_conv_item_spec_type {
513 RTE_FLOW_CONV_ITEM_SPEC,
514 RTE_FLOW_CONV_ITEM_LAST,
515 RTE_FLOW_CONV_ITEM_MASK,
519 * Copy pattern item specification.
522 * Output buffer. Can be NULL if @p size is zero.
524 * Size of @p buf in bytes.
526 * Pattern item to copy specification from.
528 * Specification selector for either @p spec, @p last or @p mask.
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_item_spec(void *buf, const size_t size,
537 const struct rte_flow_item *item,
538 enum rte_flow_conv_item_spec_type type)
542 type == RTE_FLOW_CONV_ITEM_SPEC ? item->spec :
543 type == RTE_FLOW_CONV_ITEM_LAST ? item->last :
544 type == RTE_FLOW_CONV_ITEM_MASK ? item->mask :
547 switch (item->type) {
549 const struct rte_flow_item_raw *raw;
552 const struct rte_flow_item_raw *raw;
555 const struct rte_flow_item_raw *raw;
558 const struct rte_flow_item_raw *raw;
561 struct rte_flow_item_raw *raw;
565 case RTE_FLOW_ITEM_TYPE_RAW:
566 spec.raw = item->spec;
567 last.raw = item->last ? item->last : item->spec;
568 mask.raw = item->mask ? item->mask : &rte_flow_item_raw_mask;
572 (&(struct rte_flow_item_raw){
573 .relative = src.raw->relative,
574 .search = src.raw->search,
575 .reserved = src.raw->reserved,
576 .offset = src.raw->offset,
577 .limit = src.raw->limit,
578 .length = src.raw->length,
580 size > sizeof(*dst.raw) ? sizeof(*dst.raw) : size);
581 off = sizeof(*dst.raw);
582 if (type == RTE_FLOW_CONV_ITEM_SPEC ||
583 (type == RTE_FLOW_CONV_ITEM_MASK &&
584 ((spec.raw->length & mask.raw->length) >=
585 (last.raw->length & mask.raw->length))))
586 tmp = spec.raw->length & mask.raw->length;
588 tmp = last.raw->length & mask.raw->length;
590 off = RTE_ALIGN_CEIL(off, sizeof(*dst.raw->pattern));
591 if (size >= off + tmp)
592 dst.raw->pattern = rte_memcpy
593 ((void *)((uintptr_t)dst.raw + off),
594 src.raw->pattern, tmp);
599 off = rte_flow_conv_copy(buf, data, size,
600 rte_flow_desc_item, item->type);
607 * Copy action configuration.
610 * Output buffer. Can be NULL if @p size is zero.
612 * Size of @p buf in bytes.
614 * Action to copy configuration from.
617 * Number of bytes needed to store pattern item specification regardless
618 * of @p size. @p buf contents are truncated to @p size if not large
622 rte_flow_conv_action_conf(void *buf, const size_t size,
623 const struct rte_flow_action *action)
627 switch (action->type) {
629 const struct rte_flow_action_rss *rss;
630 const struct rte_flow_action_vxlan_encap *vxlan_encap;
631 const struct rte_flow_action_nvgre_encap *nvgre_encap;
634 struct rte_flow_action_rss *rss;
635 struct rte_flow_action_vxlan_encap *vxlan_encap;
636 struct rte_flow_action_nvgre_encap *nvgre_encap;
641 case RTE_FLOW_ACTION_TYPE_RSS:
642 src.rss = action->conf;
645 (&(struct rte_flow_action_rss){
646 .func = src.rss->func,
647 .level = src.rss->level,
648 .types = src.rss->types,
649 .key_len = src.rss->key_len,
650 .queue_num = src.rss->queue_num,
652 size > sizeof(*dst.rss) ? sizeof(*dst.rss) : size);
653 off = sizeof(*dst.rss);
654 if (src.rss->key_len && src.rss->key) {
655 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->key));
656 tmp = sizeof(*src.rss->key) * src.rss->key_len;
657 if (size >= off + tmp)
658 dst.rss->key = rte_memcpy
659 ((void *)((uintptr_t)dst.rss + off),
663 if (src.rss->queue_num) {
664 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->queue));
665 tmp = sizeof(*src.rss->queue) * src.rss->queue_num;
666 if (size >= off + tmp)
667 dst.rss->queue = rte_memcpy
668 ((void *)((uintptr_t)dst.rss + off),
669 src.rss->queue, tmp);
673 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
674 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
675 src.vxlan_encap = action->conf;
676 dst.vxlan_encap = buf;
677 RTE_BUILD_BUG_ON(sizeof(*src.vxlan_encap) !=
678 sizeof(*src.nvgre_encap) ||
679 offsetof(struct rte_flow_action_vxlan_encap,
681 offsetof(struct rte_flow_action_nvgre_encap,
683 off = sizeof(*dst.vxlan_encap);
684 if (src.vxlan_encap->definition) {
686 (off, sizeof(*dst.vxlan_encap->definition));
688 (RTE_FLOW_CONV_OP_PATTERN,
689 (void *)((uintptr_t)dst.vxlan_encap + off),
690 size > off ? size - off : 0,
691 src.vxlan_encap->definition, NULL);
694 if (size >= off + ret)
695 dst.vxlan_encap->definition =
696 (void *)((uintptr_t)dst.vxlan_encap +
702 off = rte_flow_conv_copy(buf, action->conf, size,
703 rte_flow_desc_action, action->type);
710 * Copy a list of pattern items.
713 * Destination buffer. Can be NULL if @p size is zero.
715 * Size of @p dst in bytes.
717 * Source pattern items.
719 * Maximum number of pattern items to process from @p src or 0 to process
720 * the entire list. In both cases, processing stops after
721 * RTE_FLOW_ITEM_TYPE_END is encountered.
723 * Perform verbose error reporting if not NULL.
726 * A positive value representing the number of bytes needed to store
727 * pattern items regardless of @p size on success (@p buf contents are
728 * truncated to @p size if not large enough), a negative errno value
729 * otherwise and rte_errno is set.
732 rte_flow_conv_pattern(struct rte_flow_item *dst,
734 const struct rte_flow_item *src,
736 struct rte_flow_error *error)
738 uintptr_t data = (uintptr_t)dst;
743 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
745 * allow PMD private flow item
747 if (((int)src->type >= 0) &&
748 ((size_t)src->type >= RTE_DIM(rte_flow_desc_item) ||
749 !rte_flow_desc_item[src->type].name))
750 return rte_flow_error_set
751 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, src,
752 "cannot convert unknown item type");
753 if (size >= off + sizeof(*dst))
754 *dst = (struct rte_flow_item){
766 off = RTE_ALIGN_CEIL(off, sizeof(double));
767 ret = rte_flow_conv_item_spec
768 ((void *)(data + off),
769 size > off ? size - off : 0, src,
770 RTE_FLOW_CONV_ITEM_SPEC);
771 if (size && size >= off + ret)
772 dst->spec = (void *)(data + off);
777 off = RTE_ALIGN_CEIL(off, sizeof(double));
778 ret = rte_flow_conv_item_spec
779 ((void *)(data + off),
780 size > off ? size - off : 0, src,
781 RTE_FLOW_CONV_ITEM_LAST);
782 if (size && size >= off + ret)
783 dst->last = (void *)(data + off);
787 off = RTE_ALIGN_CEIL(off, sizeof(double));
788 ret = rte_flow_conv_item_spec
789 ((void *)(data + off),
790 size > off ? size - off : 0, src,
791 RTE_FLOW_CONV_ITEM_MASK);
792 if (size && size >= off + ret)
793 dst->mask = (void *)(data + off);
803 * Copy a list of actions.
806 * Destination buffer. Can be NULL if @p size is zero.
808 * Size of @p dst in bytes.
812 * Maximum number of actions to process from @p src or 0 to process the
813 * entire list. In both cases, processing stops after
814 * RTE_FLOW_ACTION_TYPE_END is encountered.
816 * Perform verbose error reporting if not NULL.
819 * A positive value representing the number of bytes needed to store
820 * actions regardless of @p size on success (@p buf contents are truncated
821 * to @p size if not large enough), a negative errno value otherwise and
825 rte_flow_conv_actions(struct rte_flow_action *dst,
827 const struct rte_flow_action *src,
829 struct rte_flow_error *error)
831 uintptr_t data = (uintptr_t)dst;
836 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
838 * allow PMD private flow action
840 if (((int)src->type >= 0) &&
841 ((size_t)src->type >= RTE_DIM(rte_flow_desc_action) ||
842 !rte_flow_desc_action[src->type].name))
843 return rte_flow_error_set
844 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
845 src, "cannot convert unknown action type");
846 if (size >= off + sizeof(*dst))
847 *dst = (struct rte_flow_action){
859 off = RTE_ALIGN_CEIL(off, sizeof(double));
860 ret = rte_flow_conv_action_conf
861 ((void *)(data + off),
862 size > off ? size - off : 0, src);
863 if (size && size >= off + ret)
864 dst->conf = (void *)(data + off);
874 * Copy flow rule components.
876 * This comprises the flow rule descriptor itself, attributes, pattern and
877 * actions list. NULL components in @p src are skipped.
880 * Destination buffer. Can be NULL if @p size is zero.
882 * Size of @p dst in bytes.
884 * Source flow rule descriptor.
886 * Perform verbose error reporting if not NULL.
889 * A positive value representing the number of bytes needed to store all
890 * components including the descriptor regardless of @p size on success
891 * (@p buf contents are truncated to @p size if not large enough), a
892 * negative errno value otherwise and rte_errno is set.
895 rte_flow_conv_rule(struct rte_flow_conv_rule *dst,
897 const struct rte_flow_conv_rule *src,
898 struct rte_flow_error *error)
904 (&(struct rte_flow_conv_rule){
909 size > sizeof(*dst) ? sizeof(*dst) : size);
912 off = RTE_ALIGN_CEIL(off, sizeof(double));
913 if (size && size >= off + sizeof(*dst->attr))
914 dst->attr = rte_memcpy
915 ((void *)((uintptr_t)dst + off),
916 src->attr_ro, sizeof(*dst->attr));
917 off += sizeof(*dst->attr);
919 if (src->pattern_ro) {
920 off = RTE_ALIGN_CEIL(off, sizeof(double));
921 ret = rte_flow_conv_pattern((void *)((uintptr_t)dst + off),
922 size > off ? size - off : 0,
923 src->pattern_ro, 0, error);
926 if (size && size >= off + (size_t)ret)
927 dst->pattern = (void *)((uintptr_t)dst + off);
930 if (src->actions_ro) {
931 off = RTE_ALIGN_CEIL(off, sizeof(double));
932 ret = rte_flow_conv_actions((void *)((uintptr_t)dst + off),
933 size > off ? size - off : 0,
934 src->actions_ro, 0, error);
937 if (size >= off + (size_t)ret)
938 dst->actions = (void *)((uintptr_t)dst + off);
945 * Retrieve the name of a pattern item/action type.
948 * Nonzero when @p src represents an action type instead of a pattern item
951 * Nonzero to write string address instead of contents into @p dst.
953 * Destination buffer. Can be NULL if @p size is zero.
955 * Size of @p dst in bytes.
957 * Depending on @p is_action, source pattern item or action type cast as a
960 * Perform verbose error reporting if not NULL.
963 * A positive value representing the number of bytes needed to store the
964 * name or its address regardless of @p size on success (@p buf contents
965 * are truncated to @p size if not large enough), a negative errno value
966 * otherwise and rte_errno is set.
969 rte_flow_conv_name(int is_action,
974 struct rte_flow_error *error)
977 const struct rte_flow_desc_data *data;
980 static const struct desc_info info_rep[2] = {
981 { rte_flow_desc_item, RTE_DIM(rte_flow_desc_item), },
982 { rte_flow_desc_action, RTE_DIM(rte_flow_desc_action), },
984 const struct desc_info *const info = &info_rep[!!is_action];
985 unsigned int type = (uintptr_t)src;
987 if (type >= info->num)
988 return rte_flow_error_set
989 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
990 "unknown object type to retrieve the name of");
992 return strlcpy(dst, info->data[type].name, size);
993 if (size >= sizeof(const char **))
994 *((const char **)dst) = info->data[type].name;
995 return sizeof(const char **);
998 /** Helper function to convert flow API objects. */
1000 rte_flow_conv(enum rte_flow_conv_op op,
1004 struct rte_flow_error *error)
1007 const struct rte_flow_attr *attr;
1009 case RTE_FLOW_CONV_OP_NONE:
1011 case RTE_FLOW_CONV_OP_ATTR:
1013 if (size > sizeof(*attr))
1014 size = sizeof(*attr);
1015 rte_memcpy(dst, attr, size);
1016 return sizeof(*attr);
1017 case RTE_FLOW_CONV_OP_ITEM:
1018 return rte_flow_conv_pattern(dst, size, src, 1, error);
1019 case RTE_FLOW_CONV_OP_ACTION:
1020 return rte_flow_conv_actions(dst, size, src, 1, error);
1021 case RTE_FLOW_CONV_OP_PATTERN:
1022 return rte_flow_conv_pattern(dst, size, src, 0, error);
1023 case RTE_FLOW_CONV_OP_ACTIONS:
1024 return rte_flow_conv_actions(dst, size, src, 0, error);
1025 case RTE_FLOW_CONV_OP_RULE:
1026 return rte_flow_conv_rule(dst, size, src, error);
1027 case RTE_FLOW_CONV_OP_ITEM_NAME:
1028 return rte_flow_conv_name(0, 0, dst, size, src, error);
1029 case RTE_FLOW_CONV_OP_ACTION_NAME:
1030 return rte_flow_conv_name(1, 0, dst, size, src, error);
1031 case RTE_FLOW_CONV_OP_ITEM_NAME_PTR:
1032 return rte_flow_conv_name(0, 1, dst, size, src, error);
1033 case RTE_FLOW_CONV_OP_ACTION_NAME_PTR:
1034 return rte_flow_conv_name(1, 1, dst, size, src, error);
1036 return rte_flow_error_set
1037 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1038 "unknown object conversion operation");
1041 /** Store a full rte_flow description. */
1043 rte_flow_copy(struct rte_flow_desc *desc, size_t len,
1044 const struct rte_flow_attr *attr,
1045 const struct rte_flow_item *items,
1046 const struct rte_flow_action *actions)
1049 * Overlap struct rte_flow_conv with struct rte_flow_desc in order
1050 * to convert the former to the latter without wasting space.
1052 struct rte_flow_conv_rule *dst =
1054 (void *)((uintptr_t)desc +
1055 (offsetof(struct rte_flow_desc, actions) -
1056 offsetof(struct rte_flow_conv_rule, actions))) :
1059 len > sizeof(*desc) - sizeof(*dst) ?
1060 len - (sizeof(*desc) - sizeof(*dst)) :
1062 struct rte_flow_conv_rule src = {
1064 .pattern_ro = items,
1065 .actions_ro = actions,
1069 RTE_BUILD_BUG_ON(sizeof(struct rte_flow_desc) <
1070 sizeof(struct rte_flow_conv_rule));
1072 (&dst->pattern != &desc->items ||
1073 &dst->actions != &desc->actions ||
1074 (uintptr_t)(dst + 1) != (uintptr_t)(desc + 1))) {
1078 ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, dst, dst_size, &src, NULL);
1081 ret += sizeof(*desc) - sizeof(*dst);
1083 (&(struct rte_flow_desc){
1086 .items = dst_size ? dst->pattern : NULL,
1087 .actions = dst_size ? dst->actions : NULL,
1089 len > sizeof(*desc) ? sizeof(*desc) : len);
1094 rte_flow_dev_dump(uint16_t port_id, struct rte_flow *flow,
1095 FILE *file, struct rte_flow_error *error)
1097 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1098 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1103 if (likely(!!ops->dev_dump)) {
1105 ret = ops->dev_dump(dev, flow, file, error);
1107 return flow_err(port_id, ret, error);
1109 return rte_flow_error_set(error, ENOSYS,
1110 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1111 NULL, rte_strerror(ENOSYS));
1115 rte_flow_get_aged_flows(uint16_t port_id, void **contexts,
1116 uint32_t nb_contexts, struct rte_flow_error *error)
1118 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1119 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1124 if (likely(!!ops->get_aged_flows)) {
1126 ret = ops->get_aged_flows(dev, contexts, nb_contexts, error);
1128 return flow_err(port_id, ret, error);
1130 return rte_flow_error_set(error, ENOTSUP,
1131 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1132 NULL, rte_strerror(ENOTSUP));
1135 struct rte_flow_action_handle *
1136 rte_flow_action_handle_create(uint16_t port_id,
1137 const struct rte_flow_indir_action_conf *conf,
1138 const struct rte_flow_action *action,
1139 struct rte_flow_error *error)
1141 struct rte_flow_action_handle *handle;
1142 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1146 if (unlikely(!ops->action_handle_create)) {
1147 rte_flow_error_set(error, ENOSYS,
1148 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1149 rte_strerror(ENOSYS));
1152 handle = ops->action_handle_create(&rte_eth_devices[port_id],
1153 conf, action, error);
1155 flow_err(port_id, -rte_errno, error);
1160 rte_flow_action_handle_destroy(uint16_t port_id,
1161 struct rte_flow_action_handle *handle,
1162 struct rte_flow_error *error)
1165 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1169 if (unlikely(!ops->action_handle_destroy))
1170 return rte_flow_error_set(error, ENOSYS,
1171 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1172 NULL, rte_strerror(ENOSYS));
1173 ret = ops->action_handle_destroy(&rte_eth_devices[port_id],
1175 return flow_err(port_id, ret, error);
1179 rte_flow_action_handle_update(uint16_t port_id,
1180 struct rte_flow_action_handle *handle,
1182 struct rte_flow_error *error)
1185 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1189 if (unlikely(!ops->action_handle_update))
1190 return rte_flow_error_set(error, ENOSYS,
1191 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1192 NULL, rte_strerror(ENOSYS));
1193 ret = ops->action_handle_update(&rte_eth_devices[port_id], handle,
1195 return flow_err(port_id, ret, error);
1199 rte_flow_action_handle_query(uint16_t port_id,
1200 const struct rte_flow_action_handle *handle,
1202 struct rte_flow_error *error)
1205 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1209 if (unlikely(!ops->action_handle_query))
1210 return rte_flow_error_set(error, ENOSYS,
1211 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1212 NULL, rte_strerror(ENOSYS));
1213 ret = ops->action_handle_query(&rte_eth_devices[port_id], handle,
1215 return flow_err(port_id, ret, error);
1219 rte_flow_tunnel_decap_set(uint16_t port_id,
1220 struct rte_flow_tunnel *tunnel,
1221 struct rte_flow_action **actions,
1222 uint32_t *num_of_actions,
1223 struct rte_flow_error *error)
1225 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1226 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1230 if (likely(!!ops->tunnel_decap_set)) {
1231 return flow_err(port_id,
1232 ops->tunnel_decap_set(dev, tunnel, actions,
1233 num_of_actions, error),
1236 return rte_flow_error_set(error, ENOTSUP,
1237 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1238 NULL, rte_strerror(ENOTSUP));
1242 rte_flow_tunnel_match(uint16_t port_id,
1243 struct rte_flow_tunnel *tunnel,
1244 struct rte_flow_item **items,
1245 uint32_t *num_of_items,
1246 struct rte_flow_error *error)
1248 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1249 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1253 if (likely(!!ops->tunnel_match)) {
1254 return flow_err(port_id,
1255 ops->tunnel_match(dev, tunnel, items,
1256 num_of_items, error),
1259 return rte_flow_error_set(error, ENOTSUP,
1260 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1261 NULL, rte_strerror(ENOTSUP));
1265 rte_flow_get_restore_info(uint16_t port_id,
1267 struct rte_flow_restore_info *restore_info,
1268 struct rte_flow_error *error)
1270 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1271 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1275 if (likely(!!ops->get_restore_info)) {
1276 return flow_err(port_id,
1277 ops->get_restore_info(dev, m, restore_info,
1281 return rte_flow_error_set(error, ENOTSUP,
1282 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1283 NULL, rte_strerror(ENOTSUP));
1287 rte_flow_tunnel_action_decap_release(uint16_t port_id,
1288 struct rte_flow_action *actions,
1289 uint32_t num_of_actions,
1290 struct rte_flow_error *error)
1292 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1293 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1297 if (likely(!!ops->tunnel_action_decap_release)) {
1298 return flow_err(port_id,
1299 ops->tunnel_action_decap_release(dev, actions,
1304 return rte_flow_error_set(error, ENOTSUP,
1305 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1306 NULL, rte_strerror(ENOTSUP));
1310 rte_flow_tunnel_item_release(uint16_t port_id,
1311 struct rte_flow_item *items,
1312 uint32_t num_of_items,
1313 struct rte_flow_error *error)
1315 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1316 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1320 if (likely(!!ops->tunnel_item_release)) {
1321 return flow_err(port_id,
1322 ops->tunnel_item_release(dev, items,
1323 num_of_items, error),
1326 return rte_flow_error_set(error, ENOTSUP,
1327 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1328 NULL, rte_strerror(ENOTSUP));
1332 rte_flow_pick_transfer_proxy(uint16_t port_id, uint16_t *proxy_port_id,
1333 struct rte_flow_error *error)
1335 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1336 struct rte_eth_dev *dev;
1338 if (unlikely(ops == NULL))
1341 if (ops->pick_transfer_proxy == NULL) {
1342 *proxy_port_id = port_id;
1346 dev = &rte_eth_devices[port_id];
1348 return flow_err(port_id,
1349 ops->pick_transfer_proxy(dev, proxy_port_id, error),
1353 struct rte_flow_item_flex_handle *
1354 rte_flow_flex_item_create(uint16_t port_id,
1355 const struct rte_flow_item_flex_conf *conf,
1356 struct rte_flow_error *error)
1358 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1359 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1360 struct rte_flow_item_flex_handle *handle;
1364 if (unlikely(!ops->flex_item_create)) {
1365 rte_flow_error_set(error, ENOTSUP,
1366 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1367 NULL, rte_strerror(ENOTSUP));
1370 handle = ops->flex_item_create(dev, conf, error);
1372 flow_err(port_id, -rte_errno, error);
1377 rte_flow_flex_item_release(uint16_t port_id,
1378 const struct rte_flow_item_flex_handle *handle,
1379 struct rte_flow_error *error)
1382 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1383 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1385 if (unlikely(!ops || !ops->flex_item_release))
1386 return rte_flow_error_set(error, ENOTSUP,
1387 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1388 NULL, rte_strerror(ENOTSUP));
1389 ret = ops->flex_item_release(dev, handle, error);
1390 return flow_err(port_id, ret, error);