1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016 6WIND S.A.
3 * Copyright 2016 Mellanox Technologies, Ltd
11 #include <rte_common.h>
12 #include <rte_errno.h>
13 #include <rte_branch_prediction.h>
14 #include <rte_string_fns.h>
16 #include <rte_mbuf_dyn.h>
17 #include "rte_ethdev.h"
18 #include "rte_flow_driver.h"
21 /* Mbuf dynamic field name for metadata. */
22 int rte_flow_dynf_metadata_offs = -1;
24 /* Mbuf dynamic field flag bit number for metadata. */
25 uint64_t rte_flow_dynf_metadata_mask;
28 * Flow elements description tables.
30 struct rte_flow_desc_data {
35 /** Generate flow_item[] entry. */
36 #define MK_FLOW_ITEM(t, s) \
37 [RTE_FLOW_ITEM_TYPE_ ## t] = { \
42 /** Information about known flow pattern items. */
43 static const struct rte_flow_desc_data rte_flow_desc_item[] = {
45 MK_FLOW_ITEM(VOID, 0),
46 MK_FLOW_ITEM(INVERT, 0),
47 MK_FLOW_ITEM(ANY, sizeof(struct rte_flow_item_any)),
49 MK_FLOW_ITEM(VF, sizeof(struct rte_flow_item_vf)),
50 MK_FLOW_ITEM(PHY_PORT, sizeof(struct rte_flow_item_phy_port)),
51 MK_FLOW_ITEM(PORT_ID, sizeof(struct rte_flow_item_port_id)),
52 MK_FLOW_ITEM(RAW, sizeof(struct rte_flow_item_raw)),
53 MK_FLOW_ITEM(ETH, sizeof(struct rte_flow_item_eth)),
54 MK_FLOW_ITEM(VLAN, sizeof(struct rte_flow_item_vlan)),
55 MK_FLOW_ITEM(IPV4, sizeof(struct rte_flow_item_ipv4)),
56 MK_FLOW_ITEM(IPV6, sizeof(struct rte_flow_item_ipv6)),
57 MK_FLOW_ITEM(ICMP, sizeof(struct rte_flow_item_icmp)),
58 MK_FLOW_ITEM(UDP, sizeof(struct rte_flow_item_udp)),
59 MK_FLOW_ITEM(TCP, sizeof(struct rte_flow_item_tcp)),
60 MK_FLOW_ITEM(SCTP, sizeof(struct rte_flow_item_sctp)),
61 MK_FLOW_ITEM(VXLAN, sizeof(struct rte_flow_item_vxlan)),
62 MK_FLOW_ITEM(E_TAG, sizeof(struct rte_flow_item_e_tag)),
63 MK_FLOW_ITEM(NVGRE, sizeof(struct rte_flow_item_nvgre)),
64 MK_FLOW_ITEM(MPLS, sizeof(struct rte_flow_item_mpls)),
65 MK_FLOW_ITEM(GRE, sizeof(struct rte_flow_item_gre)),
66 MK_FLOW_ITEM(FUZZY, sizeof(struct rte_flow_item_fuzzy)),
67 MK_FLOW_ITEM(GTP, sizeof(struct rte_flow_item_gtp)),
68 MK_FLOW_ITEM(GTPC, sizeof(struct rte_flow_item_gtp)),
69 MK_FLOW_ITEM(GTPU, sizeof(struct rte_flow_item_gtp)),
70 MK_FLOW_ITEM(ESP, sizeof(struct rte_flow_item_esp)),
71 MK_FLOW_ITEM(GENEVE, sizeof(struct rte_flow_item_geneve)),
72 MK_FLOW_ITEM(VXLAN_GPE, sizeof(struct rte_flow_item_vxlan_gpe)),
73 MK_FLOW_ITEM(ARP_ETH_IPV4, sizeof(struct rte_flow_item_arp_eth_ipv4)),
74 MK_FLOW_ITEM(IPV6_EXT, sizeof(struct rte_flow_item_ipv6_ext)),
75 MK_FLOW_ITEM(ICMP6, sizeof(struct rte_flow_item_icmp6)),
76 MK_FLOW_ITEM(ICMP6_ND_NS, sizeof(struct rte_flow_item_icmp6_nd_ns)),
77 MK_FLOW_ITEM(ICMP6_ND_NA, sizeof(struct rte_flow_item_icmp6_nd_na)),
78 MK_FLOW_ITEM(ICMP6_ND_OPT, sizeof(struct rte_flow_item_icmp6_nd_opt)),
79 MK_FLOW_ITEM(ICMP6_ND_OPT_SLA_ETH,
80 sizeof(struct rte_flow_item_icmp6_nd_opt_sla_eth)),
81 MK_FLOW_ITEM(ICMP6_ND_OPT_TLA_ETH,
82 sizeof(struct rte_flow_item_icmp6_nd_opt_tla_eth)),
83 MK_FLOW_ITEM(MARK, sizeof(struct rte_flow_item_mark)),
84 MK_FLOW_ITEM(META, sizeof(struct rte_flow_item_meta)),
85 MK_FLOW_ITEM(TAG, sizeof(struct rte_flow_item_tag)),
86 MK_FLOW_ITEM(GRE_KEY, sizeof(rte_be32_t)),
87 MK_FLOW_ITEM(GTP_PSC, sizeof(struct rte_flow_item_gtp_psc)),
88 MK_FLOW_ITEM(PPPOES, sizeof(struct rte_flow_item_pppoe)),
89 MK_FLOW_ITEM(PPPOED, sizeof(struct rte_flow_item_pppoe)),
90 MK_FLOW_ITEM(PPPOE_PROTO_ID,
91 sizeof(struct rte_flow_item_pppoe_proto_id)),
92 MK_FLOW_ITEM(NSH, sizeof(struct rte_flow_item_nsh)),
93 MK_FLOW_ITEM(IGMP, sizeof(struct rte_flow_item_igmp)),
94 MK_FLOW_ITEM(AH, sizeof(struct rte_flow_item_ah)),
95 MK_FLOW_ITEM(HIGIG2, sizeof(struct rte_flow_item_higig2_hdr)),
96 MK_FLOW_ITEM(L2TPV3OIP, sizeof(struct rte_flow_item_l2tpv3oip)),
99 /** Generate flow_action[] entry. */
100 #define MK_FLOW_ACTION(t, s) \
101 [RTE_FLOW_ACTION_TYPE_ ## t] = { \
106 /** Information about known flow actions. */
107 static const struct rte_flow_desc_data rte_flow_desc_action[] = {
108 MK_FLOW_ACTION(END, 0),
109 MK_FLOW_ACTION(VOID, 0),
110 MK_FLOW_ACTION(PASSTHRU, 0),
111 MK_FLOW_ACTION(JUMP, sizeof(struct rte_flow_action_jump)),
112 MK_FLOW_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
113 MK_FLOW_ACTION(FLAG, 0),
114 MK_FLOW_ACTION(QUEUE, sizeof(struct rte_flow_action_queue)),
115 MK_FLOW_ACTION(DROP, 0),
116 MK_FLOW_ACTION(COUNT, sizeof(struct rte_flow_action_count)),
117 MK_FLOW_ACTION(RSS, sizeof(struct rte_flow_action_rss)),
118 MK_FLOW_ACTION(PF, 0),
119 MK_FLOW_ACTION(VF, sizeof(struct rte_flow_action_vf)),
120 MK_FLOW_ACTION(PHY_PORT, sizeof(struct rte_flow_action_phy_port)),
121 MK_FLOW_ACTION(PORT_ID, sizeof(struct rte_flow_action_port_id)),
122 MK_FLOW_ACTION(METER, sizeof(struct rte_flow_action_meter)),
123 MK_FLOW_ACTION(SECURITY, sizeof(struct rte_flow_action_security)),
124 MK_FLOW_ACTION(OF_SET_MPLS_TTL,
125 sizeof(struct rte_flow_action_of_set_mpls_ttl)),
126 MK_FLOW_ACTION(OF_DEC_MPLS_TTL, 0),
127 MK_FLOW_ACTION(OF_SET_NW_TTL,
128 sizeof(struct rte_flow_action_of_set_nw_ttl)),
129 MK_FLOW_ACTION(OF_DEC_NW_TTL, 0),
130 MK_FLOW_ACTION(OF_COPY_TTL_OUT, 0),
131 MK_FLOW_ACTION(OF_COPY_TTL_IN, 0),
132 MK_FLOW_ACTION(OF_POP_VLAN, 0),
133 MK_FLOW_ACTION(OF_PUSH_VLAN,
134 sizeof(struct rte_flow_action_of_push_vlan)),
135 MK_FLOW_ACTION(OF_SET_VLAN_VID,
136 sizeof(struct rte_flow_action_of_set_vlan_vid)),
137 MK_FLOW_ACTION(OF_SET_VLAN_PCP,
138 sizeof(struct rte_flow_action_of_set_vlan_pcp)),
139 MK_FLOW_ACTION(OF_POP_MPLS,
140 sizeof(struct rte_flow_action_of_pop_mpls)),
141 MK_FLOW_ACTION(OF_PUSH_MPLS,
142 sizeof(struct rte_flow_action_of_push_mpls)),
143 MK_FLOW_ACTION(VXLAN_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
144 MK_FLOW_ACTION(VXLAN_DECAP, 0),
145 MK_FLOW_ACTION(NVGRE_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
146 MK_FLOW_ACTION(NVGRE_DECAP, 0),
147 MK_FLOW_ACTION(RAW_ENCAP, sizeof(struct rte_flow_action_raw_encap)),
148 MK_FLOW_ACTION(RAW_DECAP, sizeof(struct rte_flow_action_raw_decap)),
149 MK_FLOW_ACTION(SET_IPV4_SRC,
150 sizeof(struct rte_flow_action_set_ipv4)),
151 MK_FLOW_ACTION(SET_IPV4_DST,
152 sizeof(struct rte_flow_action_set_ipv4)),
153 MK_FLOW_ACTION(SET_IPV6_SRC,
154 sizeof(struct rte_flow_action_set_ipv6)),
155 MK_FLOW_ACTION(SET_IPV6_DST,
156 sizeof(struct rte_flow_action_set_ipv6)),
157 MK_FLOW_ACTION(SET_TP_SRC,
158 sizeof(struct rte_flow_action_set_tp)),
159 MK_FLOW_ACTION(SET_TP_DST,
160 sizeof(struct rte_flow_action_set_tp)),
161 MK_FLOW_ACTION(MAC_SWAP, 0),
162 MK_FLOW_ACTION(DEC_TTL, 0),
163 MK_FLOW_ACTION(SET_TTL, sizeof(struct rte_flow_action_set_ttl)),
164 MK_FLOW_ACTION(SET_MAC_SRC, sizeof(struct rte_flow_action_set_mac)),
165 MK_FLOW_ACTION(SET_MAC_DST, sizeof(struct rte_flow_action_set_mac)),
166 MK_FLOW_ACTION(INC_TCP_SEQ, sizeof(rte_be32_t)),
167 MK_FLOW_ACTION(DEC_TCP_SEQ, sizeof(rte_be32_t)),
168 MK_FLOW_ACTION(INC_TCP_ACK, sizeof(rte_be32_t)),
169 MK_FLOW_ACTION(DEC_TCP_ACK, sizeof(rte_be32_t)),
170 MK_FLOW_ACTION(SET_TAG, sizeof(struct rte_flow_action_set_tag)),
171 MK_FLOW_ACTION(SET_META, sizeof(struct rte_flow_action_set_meta)),
172 MK_FLOW_ACTION(SET_IPV4_DSCP, sizeof(struct rte_flow_action_set_dscp)),
173 MK_FLOW_ACTION(SET_IPV6_DSCP, sizeof(struct rte_flow_action_set_dscp)),
177 rte_flow_dynf_metadata_register(void)
182 static const struct rte_mbuf_dynfield desc_offs = {
183 .name = RTE_MBUF_DYNFIELD_METADATA_NAME,
184 .size = sizeof(uint32_t),
185 .align = __alignof__(uint32_t),
187 static const struct rte_mbuf_dynflag desc_flag = {
188 .name = RTE_MBUF_DYNFLAG_METADATA_NAME,
191 offset = rte_mbuf_dynfield_register(&desc_offs);
194 flag = rte_mbuf_dynflag_register(&desc_flag);
197 rte_flow_dynf_metadata_offs = offset;
198 rte_flow_dynf_metadata_mask = (1ULL << flag);
202 rte_flow_dynf_metadata_offs = -1;
203 rte_flow_dynf_metadata_mask = 0ULL;
208 flow_err(uint16_t port_id, int ret, struct rte_flow_error *error)
212 if (rte_eth_dev_is_removed(port_id))
213 return rte_flow_error_set(error, EIO,
214 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
215 NULL, rte_strerror(EIO));
219 static enum rte_flow_item_type
220 rte_flow_expand_rss_item_complete(const struct rte_flow_item *item)
222 enum rte_flow_item_type ret = RTE_FLOW_ITEM_TYPE_VOID;
223 uint16_t ether_type = 0;
224 uint16_t ether_type_m;
225 uint8_t ip_next_proto = 0;
226 uint8_t ip_next_proto_m;
228 if (item == NULL || item->spec == NULL)
230 switch (item->type) {
231 case RTE_FLOW_ITEM_TYPE_ETH:
233 ether_type_m = ((const struct rte_flow_item_eth *)
236 ether_type_m = rte_flow_item_eth_mask.type;
237 if (ether_type_m != RTE_BE16(0xFFFF))
239 ether_type = ((const struct rte_flow_item_eth *)
241 if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_IPV4)
242 ret = RTE_FLOW_ITEM_TYPE_IPV4;
243 else if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_IPV6)
244 ret = RTE_FLOW_ITEM_TYPE_IPV6;
245 else if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_VLAN)
246 ret = RTE_FLOW_ITEM_TYPE_VLAN;
248 case RTE_FLOW_ITEM_TYPE_VLAN:
250 ether_type_m = ((const struct rte_flow_item_vlan *)
251 (item->mask))->inner_type;
253 ether_type_m = rte_flow_item_vlan_mask.inner_type;
254 if (ether_type_m != RTE_BE16(0xFFFF))
256 ether_type = ((const struct rte_flow_item_vlan *)
257 (item->spec))->inner_type;
258 if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_IPV4)
259 ret = RTE_FLOW_ITEM_TYPE_IPV4;
260 else if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_IPV6)
261 ret = RTE_FLOW_ITEM_TYPE_IPV6;
262 else if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_VLAN)
263 ret = RTE_FLOW_ITEM_TYPE_VLAN;
265 case RTE_FLOW_ITEM_TYPE_IPV4:
267 ip_next_proto_m = ((const struct rte_flow_item_ipv4 *)
268 (item->mask))->hdr.next_proto_id;
271 rte_flow_item_ipv4_mask.hdr.next_proto_id;
272 if (ip_next_proto_m != 0xFF)
274 ip_next_proto = ((const struct rte_flow_item_ipv4 *)
275 (item->spec))->hdr.next_proto_id;
276 if (ip_next_proto == IPPROTO_UDP)
277 ret = RTE_FLOW_ITEM_TYPE_UDP;
278 else if (ip_next_proto == IPPROTO_TCP)
279 ret = RTE_FLOW_ITEM_TYPE_TCP;
280 else if (ip_next_proto == IPPROTO_IP)
281 ret = RTE_FLOW_ITEM_TYPE_IPV4;
282 else if (ip_next_proto == IPPROTO_IPV6)
283 ret = RTE_FLOW_ITEM_TYPE_IPV6;
285 case RTE_FLOW_ITEM_TYPE_IPV6:
287 ip_next_proto_m = ((const struct rte_flow_item_ipv6 *)
288 (item->mask))->hdr.proto;
291 rte_flow_item_ipv6_mask.hdr.proto;
292 if (ip_next_proto_m != 0xFF)
294 ip_next_proto = ((const struct rte_flow_item_ipv6 *)
295 (item->spec))->hdr.proto;
296 if (ip_next_proto == IPPROTO_UDP)
297 ret = RTE_FLOW_ITEM_TYPE_UDP;
298 else if (ip_next_proto == IPPROTO_TCP)
299 ret = RTE_FLOW_ITEM_TYPE_TCP;
300 else if (ip_next_proto == IPPROTO_IP)
301 ret = RTE_FLOW_ITEM_TYPE_IPV4;
302 else if (ip_next_proto == IPPROTO_IPV6)
303 ret = RTE_FLOW_ITEM_TYPE_IPV6;
306 ret = RTE_FLOW_ITEM_TYPE_VOID;
312 /* Get generic flow operations structure from a port. */
313 const struct rte_flow_ops *
314 rte_flow_ops_get(uint16_t port_id, struct rte_flow_error *error)
316 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
317 const struct rte_flow_ops *ops;
320 if (unlikely(!rte_eth_dev_is_valid_port(port_id)))
322 else if (unlikely(!dev->dev_ops->filter_ctrl ||
323 dev->dev_ops->filter_ctrl(dev,
324 RTE_ETH_FILTER_GENERIC,
331 rte_flow_error_set(error, code, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
332 NULL, rte_strerror(code));
336 /* Check whether a flow rule can be created on a given port. */
338 rte_flow_validate(uint16_t port_id,
339 const struct rte_flow_attr *attr,
340 const struct rte_flow_item pattern[],
341 const struct rte_flow_action actions[],
342 struct rte_flow_error *error)
344 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
345 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
349 if (likely(!!ops->validate))
350 return flow_err(port_id, ops->validate(dev, attr, pattern,
351 actions, error), error);
352 return rte_flow_error_set(error, ENOSYS,
353 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
354 NULL, rte_strerror(ENOSYS));
357 /* Create a flow rule on a given port. */
359 rte_flow_create(uint16_t port_id,
360 const struct rte_flow_attr *attr,
361 const struct rte_flow_item pattern[],
362 const struct rte_flow_action actions[],
363 struct rte_flow_error *error)
365 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
366 struct rte_flow *flow;
367 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
371 if (likely(!!ops->create)) {
372 flow = ops->create(dev, attr, pattern, actions, error);
374 flow_err(port_id, -rte_errno, error);
377 rte_flow_error_set(error, ENOSYS, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
378 NULL, rte_strerror(ENOSYS));
382 /* Destroy a flow rule on a given port. */
384 rte_flow_destroy(uint16_t port_id,
385 struct rte_flow *flow,
386 struct rte_flow_error *error)
388 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
389 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
393 if (likely(!!ops->destroy))
394 return flow_err(port_id, ops->destroy(dev, flow, error),
396 return rte_flow_error_set(error, ENOSYS,
397 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
398 NULL, rte_strerror(ENOSYS));
401 /* Destroy all flow rules associated with a port. */
403 rte_flow_flush(uint16_t port_id,
404 struct rte_flow_error *error)
406 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
407 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
411 if (likely(!!ops->flush))
412 return flow_err(port_id, ops->flush(dev, error), error);
413 return rte_flow_error_set(error, ENOSYS,
414 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
415 NULL, rte_strerror(ENOSYS));
418 /* Query an existing flow rule. */
420 rte_flow_query(uint16_t port_id,
421 struct rte_flow *flow,
422 const struct rte_flow_action *action,
424 struct rte_flow_error *error)
426 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
427 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
431 if (likely(!!ops->query))
432 return flow_err(port_id, ops->query(dev, flow, action, data,
434 return rte_flow_error_set(error, ENOSYS,
435 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
436 NULL, rte_strerror(ENOSYS));
439 /* Restrict ingress traffic to the defined flow rules. */
441 rte_flow_isolate(uint16_t port_id,
443 struct rte_flow_error *error)
445 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
446 const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
450 if (likely(!!ops->isolate))
451 return flow_err(port_id, ops->isolate(dev, set, error), error);
452 return rte_flow_error_set(error, ENOSYS,
453 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
454 NULL, rte_strerror(ENOSYS));
457 /* Initialize flow error structure. */
459 rte_flow_error_set(struct rte_flow_error *error,
461 enum rte_flow_error_type type,
466 *error = (struct rte_flow_error){
476 /** Pattern item specification types. */
477 enum rte_flow_conv_item_spec_type {
478 RTE_FLOW_CONV_ITEM_SPEC,
479 RTE_FLOW_CONV_ITEM_LAST,
480 RTE_FLOW_CONV_ITEM_MASK,
484 * Copy pattern item specification.
487 * Output buffer. Can be NULL if @p size is zero.
489 * Size of @p buf in bytes.
491 * Pattern item to copy specification from.
493 * Specification selector for either @p spec, @p last or @p mask.
496 * Number of bytes needed to store pattern item specification regardless
497 * of @p size. @p buf contents are truncated to @p size if not large
501 rte_flow_conv_item_spec(void *buf, const size_t size,
502 const struct rte_flow_item *item,
503 enum rte_flow_conv_item_spec_type type)
507 type == RTE_FLOW_CONV_ITEM_SPEC ? item->spec :
508 type == RTE_FLOW_CONV_ITEM_LAST ? item->last :
509 type == RTE_FLOW_CONV_ITEM_MASK ? item->mask :
512 switch (item->type) {
514 const struct rte_flow_item_raw *raw;
517 const struct rte_flow_item_raw *raw;
520 const struct rte_flow_item_raw *raw;
523 const struct rte_flow_item_raw *raw;
526 struct rte_flow_item_raw *raw;
530 case RTE_FLOW_ITEM_TYPE_RAW:
531 spec.raw = item->spec;
532 last.raw = item->last ? item->last : item->spec;
533 mask.raw = item->mask ? item->mask : &rte_flow_item_raw_mask;
537 (&(struct rte_flow_item_raw){
538 .relative = src.raw->relative,
539 .search = src.raw->search,
540 .reserved = src.raw->reserved,
541 .offset = src.raw->offset,
542 .limit = src.raw->limit,
543 .length = src.raw->length,
545 size > sizeof(*dst.raw) ? sizeof(*dst.raw) : size);
546 off = sizeof(*dst.raw);
547 if (type == RTE_FLOW_CONV_ITEM_SPEC ||
548 (type == RTE_FLOW_CONV_ITEM_MASK &&
549 ((spec.raw->length & mask.raw->length) >=
550 (last.raw->length & mask.raw->length))))
551 tmp = spec.raw->length & mask.raw->length;
553 tmp = last.raw->length & mask.raw->length;
555 off = RTE_ALIGN_CEIL(off, sizeof(*dst.raw->pattern));
556 if (size >= off + tmp)
557 dst.raw->pattern = rte_memcpy
558 ((void *)((uintptr_t)dst.raw + off),
559 src.raw->pattern, tmp);
564 off = rte_flow_desc_item[item->type].size;
565 rte_memcpy(buf, data, (size > off ? off : size));
572 * Copy action configuration.
575 * Output buffer. Can be NULL if @p size is zero.
577 * Size of @p buf in bytes.
579 * Action to copy configuration from.
582 * Number of bytes needed to store pattern item specification regardless
583 * of @p size. @p buf contents are truncated to @p size if not large
587 rte_flow_conv_action_conf(void *buf, const size_t size,
588 const struct rte_flow_action *action)
592 switch (action->type) {
594 const struct rte_flow_action_rss *rss;
595 const struct rte_flow_action_vxlan_encap *vxlan_encap;
596 const struct rte_flow_action_nvgre_encap *nvgre_encap;
599 struct rte_flow_action_rss *rss;
600 struct rte_flow_action_vxlan_encap *vxlan_encap;
601 struct rte_flow_action_nvgre_encap *nvgre_encap;
606 case RTE_FLOW_ACTION_TYPE_RSS:
607 src.rss = action->conf;
610 (&(struct rte_flow_action_rss){
611 .func = src.rss->func,
612 .level = src.rss->level,
613 .types = src.rss->types,
614 .key_len = src.rss->key_len,
615 .queue_num = src.rss->queue_num,
617 size > sizeof(*dst.rss) ? sizeof(*dst.rss) : size);
618 off = sizeof(*dst.rss);
619 if (src.rss->key_len) {
620 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->key));
621 tmp = sizeof(*src.rss->key) * src.rss->key_len;
622 if (size >= off + tmp)
623 dst.rss->key = rte_memcpy
624 ((void *)((uintptr_t)dst.rss + off),
628 if (src.rss->queue_num) {
629 off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->queue));
630 tmp = sizeof(*src.rss->queue) * src.rss->queue_num;
631 if (size >= off + tmp)
632 dst.rss->queue = rte_memcpy
633 ((void *)((uintptr_t)dst.rss + off),
634 src.rss->queue, tmp);
638 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
639 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
640 src.vxlan_encap = action->conf;
641 dst.vxlan_encap = buf;
642 RTE_BUILD_BUG_ON(sizeof(*src.vxlan_encap) !=
643 sizeof(*src.nvgre_encap) ||
644 offsetof(struct rte_flow_action_vxlan_encap,
646 offsetof(struct rte_flow_action_nvgre_encap,
648 off = sizeof(*dst.vxlan_encap);
649 if (src.vxlan_encap->definition) {
651 (off, sizeof(*dst.vxlan_encap->definition));
653 (RTE_FLOW_CONV_OP_PATTERN,
654 (void *)((uintptr_t)dst.vxlan_encap + off),
655 size > off ? size - off : 0,
656 src.vxlan_encap->definition, NULL);
659 if (size >= off + ret)
660 dst.vxlan_encap->definition =
661 (void *)((uintptr_t)dst.vxlan_encap +
667 off = rte_flow_desc_action[action->type].size;
668 rte_memcpy(buf, action->conf, (size > off ? off : size));
675 * Copy a list of pattern items.
678 * Destination buffer. Can be NULL if @p size is zero.
680 * Size of @p dst in bytes.
682 * Source pattern items.
684 * Maximum number of pattern items to process from @p src or 0 to process
685 * the entire list. In both cases, processing stops after
686 * RTE_FLOW_ITEM_TYPE_END is encountered.
688 * Perform verbose error reporting if not NULL.
691 * A positive value representing the number of bytes needed to store
692 * pattern items regardless of @p size on success (@p buf contents are
693 * truncated to @p size if not large enough), a negative errno value
694 * otherwise and rte_errno is set.
697 rte_flow_conv_pattern(struct rte_flow_item *dst,
699 const struct rte_flow_item *src,
701 struct rte_flow_error *error)
703 uintptr_t data = (uintptr_t)dst;
708 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
709 if ((size_t)src->type >= RTE_DIM(rte_flow_desc_item) ||
710 !rte_flow_desc_item[src->type].name)
711 return rte_flow_error_set
712 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, src,
713 "cannot convert unknown item type");
714 if (size >= off + sizeof(*dst))
715 *dst = (struct rte_flow_item){
727 off = RTE_ALIGN_CEIL(off, sizeof(double));
728 ret = rte_flow_conv_item_spec
729 ((void *)(data + off),
730 size > off ? size - off : 0, src,
731 RTE_FLOW_CONV_ITEM_SPEC);
732 if (size && size >= off + ret)
733 dst->spec = (void *)(data + off);
738 off = RTE_ALIGN_CEIL(off, sizeof(double));
739 ret = rte_flow_conv_item_spec
740 ((void *)(data + off),
741 size > off ? size - off : 0, src,
742 RTE_FLOW_CONV_ITEM_LAST);
743 if (size && size >= off + ret)
744 dst->last = (void *)(data + off);
748 off = RTE_ALIGN_CEIL(off, sizeof(double));
749 ret = rte_flow_conv_item_spec
750 ((void *)(data + off),
751 size > off ? size - off : 0, src,
752 RTE_FLOW_CONV_ITEM_MASK);
753 if (size && size >= off + ret)
754 dst->mask = (void *)(data + off);
764 * Copy a list of actions.
767 * Destination buffer. Can be NULL if @p size is zero.
769 * Size of @p dst in bytes.
773 * Maximum number of actions to process from @p src or 0 to process the
774 * entire list. In both cases, processing stops after
775 * RTE_FLOW_ACTION_TYPE_END is encountered.
777 * Perform verbose error reporting if not NULL.
780 * A positive value representing the number of bytes needed to store
781 * actions regardless of @p size on success (@p buf contents are truncated
782 * to @p size if not large enough), a negative errno value otherwise and
786 rte_flow_conv_actions(struct rte_flow_action *dst,
788 const struct rte_flow_action *src,
790 struct rte_flow_error *error)
792 uintptr_t data = (uintptr_t)dst;
797 for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
798 if ((size_t)src->type >= RTE_DIM(rte_flow_desc_action) ||
799 !rte_flow_desc_action[src->type].name)
800 return rte_flow_error_set
801 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
802 src, "cannot convert unknown action type");
803 if (size >= off + sizeof(*dst))
804 *dst = (struct rte_flow_action){
816 off = RTE_ALIGN_CEIL(off, sizeof(double));
817 ret = rte_flow_conv_action_conf
818 ((void *)(data + off),
819 size > off ? size - off : 0, src);
820 if (size && size >= off + ret)
821 dst->conf = (void *)(data + off);
831 * Copy flow rule components.
833 * This comprises the flow rule descriptor itself, attributes, pattern and
834 * actions list. NULL components in @p src are skipped.
837 * Destination buffer. Can be NULL if @p size is zero.
839 * Size of @p dst in bytes.
841 * Source flow rule descriptor.
843 * Perform verbose error reporting if not NULL.
846 * A positive value representing the number of bytes needed to store all
847 * components including the descriptor regardless of @p size on success
848 * (@p buf contents are truncated to @p size if not large enough), a
849 * negative errno value otherwise and rte_errno is set.
852 rte_flow_conv_rule(struct rte_flow_conv_rule *dst,
854 const struct rte_flow_conv_rule *src,
855 struct rte_flow_error *error)
861 (&(struct rte_flow_conv_rule){
866 size > sizeof(*dst) ? sizeof(*dst) : size);
869 off = RTE_ALIGN_CEIL(off, sizeof(double));
870 if (size && size >= off + sizeof(*dst->attr))
871 dst->attr = rte_memcpy
872 ((void *)((uintptr_t)dst + off),
873 src->attr_ro, sizeof(*dst->attr));
874 off += sizeof(*dst->attr);
876 if (src->pattern_ro) {
877 off = RTE_ALIGN_CEIL(off, sizeof(double));
878 ret = rte_flow_conv_pattern((void *)((uintptr_t)dst + off),
879 size > off ? size - off : 0,
880 src->pattern_ro, 0, error);
883 if (size && size >= off + (size_t)ret)
884 dst->pattern = (void *)((uintptr_t)dst + off);
887 if (src->actions_ro) {
888 off = RTE_ALIGN_CEIL(off, sizeof(double));
889 ret = rte_flow_conv_actions((void *)((uintptr_t)dst + off),
890 size > off ? size - off : 0,
891 src->actions_ro, 0, error);
894 if (size >= off + (size_t)ret)
895 dst->actions = (void *)((uintptr_t)dst + off);
902 * Retrieve the name of a pattern item/action type.
905 * Nonzero when @p src represents an action type instead of a pattern item
908 * Nonzero to write string address instead of contents into @p dst.
910 * Destination buffer. Can be NULL if @p size is zero.
912 * Size of @p dst in bytes.
914 * Depending on @p is_action, source pattern item or action type cast as a
917 * Perform verbose error reporting if not NULL.
920 * A positive value representing the number of bytes needed to store the
921 * name or its address regardless of @p size on success (@p buf contents
922 * are truncated to @p size if not large enough), a negative errno value
923 * otherwise and rte_errno is set.
926 rte_flow_conv_name(int is_action,
931 struct rte_flow_error *error)
934 const struct rte_flow_desc_data *data;
937 static const struct desc_info info_rep[2] = {
938 { rte_flow_desc_item, RTE_DIM(rte_flow_desc_item), },
939 { rte_flow_desc_action, RTE_DIM(rte_flow_desc_action), },
941 const struct desc_info *const info = &info_rep[!!is_action];
942 unsigned int type = (uintptr_t)src;
944 if (type >= info->num)
945 return rte_flow_error_set
946 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
947 "unknown object type to retrieve the name of");
949 return strlcpy(dst, info->data[type].name, size);
950 if (size >= sizeof(const char **))
951 *((const char **)dst) = info->data[type].name;
952 return sizeof(const char **);
955 /** Helper function to convert flow API objects. */
957 rte_flow_conv(enum rte_flow_conv_op op,
961 struct rte_flow_error *error)
964 const struct rte_flow_attr *attr;
966 case RTE_FLOW_CONV_OP_NONE:
968 case RTE_FLOW_CONV_OP_ATTR:
970 if (size > sizeof(*attr))
971 size = sizeof(*attr);
972 rte_memcpy(dst, attr, size);
973 return sizeof(*attr);
974 case RTE_FLOW_CONV_OP_ITEM:
975 return rte_flow_conv_pattern(dst, size, src, 1, error);
976 case RTE_FLOW_CONV_OP_ACTION:
977 return rte_flow_conv_actions(dst, size, src, 1, error);
978 case RTE_FLOW_CONV_OP_PATTERN:
979 return rte_flow_conv_pattern(dst, size, src, 0, error);
980 case RTE_FLOW_CONV_OP_ACTIONS:
981 return rte_flow_conv_actions(dst, size, src, 0, error);
982 case RTE_FLOW_CONV_OP_RULE:
983 return rte_flow_conv_rule(dst, size, src, error);
984 case RTE_FLOW_CONV_OP_ITEM_NAME:
985 return rte_flow_conv_name(0, 0, dst, size, src, error);
986 case RTE_FLOW_CONV_OP_ACTION_NAME:
987 return rte_flow_conv_name(1, 0, dst, size, src, error);
988 case RTE_FLOW_CONV_OP_ITEM_NAME_PTR:
989 return rte_flow_conv_name(0, 1, dst, size, src, error);
990 case RTE_FLOW_CONV_OP_ACTION_NAME_PTR:
991 return rte_flow_conv_name(1, 1, dst, size, src, error);
993 return rte_flow_error_set
994 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
995 "unknown object conversion operation");
998 /** Store a full rte_flow description. */
1000 rte_flow_copy(struct rte_flow_desc *desc, size_t len,
1001 const struct rte_flow_attr *attr,
1002 const struct rte_flow_item *items,
1003 const struct rte_flow_action *actions)
1006 * Overlap struct rte_flow_conv with struct rte_flow_desc in order
1007 * to convert the former to the latter without wasting space.
1009 struct rte_flow_conv_rule *dst =
1011 (void *)((uintptr_t)desc +
1012 (offsetof(struct rte_flow_desc, actions) -
1013 offsetof(struct rte_flow_conv_rule, actions))) :
1016 len > sizeof(*desc) - sizeof(*dst) ?
1017 len - (sizeof(*desc) - sizeof(*dst)) :
1019 struct rte_flow_conv_rule src = {
1021 .pattern_ro = items,
1022 .actions_ro = actions,
1026 RTE_BUILD_BUG_ON(sizeof(struct rte_flow_desc) <
1027 sizeof(struct rte_flow_conv_rule));
1029 (&dst->pattern != &desc->items ||
1030 &dst->actions != &desc->actions ||
1031 (uintptr_t)(dst + 1) != (uintptr_t)(desc + 1))) {
1035 ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, dst, dst_size, &src, NULL);
1038 ret += sizeof(*desc) - sizeof(*dst);
1040 (&(struct rte_flow_desc){
1043 .items = dst_size ? dst->pattern : NULL,
1044 .actions = dst_size ? dst->actions : NULL,
1046 len > sizeof(*desc) ? sizeof(*desc) : len);
1051 * Expand RSS flows into several possible flows according to the RSS hash
1052 * fields requested and the driver capabilities.
1055 rte_flow_expand_rss(struct rte_flow_expand_rss *buf, size_t size,
1056 const struct rte_flow_item *pattern, uint64_t types,
1057 const struct rte_flow_expand_node graph[],
1058 int graph_root_index)
1060 const int elt_n = 8;
1061 const struct rte_flow_item *item;
1062 const struct rte_flow_expand_node *node = &graph[graph_root_index];
1063 const int *next_node;
1064 const int *stack[elt_n];
1066 struct rte_flow_item flow_items[elt_n];
1069 size_t user_pattern_size = 0;
1071 const struct rte_flow_expand_node *next = NULL;
1072 struct rte_flow_item missed_item;
1075 const struct rte_flow_item *last_item = NULL;
1077 memset(&missed_item, 0, sizeof(missed_item));
1078 lsize = offsetof(struct rte_flow_expand_rss, entry) +
1079 elt_n * sizeof(buf->entry[0]);
1080 if (lsize <= size) {
1081 buf->entry[0].priority = 0;
1082 buf->entry[0].pattern = (void *)&buf->entry[elt_n];
1084 addr = buf->entry[0].pattern;
1086 for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
1087 if (item->type != RTE_FLOW_ITEM_TYPE_VOID)
1089 for (i = 0; node->next && node->next[i]; ++i) {
1090 next = &graph[node->next[i]];
1091 if (next->type == item->type)
1096 user_pattern_size += sizeof(*item);
1098 user_pattern_size += sizeof(*item); /* Handle END item. */
1099 lsize += user_pattern_size;
1100 /* Copy the user pattern in the first entry of the buffer. */
1101 if (lsize <= size) {
1102 rte_memcpy(addr, pattern, user_pattern_size);
1103 addr = (void *)(((uintptr_t)addr) + user_pattern_size);
1106 /* Start expanding. */
1107 memset(flow_items, 0, sizeof(flow_items));
1108 user_pattern_size -= sizeof(*item);
1110 * Check if the last valid item has spec set
1111 * and need complete pattern.
1113 missed_item.type = rte_flow_expand_rss_item_complete(last_item);
1114 if (missed_item.type != RTE_FLOW_ITEM_TYPE_VOID) {
1117 for (i = 0; node->next && node->next[i]; ++i) {
1118 next = &graph[node->next[i]];
1119 if (next->type == missed_item.type) {
1120 flow_items[0].type = missed_item.type;
1121 flow_items[1].type = RTE_FLOW_ITEM_TYPE_END;
1127 if (next && missed) {
1128 elt = 2; /* missed item + item end. */
1130 lsize += elt * sizeof(*item) + user_pattern_size;
1131 if ((node->rss_types & types) && lsize <= size) {
1132 buf->entry[buf->entries].priority = 1;
1133 buf->entry[buf->entries].pattern = addr;
1135 rte_memcpy(addr, buf->entry[0].pattern,
1137 addr = (void *)(((uintptr_t)addr) + user_pattern_size);
1138 rte_memcpy(addr, flow_items, elt * sizeof(*item));
1139 addr = (void *)(((uintptr_t)addr) +
1140 elt * sizeof(*item));
1143 memset(flow_items, 0, sizeof(flow_items));
1144 next_node = node->next;
1145 stack[stack_pos] = next_node;
1146 node = next_node ? &graph[*next_node] : NULL;
1148 flow_items[stack_pos].type = node->type;
1149 if (node->rss_types & types) {
1151 * compute the number of items to copy from the
1152 * expansion and copy it.
1153 * When the stack_pos is 0, there are 1 element in it,
1154 * plus the addition END item.
1156 elt = stack_pos + 2;
1157 flow_items[stack_pos + 1].type = RTE_FLOW_ITEM_TYPE_END;
1158 lsize += elt * sizeof(*item) + user_pattern_size;
1159 if (lsize <= size) {
1160 size_t n = elt * sizeof(*item);
1162 buf->entry[buf->entries].priority =
1163 stack_pos + 1 + missed;
1164 buf->entry[buf->entries].pattern = addr;
1166 rte_memcpy(addr, buf->entry[0].pattern,
1168 addr = (void *)(((uintptr_t)addr) +
1170 rte_memcpy(addr, &missed_item,
1171 missed * sizeof(*item));
1172 addr = (void *)(((uintptr_t)addr) +
1173 missed * sizeof(*item));
1174 rte_memcpy(addr, flow_items, n);
1175 addr = (void *)(((uintptr_t)addr) + n);
1180 next_node = node->next;
1181 if (stack_pos++ == elt_n) {
1185 stack[stack_pos] = next_node;
1186 } else if (*(next_node + 1)) {
1187 /* Follow up with the next possibility. */
1190 /* Move to the next path. */
1192 next_node = stack[--stack_pos];
1194 stack[stack_pos] = next_node;
1196 node = *next_node ? &graph[*next_node] : NULL;
1198 /* no expanded flows but we have missed item, create one rule for it */
1199 if (buf->entries == 1 && missed != 0) {
1201 lsize += elt * sizeof(*item) + user_pattern_size;
1202 if (lsize <= size) {
1203 buf->entry[buf->entries].priority = 1;
1204 buf->entry[buf->entries].pattern = addr;
1206 flow_items[0].type = missed_item.type;
1207 flow_items[1].type = RTE_FLOW_ITEM_TYPE_END;
1208 rte_memcpy(addr, buf->entry[0].pattern,
1210 addr = (void *)(((uintptr_t)addr) + user_pattern_size);
1211 rte_memcpy(addr, flow_items, elt * sizeof(*item));
1212 addr = (void *)(((uintptr_t)addr) +
1213 elt * sizeof(*item));