ethdev: add IPv4/IPv6 DSCP rewrite action
[dpdk.git] / lib / librte_ethdev / rte_flow.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016 6WIND S.A.
3  * Copyright 2016 Mellanox Technologies, Ltd
4  */
5
6 #include <errno.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <string.h>
10
11 #include <rte_common.h>
12 #include <rte_errno.h>
13 #include <rte_branch_prediction.h>
14 #include <rte_string_fns.h>
15 #include <rte_mbuf.h>
16 #include <rte_mbuf_dyn.h>
17 #include "rte_ethdev.h"
18 #include "rte_flow_driver.h"
19 #include "rte_flow.h"
20
21 /* Mbuf dynamic field name for metadata. */
22 int rte_flow_dynf_metadata_offs = -1;
23
24 /* Mbuf dynamic field flag bit number for metadata. */
25 uint64_t rte_flow_dynf_metadata_mask;
26
27 /**
28  * Flow elements description tables.
29  */
30 struct rte_flow_desc_data {
31         const char *name;
32         size_t size;
33 };
34
35 /** Generate flow_item[] entry. */
36 #define MK_FLOW_ITEM(t, s) \
37         [RTE_FLOW_ITEM_TYPE_ ## t] = { \
38                 .name = # t, \
39                 .size = s, \
40         }
41
42 /** Information about known flow pattern items. */
43 static const struct rte_flow_desc_data rte_flow_desc_item[] = {
44         MK_FLOW_ITEM(END, 0),
45         MK_FLOW_ITEM(VOID, 0),
46         MK_FLOW_ITEM(INVERT, 0),
47         MK_FLOW_ITEM(ANY, sizeof(struct rte_flow_item_any)),
48         MK_FLOW_ITEM(PF, 0),
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 };
97
98 /** Generate flow_action[] entry. */
99 #define MK_FLOW_ACTION(t, s) \
100         [RTE_FLOW_ACTION_TYPE_ ## t] = { \
101                 .name = # t, \
102                 .size = s, \
103         }
104
105 /** Information about known flow actions. */
106 static const struct rte_flow_desc_data rte_flow_desc_action[] = {
107         MK_FLOW_ACTION(END, 0),
108         MK_FLOW_ACTION(VOID, 0),
109         MK_FLOW_ACTION(PASSTHRU, 0),
110         MK_FLOW_ACTION(JUMP, sizeof(struct rte_flow_action_jump)),
111         MK_FLOW_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
112         MK_FLOW_ACTION(FLAG, 0),
113         MK_FLOW_ACTION(QUEUE, sizeof(struct rte_flow_action_queue)),
114         MK_FLOW_ACTION(DROP, 0),
115         MK_FLOW_ACTION(COUNT, sizeof(struct rte_flow_action_count)),
116         MK_FLOW_ACTION(RSS, sizeof(struct rte_flow_action_rss)),
117         MK_FLOW_ACTION(PF, 0),
118         MK_FLOW_ACTION(VF, sizeof(struct rte_flow_action_vf)),
119         MK_FLOW_ACTION(PHY_PORT, sizeof(struct rte_flow_action_phy_port)),
120         MK_FLOW_ACTION(PORT_ID, sizeof(struct rte_flow_action_port_id)),
121         MK_FLOW_ACTION(METER, sizeof(struct rte_flow_action_meter)),
122         MK_FLOW_ACTION(SECURITY, sizeof(struct rte_flow_action_security)),
123         MK_FLOW_ACTION(OF_SET_MPLS_TTL,
124                        sizeof(struct rte_flow_action_of_set_mpls_ttl)),
125         MK_FLOW_ACTION(OF_DEC_MPLS_TTL, 0),
126         MK_FLOW_ACTION(OF_SET_NW_TTL,
127                        sizeof(struct rte_flow_action_of_set_nw_ttl)),
128         MK_FLOW_ACTION(OF_DEC_NW_TTL, 0),
129         MK_FLOW_ACTION(OF_COPY_TTL_OUT, 0),
130         MK_FLOW_ACTION(OF_COPY_TTL_IN, 0),
131         MK_FLOW_ACTION(OF_POP_VLAN, 0),
132         MK_FLOW_ACTION(OF_PUSH_VLAN,
133                        sizeof(struct rte_flow_action_of_push_vlan)),
134         MK_FLOW_ACTION(OF_SET_VLAN_VID,
135                        sizeof(struct rte_flow_action_of_set_vlan_vid)),
136         MK_FLOW_ACTION(OF_SET_VLAN_PCP,
137                        sizeof(struct rte_flow_action_of_set_vlan_pcp)),
138         MK_FLOW_ACTION(OF_POP_MPLS,
139                        sizeof(struct rte_flow_action_of_pop_mpls)),
140         MK_FLOW_ACTION(OF_PUSH_MPLS,
141                        sizeof(struct rte_flow_action_of_push_mpls)),
142         MK_FLOW_ACTION(VXLAN_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
143         MK_FLOW_ACTION(VXLAN_DECAP, 0),
144         MK_FLOW_ACTION(NVGRE_ENCAP, sizeof(struct rte_flow_action_vxlan_encap)),
145         MK_FLOW_ACTION(NVGRE_DECAP, 0),
146         MK_FLOW_ACTION(RAW_ENCAP, sizeof(struct rte_flow_action_raw_encap)),
147         MK_FLOW_ACTION(RAW_DECAP, sizeof(struct rte_flow_action_raw_decap)),
148         MK_FLOW_ACTION(SET_IPV4_SRC,
149                        sizeof(struct rte_flow_action_set_ipv4)),
150         MK_FLOW_ACTION(SET_IPV4_DST,
151                        sizeof(struct rte_flow_action_set_ipv4)),
152         MK_FLOW_ACTION(SET_IPV6_SRC,
153                        sizeof(struct rte_flow_action_set_ipv6)),
154         MK_FLOW_ACTION(SET_IPV6_DST,
155                        sizeof(struct rte_flow_action_set_ipv6)),
156         MK_FLOW_ACTION(SET_TP_SRC,
157                        sizeof(struct rte_flow_action_set_tp)),
158         MK_FLOW_ACTION(SET_TP_DST,
159                        sizeof(struct rte_flow_action_set_tp)),
160         MK_FLOW_ACTION(MAC_SWAP, 0),
161         MK_FLOW_ACTION(DEC_TTL, 0),
162         MK_FLOW_ACTION(SET_TTL, sizeof(struct rte_flow_action_set_ttl)),
163         MK_FLOW_ACTION(SET_MAC_SRC, sizeof(struct rte_flow_action_set_mac)),
164         MK_FLOW_ACTION(SET_MAC_DST, sizeof(struct rte_flow_action_set_mac)),
165         MK_FLOW_ACTION(INC_TCP_SEQ, sizeof(rte_be32_t)),
166         MK_FLOW_ACTION(DEC_TCP_SEQ, sizeof(rte_be32_t)),
167         MK_FLOW_ACTION(INC_TCP_ACK, sizeof(rte_be32_t)),
168         MK_FLOW_ACTION(DEC_TCP_ACK, sizeof(rte_be32_t)),
169         MK_FLOW_ACTION(SET_TAG, sizeof(struct rte_flow_action_set_tag)),
170         MK_FLOW_ACTION(SET_META, sizeof(struct rte_flow_action_set_meta)),
171         MK_FLOW_ACTION(SET_IPV4_DSCP, sizeof(struct rte_flow_action_set_dscp)),
172         MK_FLOW_ACTION(SET_IPV6_DSCP, sizeof(struct rte_flow_action_set_dscp)),
173 };
174
175 int
176 rte_flow_dynf_metadata_register(void)
177 {
178         int offset;
179         int flag;
180
181         static const struct rte_mbuf_dynfield desc_offs = {
182                 .name = RTE_MBUF_DYNFIELD_METADATA_NAME,
183                 .size = sizeof(uint32_t),
184                 .align = __alignof__(uint32_t),
185         };
186         static const struct rte_mbuf_dynflag desc_flag = {
187                 .name = RTE_MBUF_DYNFLAG_METADATA_NAME,
188         };
189
190         offset = rte_mbuf_dynfield_register(&desc_offs);
191         if (offset < 0)
192                 goto error;
193         flag = rte_mbuf_dynflag_register(&desc_flag);
194         if (flag < 0)
195                 goto error;
196         rte_flow_dynf_metadata_offs = offset;
197         rte_flow_dynf_metadata_mask = (1ULL << flag);
198         return 0;
199
200 error:
201         rte_flow_dynf_metadata_offs = -1;
202         rte_flow_dynf_metadata_mask = 0ULL;
203         return -rte_errno;
204 }
205
206 static int
207 flow_err(uint16_t port_id, int ret, struct rte_flow_error *error)
208 {
209         if (ret == 0)
210                 return 0;
211         if (rte_eth_dev_is_removed(port_id))
212                 return rte_flow_error_set(error, EIO,
213                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
214                                           NULL, rte_strerror(EIO));
215         return ret;
216 }
217
218 static enum rte_flow_item_type
219 rte_flow_expand_rss_item_complete(const struct rte_flow_item *item)
220 {
221         enum rte_flow_item_type ret = RTE_FLOW_ITEM_TYPE_VOID;
222         uint16_t ether_type = 0;
223         uint16_t ether_type_m;
224         uint8_t ip_next_proto = 0;
225         uint8_t ip_next_proto_m;
226
227         if (item == NULL || item->spec == NULL)
228                 return ret;
229         switch (item->type) {
230         case RTE_FLOW_ITEM_TYPE_ETH:
231                 if (item->mask)
232                         ether_type_m = ((const struct rte_flow_item_eth *)
233                                                 (item->mask))->type;
234                 else
235                         ether_type_m = rte_flow_item_eth_mask.type;
236                 if (ether_type_m != RTE_BE16(0xFFFF))
237                         break;
238                 ether_type = ((const struct rte_flow_item_eth *)
239                                 (item->spec))->type;
240                 if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_IPV4)
241                         ret = RTE_FLOW_ITEM_TYPE_IPV4;
242                 else if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_IPV6)
243                         ret = RTE_FLOW_ITEM_TYPE_IPV6;
244                 else if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_VLAN)
245                         ret = RTE_FLOW_ITEM_TYPE_VLAN;
246                 break;
247         case RTE_FLOW_ITEM_TYPE_VLAN:
248                 if (item->mask)
249                         ether_type_m = ((const struct rte_flow_item_vlan *)
250                                                 (item->mask))->inner_type;
251                 else
252                         ether_type_m = rte_flow_item_vlan_mask.inner_type;
253                 if (ether_type_m != RTE_BE16(0xFFFF))
254                         break;
255                 ether_type = ((const struct rte_flow_item_vlan *)
256                                 (item->spec))->inner_type;
257                 if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_IPV4)
258                         ret = RTE_FLOW_ITEM_TYPE_IPV4;
259                 else if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_IPV6)
260                         ret = RTE_FLOW_ITEM_TYPE_IPV6;
261                 else if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_VLAN)
262                         ret = RTE_FLOW_ITEM_TYPE_VLAN;
263                 break;
264         case RTE_FLOW_ITEM_TYPE_IPV4:
265                 if (item->mask)
266                         ip_next_proto_m = ((const struct rte_flow_item_ipv4 *)
267                                         (item->mask))->hdr.next_proto_id;
268                 else
269                         ip_next_proto_m =
270                                 rte_flow_item_ipv4_mask.hdr.next_proto_id;
271                 if (ip_next_proto_m != 0xFF)
272                         break;
273                 ip_next_proto = ((const struct rte_flow_item_ipv4 *)
274                                 (item->spec))->hdr.next_proto_id;
275                 if (ip_next_proto == IPPROTO_UDP)
276                         ret = RTE_FLOW_ITEM_TYPE_UDP;
277                 else if (ip_next_proto == IPPROTO_TCP)
278                         ret = RTE_FLOW_ITEM_TYPE_TCP;
279                 else if (ip_next_proto == IPPROTO_IP)
280                         ret = RTE_FLOW_ITEM_TYPE_IPV4;
281                 else if (ip_next_proto == IPPROTO_IPV6)
282                         ret = RTE_FLOW_ITEM_TYPE_IPV6;
283                 break;
284         case RTE_FLOW_ITEM_TYPE_IPV6:
285                 if (item->mask)
286                         ip_next_proto_m = ((const struct rte_flow_item_ipv6 *)
287                                                 (item->mask))->hdr.proto;
288                 else
289                         ip_next_proto_m =
290                                 rte_flow_item_ipv6_mask.hdr.proto;
291                 if (ip_next_proto_m != 0xFF)
292                         break;
293                 ip_next_proto = ((const struct rte_flow_item_ipv6 *)
294                                 (item->spec))->hdr.proto;
295                 if (ip_next_proto == IPPROTO_UDP)
296                         ret = RTE_FLOW_ITEM_TYPE_UDP;
297                 else if (ip_next_proto == IPPROTO_TCP)
298                         ret = RTE_FLOW_ITEM_TYPE_TCP;
299                 else if (ip_next_proto == IPPROTO_IP)
300                         ret = RTE_FLOW_ITEM_TYPE_IPV4;
301                 else if (ip_next_proto == IPPROTO_IPV6)
302                         ret = RTE_FLOW_ITEM_TYPE_IPV6;
303                 break;
304         default:
305                 ret = RTE_FLOW_ITEM_TYPE_VOID;
306                 break;
307         }
308         return ret;
309 }
310
311 /* Get generic flow operations structure from a port. */
312 const struct rte_flow_ops *
313 rte_flow_ops_get(uint16_t port_id, struct rte_flow_error *error)
314 {
315         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
316         const struct rte_flow_ops *ops;
317         int code;
318
319         if (unlikely(!rte_eth_dev_is_valid_port(port_id)))
320                 code = ENODEV;
321         else if (unlikely(!dev->dev_ops->filter_ctrl ||
322                           dev->dev_ops->filter_ctrl(dev,
323                                                     RTE_ETH_FILTER_GENERIC,
324                                                     RTE_ETH_FILTER_GET,
325                                                     &ops) ||
326                           !ops))
327                 code = ENOSYS;
328         else
329                 return ops;
330         rte_flow_error_set(error, code, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
331                            NULL, rte_strerror(code));
332         return NULL;
333 }
334
335 /* Check whether a flow rule can be created on a given port. */
336 int
337 rte_flow_validate(uint16_t port_id,
338                   const struct rte_flow_attr *attr,
339                   const struct rte_flow_item pattern[],
340                   const struct rte_flow_action actions[],
341                   struct rte_flow_error *error)
342 {
343         const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
344         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
345
346         if (unlikely(!ops))
347                 return -rte_errno;
348         if (likely(!!ops->validate))
349                 return flow_err(port_id, ops->validate(dev, attr, pattern,
350                                                        actions, error), error);
351         return rte_flow_error_set(error, ENOSYS,
352                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
353                                   NULL, rte_strerror(ENOSYS));
354 }
355
356 /* Create a flow rule on a given port. */
357 struct rte_flow *
358 rte_flow_create(uint16_t port_id,
359                 const struct rte_flow_attr *attr,
360                 const struct rte_flow_item pattern[],
361                 const struct rte_flow_action actions[],
362                 struct rte_flow_error *error)
363 {
364         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
365         struct rte_flow *flow;
366         const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
367
368         if (unlikely(!ops))
369                 return NULL;
370         if (likely(!!ops->create)) {
371                 flow = ops->create(dev, attr, pattern, actions, error);
372                 if (flow == NULL)
373                         flow_err(port_id, -rte_errno, error);
374                 return flow;
375         }
376         rte_flow_error_set(error, ENOSYS, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
377                            NULL, rte_strerror(ENOSYS));
378         return NULL;
379 }
380
381 /* Destroy a flow rule on a given port. */
382 int
383 rte_flow_destroy(uint16_t port_id,
384                  struct rte_flow *flow,
385                  struct rte_flow_error *error)
386 {
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);
389
390         if (unlikely(!ops))
391                 return -rte_errno;
392         if (likely(!!ops->destroy))
393                 return flow_err(port_id, ops->destroy(dev, flow, error),
394                                 error);
395         return rte_flow_error_set(error, ENOSYS,
396                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
397                                   NULL, rte_strerror(ENOSYS));
398 }
399
400 /* Destroy all flow rules associated with a port. */
401 int
402 rte_flow_flush(uint16_t port_id,
403                struct rte_flow_error *error)
404 {
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);
407
408         if (unlikely(!ops))
409                 return -rte_errno;
410         if (likely(!!ops->flush))
411                 return flow_err(port_id, ops->flush(dev, error), error);
412         return rte_flow_error_set(error, ENOSYS,
413                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
414                                   NULL, rte_strerror(ENOSYS));
415 }
416
417 /* Query an existing flow rule. */
418 int
419 rte_flow_query(uint16_t port_id,
420                struct rte_flow *flow,
421                const struct rte_flow_action *action,
422                void *data,
423                struct rte_flow_error *error)
424 {
425         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
426         const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
427
428         if (!ops)
429                 return -rte_errno;
430         if (likely(!!ops->query))
431                 return flow_err(port_id, ops->query(dev, flow, action, data,
432                                                     error), error);
433         return rte_flow_error_set(error, ENOSYS,
434                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
435                                   NULL, rte_strerror(ENOSYS));
436 }
437
438 /* Restrict ingress traffic to the defined flow rules. */
439 int
440 rte_flow_isolate(uint16_t port_id,
441                  int set,
442                  struct rte_flow_error *error)
443 {
444         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
445         const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
446
447         if (!ops)
448                 return -rte_errno;
449         if (likely(!!ops->isolate))
450                 return flow_err(port_id, ops->isolate(dev, set, error), error);
451         return rte_flow_error_set(error, ENOSYS,
452                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
453                                   NULL, rte_strerror(ENOSYS));
454 }
455
456 /* Initialize flow error structure. */
457 int
458 rte_flow_error_set(struct rte_flow_error *error,
459                    int code,
460                    enum rte_flow_error_type type,
461                    const void *cause,
462                    const char *message)
463 {
464         if (error) {
465                 *error = (struct rte_flow_error){
466                         .type = type,
467                         .cause = cause,
468                         .message = message,
469                 };
470         }
471         rte_errno = code;
472         return -code;
473 }
474
475 /** Pattern item specification types. */
476 enum rte_flow_conv_item_spec_type {
477         RTE_FLOW_CONV_ITEM_SPEC,
478         RTE_FLOW_CONV_ITEM_LAST,
479         RTE_FLOW_CONV_ITEM_MASK,
480 };
481
482 /**
483  * Copy pattern item specification.
484  *
485  * @param[out] buf
486  *   Output buffer. Can be NULL if @p size is zero.
487  * @param size
488  *   Size of @p buf in bytes.
489  * @param[in] item
490  *   Pattern item to copy specification from.
491  * @param type
492  *   Specification selector for either @p spec, @p last or @p mask.
493  *
494  * @return
495  *   Number of bytes needed to store pattern item specification regardless
496  *   of @p size. @p buf contents are truncated to @p size if not large
497  *   enough.
498  */
499 static size_t
500 rte_flow_conv_item_spec(void *buf, const size_t size,
501                         const struct rte_flow_item *item,
502                         enum rte_flow_conv_item_spec_type type)
503 {
504         size_t off;
505         const void *data =
506                 type == RTE_FLOW_CONV_ITEM_SPEC ? item->spec :
507                 type == RTE_FLOW_CONV_ITEM_LAST ? item->last :
508                 type == RTE_FLOW_CONV_ITEM_MASK ? item->mask :
509                 NULL;
510
511         switch (item->type) {
512                 union {
513                         const struct rte_flow_item_raw *raw;
514                 } spec;
515                 union {
516                         const struct rte_flow_item_raw *raw;
517                 } last;
518                 union {
519                         const struct rte_flow_item_raw *raw;
520                 } mask;
521                 union {
522                         const struct rte_flow_item_raw *raw;
523                 } src;
524                 union {
525                         struct rte_flow_item_raw *raw;
526                 } dst;
527                 size_t tmp;
528
529         case RTE_FLOW_ITEM_TYPE_RAW:
530                 spec.raw = item->spec;
531                 last.raw = item->last ? item->last : item->spec;
532                 mask.raw = item->mask ? item->mask : &rte_flow_item_raw_mask;
533                 src.raw = data;
534                 dst.raw = buf;
535                 rte_memcpy(dst.raw,
536                            (&(struct rte_flow_item_raw){
537                                 .relative = src.raw->relative,
538                                 .search = src.raw->search,
539                                 .reserved = src.raw->reserved,
540                                 .offset = src.raw->offset,
541                                 .limit = src.raw->limit,
542                                 .length = src.raw->length,
543                            }),
544                            size > sizeof(*dst.raw) ? sizeof(*dst.raw) : size);
545                 off = sizeof(*dst.raw);
546                 if (type == RTE_FLOW_CONV_ITEM_SPEC ||
547                     (type == RTE_FLOW_CONV_ITEM_MASK &&
548                      ((spec.raw->length & mask.raw->length) >=
549                       (last.raw->length & mask.raw->length))))
550                         tmp = spec.raw->length & mask.raw->length;
551                 else
552                         tmp = last.raw->length & mask.raw->length;
553                 if (tmp) {
554                         off = RTE_ALIGN_CEIL(off, sizeof(*dst.raw->pattern));
555                         if (size >= off + tmp)
556                                 dst.raw->pattern = rte_memcpy
557                                         ((void *)((uintptr_t)dst.raw + off),
558                                          src.raw->pattern, tmp);
559                         off += tmp;
560                 }
561                 break;
562         default:
563                 off = rte_flow_desc_item[item->type].size;
564                 rte_memcpy(buf, data, (size > off ? off : size));
565                 break;
566         }
567         return off;
568 }
569
570 /**
571  * Copy action configuration.
572  *
573  * @param[out] buf
574  *   Output buffer. Can be NULL if @p size is zero.
575  * @param size
576  *   Size of @p buf in bytes.
577  * @param[in] action
578  *   Action to copy configuration from.
579  *
580  * @return
581  *   Number of bytes needed to store pattern item specification regardless
582  *   of @p size. @p buf contents are truncated to @p size if not large
583  *   enough.
584  */
585 static size_t
586 rte_flow_conv_action_conf(void *buf, const size_t size,
587                           const struct rte_flow_action *action)
588 {
589         size_t off;
590
591         switch (action->type) {
592                 union {
593                         const struct rte_flow_action_rss *rss;
594                         const struct rte_flow_action_vxlan_encap *vxlan_encap;
595                         const struct rte_flow_action_nvgre_encap *nvgre_encap;
596                 } src;
597                 union {
598                         struct rte_flow_action_rss *rss;
599                         struct rte_flow_action_vxlan_encap *vxlan_encap;
600                         struct rte_flow_action_nvgre_encap *nvgre_encap;
601                 } dst;
602                 size_t tmp;
603                 int ret;
604
605         case RTE_FLOW_ACTION_TYPE_RSS:
606                 src.rss = action->conf;
607                 dst.rss = buf;
608                 rte_memcpy(dst.rss,
609                            (&(struct rte_flow_action_rss){
610                                 .func = src.rss->func,
611                                 .level = src.rss->level,
612                                 .types = src.rss->types,
613                                 .key_len = src.rss->key_len,
614                                 .queue_num = src.rss->queue_num,
615                            }),
616                            size > sizeof(*dst.rss) ? sizeof(*dst.rss) : size);
617                 off = sizeof(*dst.rss);
618                 if (src.rss->key_len) {
619                         off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->key));
620                         tmp = sizeof(*src.rss->key) * src.rss->key_len;
621                         if (size >= off + tmp)
622                                 dst.rss->key = rte_memcpy
623                                         ((void *)((uintptr_t)dst.rss + off),
624                                          src.rss->key, tmp);
625                         off += tmp;
626                 }
627                 if (src.rss->queue_num) {
628                         off = RTE_ALIGN_CEIL(off, sizeof(*dst.rss->queue));
629                         tmp = sizeof(*src.rss->queue) * src.rss->queue_num;
630                         if (size >= off + tmp)
631                                 dst.rss->queue = rte_memcpy
632                                         ((void *)((uintptr_t)dst.rss + off),
633                                          src.rss->queue, tmp);
634                         off += tmp;
635                 }
636                 break;
637         case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
638         case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
639                 src.vxlan_encap = action->conf;
640                 dst.vxlan_encap = buf;
641                 RTE_BUILD_BUG_ON(sizeof(*src.vxlan_encap) !=
642                                  sizeof(*src.nvgre_encap) ||
643                                  offsetof(struct rte_flow_action_vxlan_encap,
644                                           definition) !=
645                                  offsetof(struct rte_flow_action_nvgre_encap,
646                                           definition));
647                 off = sizeof(*dst.vxlan_encap);
648                 if (src.vxlan_encap->definition) {
649                         off = RTE_ALIGN_CEIL
650                                 (off, sizeof(*dst.vxlan_encap->definition));
651                         ret = rte_flow_conv
652                                 (RTE_FLOW_CONV_OP_PATTERN,
653                                  (void *)((uintptr_t)dst.vxlan_encap + off),
654                                  size > off ? size - off : 0,
655                                  src.vxlan_encap->definition, NULL);
656                         if (ret < 0)
657                                 return 0;
658                         if (size >= off + ret)
659                                 dst.vxlan_encap->definition =
660                                         (void *)((uintptr_t)dst.vxlan_encap +
661                                                  off);
662                         off += ret;
663                 }
664                 break;
665         default:
666                 off = rte_flow_desc_action[action->type].size;
667                 rte_memcpy(buf, action->conf, (size > off ? off : size));
668                 break;
669         }
670         return off;
671 }
672
673 /**
674  * Copy a list of pattern items.
675  *
676  * @param[out] dst
677  *   Destination buffer. Can be NULL if @p size is zero.
678  * @param size
679  *   Size of @p dst in bytes.
680  * @param[in] src
681  *   Source pattern items.
682  * @param num
683  *   Maximum number of pattern items to process from @p src or 0 to process
684  *   the entire list. In both cases, processing stops after
685  *   RTE_FLOW_ITEM_TYPE_END is encountered.
686  * @param[out] error
687  *   Perform verbose error reporting if not NULL.
688  *
689  * @return
690  *   A positive value representing the number of bytes needed to store
691  *   pattern items regardless of @p size on success (@p buf contents are
692  *   truncated to @p size if not large enough), a negative errno value
693  *   otherwise and rte_errno is set.
694  */
695 static int
696 rte_flow_conv_pattern(struct rte_flow_item *dst,
697                       const size_t size,
698                       const struct rte_flow_item *src,
699                       unsigned int num,
700                       struct rte_flow_error *error)
701 {
702         uintptr_t data = (uintptr_t)dst;
703         size_t off;
704         size_t ret;
705         unsigned int i;
706
707         for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
708                 if ((size_t)src->type >= RTE_DIM(rte_flow_desc_item) ||
709                     !rte_flow_desc_item[src->type].name)
710                         return rte_flow_error_set
711                                 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, src,
712                                  "cannot convert unknown item type");
713                 if (size >= off + sizeof(*dst))
714                         *dst = (struct rte_flow_item){
715                                 .type = src->type,
716                         };
717                 off += sizeof(*dst);
718                 if (!src->type)
719                         num = i + 1;
720         }
721         num = i;
722         src -= num;
723         dst -= num;
724         do {
725                 if (src->spec) {
726                         off = RTE_ALIGN_CEIL(off, sizeof(double));
727                         ret = rte_flow_conv_item_spec
728                                 ((void *)(data + off),
729                                  size > off ? size - off : 0, src,
730                                  RTE_FLOW_CONV_ITEM_SPEC);
731                         if (size && size >= off + ret)
732                                 dst->spec = (void *)(data + off);
733                         off += ret;
734
735                 }
736                 if (src->last) {
737                         off = RTE_ALIGN_CEIL(off, sizeof(double));
738                         ret = rte_flow_conv_item_spec
739                                 ((void *)(data + off),
740                                  size > off ? size - off : 0, src,
741                                  RTE_FLOW_CONV_ITEM_LAST);
742                         if (size && size >= off + ret)
743                                 dst->last = (void *)(data + off);
744                         off += ret;
745                 }
746                 if (src->mask) {
747                         off = RTE_ALIGN_CEIL(off, sizeof(double));
748                         ret = rte_flow_conv_item_spec
749                                 ((void *)(data + off),
750                                  size > off ? size - off : 0, src,
751                                  RTE_FLOW_CONV_ITEM_MASK);
752                         if (size && size >= off + ret)
753                                 dst->mask = (void *)(data + off);
754                         off += ret;
755                 }
756                 ++src;
757                 ++dst;
758         } while (--num);
759         return off;
760 }
761
762 /**
763  * Copy a list of actions.
764  *
765  * @param[out] dst
766  *   Destination buffer. Can be NULL if @p size is zero.
767  * @param size
768  *   Size of @p dst in bytes.
769  * @param[in] src
770  *   Source actions.
771  * @param num
772  *   Maximum number of actions to process from @p src or 0 to process the
773  *   entire list. In both cases, processing stops after
774  *   RTE_FLOW_ACTION_TYPE_END is encountered.
775  * @param[out] error
776  *   Perform verbose error reporting if not NULL.
777  *
778  * @return
779  *   A positive value representing the number of bytes needed to store
780  *   actions regardless of @p size on success (@p buf contents are truncated
781  *   to @p size if not large enough), a negative errno value otherwise and
782  *   rte_errno is set.
783  */
784 static int
785 rte_flow_conv_actions(struct rte_flow_action *dst,
786                       const size_t size,
787                       const struct rte_flow_action *src,
788                       unsigned int num,
789                       struct rte_flow_error *error)
790 {
791         uintptr_t data = (uintptr_t)dst;
792         size_t off;
793         size_t ret;
794         unsigned int i;
795
796         for (i = 0, off = 0; !num || i != num; ++i, ++src, ++dst) {
797                 if ((size_t)src->type >= RTE_DIM(rte_flow_desc_action) ||
798                     !rte_flow_desc_action[src->type].name)
799                         return rte_flow_error_set
800                                 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
801                                  src, "cannot convert unknown action type");
802                 if (size >= off + sizeof(*dst))
803                         *dst = (struct rte_flow_action){
804                                 .type = src->type,
805                         };
806                 off += sizeof(*dst);
807                 if (!src->type)
808                         num = i + 1;
809         }
810         num = i;
811         src -= num;
812         dst -= num;
813         do {
814                 if (src->conf) {
815                         off = RTE_ALIGN_CEIL(off, sizeof(double));
816                         ret = rte_flow_conv_action_conf
817                                 ((void *)(data + off),
818                                  size > off ? size - off : 0, src);
819                         if (size && size >= off + ret)
820                                 dst->conf = (void *)(data + off);
821                         off += ret;
822                 }
823                 ++src;
824                 ++dst;
825         } while (--num);
826         return off;
827 }
828
829 /**
830  * Copy flow rule components.
831  *
832  * This comprises the flow rule descriptor itself, attributes, pattern and
833  * actions list. NULL components in @p src are skipped.
834  *
835  * @param[out] dst
836  *   Destination buffer. Can be NULL if @p size is zero.
837  * @param size
838  *   Size of @p dst in bytes.
839  * @param[in] src
840  *   Source flow rule descriptor.
841  * @param[out] error
842  *   Perform verbose error reporting if not NULL.
843  *
844  * @return
845  *   A positive value representing the number of bytes needed to store all
846  *   components including the descriptor regardless of @p size on success
847  *   (@p buf contents are truncated to @p size if not large enough), a
848  *   negative errno value otherwise and rte_errno is set.
849  */
850 static int
851 rte_flow_conv_rule(struct rte_flow_conv_rule *dst,
852                    const size_t size,
853                    const struct rte_flow_conv_rule *src,
854                    struct rte_flow_error *error)
855 {
856         size_t off;
857         int ret;
858
859         rte_memcpy(dst,
860                    (&(struct rte_flow_conv_rule){
861                         .attr = NULL,
862                         .pattern = NULL,
863                         .actions = NULL,
864                    }),
865                    size > sizeof(*dst) ? sizeof(*dst) : size);
866         off = sizeof(*dst);
867         if (src->attr_ro) {
868                 off = RTE_ALIGN_CEIL(off, sizeof(double));
869                 if (size && size >= off + sizeof(*dst->attr))
870                         dst->attr = rte_memcpy
871                                 ((void *)((uintptr_t)dst + off),
872                                  src->attr_ro, sizeof(*dst->attr));
873                 off += sizeof(*dst->attr);
874         }
875         if (src->pattern_ro) {
876                 off = RTE_ALIGN_CEIL(off, sizeof(double));
877                 ret = rte_flow_conv_pattern((void *)((uintptr_t)dst + off),
878                                             size > off ? size - off : 0,
879                                             src->pattern_ro, 0, error);
880                 if (ret < 0)
881                         return ret;
882                 if (size && size >= off + (size_t)ret)
883                         dst->pattern = (void *)((uintptr_t)dst + off);
884                 off += ret;
885         }
886         if (src->actions_ro) {
887                 off = RTE_ALIGN_CEIL(off, sizeof(double));
888                 ret = rte_flow_conv_actions((void *)((uintptr_t)dst + off),
889                                             size > off ? size - off : 0,
890                                             src->actions_ro, 0, error);
891                 if (ret < 0)
892                         return ret;
893                 if (size >= off + (size_t)ret)
894                         dst->actions = (void *)((uintptr_t)dst + off);
895                 off += ret;
896         }
897         return off;
898 }
899
900 /**
901  * Retrieve the name of a pattern item/action type.
902  *
903  * @param is_action
904  *   Nonzero when @p src represents an action type instead of a pattern item
905  *   type.
906  * @param is_ptr
907  *   Nonzero to write string address instead of contents into @p dst.
908  * @param[out] dst
909  *   Destination buffer. Can be NULL if @p size is zero.
910  * @param size
911  *   Size of @p dst in bytes.
912  * @param[in] src
913  *   Depending on @p is_action, source pattern item or action type cast as a
914  *   pointer.
915  * @param[out] error
916  *   Perform verbose error reporting if not NULL.
917  *
918  * @return
919  *   A positive value representing the number of bytes needed to store the
920  *   name or its address regardless of @p size on success (@p buf contents
921  *   are truncated to @p size if not large enough), a negative errno value
922  *   otherwise and rte_errno is set.
923  */
924 static int
925 rte_flow_conv_name(int is_action,
926                    int is_ptr,
927                    char *dst,
928                    const size_t size,
929                    const void *src,
930                    struct rte_flow_error *error)
931 {
932         struct desc_info {
933                 const struct rte_flow_desc_data *data;
934                 size_t num;
935         };
936         static const struct desc_info info_rep[2] = {
937                 { rte_flow_desc_item, RTE_DIM(rte_flow_desc_item), },
938                 { rte_flow_desc_action, RTE_DIM(rte_flow_desc_action), },
939         };
940         const struct desc_info *const info = &info_rep[!!is_action];
941         unsigned int type = (uintptr_t)src;
942
943         if (type >= info->num)
944                 return rte_flow_error_set
945                         (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
946                          "unknown object type to retrieve the name of");
947         if (!is_ptr)
948                 return strlcpy(dst, info->data[type].name, size);
949         if (size >= sizeof(const char **))
950                 *((const char **)dst) = info->data[type].name;
951         return sizeof(const char **);
952 }
953
954 /** Helper function to convert flow API objects. */
955 int
956 rte_flow_conv(enum rte_flow_conv_op op,
957               void *dst,
958               size_t size,
959               const void *src,
960               struct rte_flow_error *error)
961 {
962         switch (op) {
963                 const struct rte_flow_attr *attr;
964
965         case RTE_FLOW_CONV_OP_NONE:
966                 return 0;
967         case RTE_FLOW_CONV_OP_ATTR:
968                 attr = src;
969                 if (size > sizeof(*attr))
970                         size = sizeof(*attr);
971                 rte_memcpy(dst, attr, size);
972                 return sizeof(*attr);
973         case RTE_FLOW_CONV_OP_ITEM:
974                 return rte_flow_conv_pattern(dst, size, src, 1, error);
975         case RTE_FLOW_CONV_OP_ACTION:
976                 return rte_flow_conv_actions(dst, size, src, 1, error);
977         case RTE_FLOW_CONV_OP_PATTERN:
978                 return rte_flow_conv_pattern(dst, size, src, 0, error);
979         case RTE_FLOW_CONV_OP_ACTIONS:
980                 return rte_flow_conv_actions(dst, size, src, 0, error);
981         case RTE_FLOW_CONV_OP_RULE:
982                 return rte_flow_conv_rule(dst, size, src, error);
983         case RTE_FLOW_CONV_OP_ITEM_NAME:
984                 return rte_flow_conv_name(0, 0, dst, size, src, error);
985         case RTE_FLOW_CONV_OP_ACTION_NAME:
986                 return rte_flow_conv_name(1, 0, dst, size, src, error);
987         case RTE_FLOW_CONV_OP_ITEM_NAME_PTR:
988                 return rte_flow_conv_name(0, 1, dst, size, src, error);
989         case RTE_FLOW_CONV_OP_ACTION_NAME_PTR:
990                 return rte_flow_conv_name(1, 1, dst, size, src, error);
991         }
992         return rte_flow_error_set
993                 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
994                  "unknown object conversion operation");
995 }
996
997 /** Store a full rte_flow description. */
998 size_t
999 rte_flow_copy(struct rte_flow_desc *desc, size_t len,
1000               const struct rte_flow_attr *attr,
1001               const struct rte_flow_item *items,
1002               const struct rte_flow_action *actions)
1003 {
1004         /*
1005          * Overlap struct rte_flow_conv with struct rte_flow_desc in order
1006          * to convert the former to the latter without wasting space.
1007          */
1008         struct rte_flow_conv_rule *dst =
1009                 len ?
1010                 (void *)((uintptr_t)desc +
1011                          (offsetof(struct rte_flow_desc, actions) -
1012                           offsetof(struct rte_flow_conv_rule, actions))) :
1013                 NULL;
1014         size_t dst_size =
1015                 len > sizeof(*desc) - sizeof(*dst) ?
1016                 len - (sizeof(*desc) - sizeof(*dst)) :
1017                 0;
1018         struct rte_flow_conv_rule src = {
1019                 .attr_ro = NULL,
1020                 .pattern_ro = items,
1021                 .actions_ro = actions,
1022         };
1023         int ret;
1024
1025         RTE_BUILD_BUG_ON(sizeof(struct rte_flow_desc) <
1026                          sizeof(struct rte_flow_conv_rule));
1027         if (dst_size &&
1028             (&dst->pattern != &desc->items ||
1029              &dst->actions != &desc->actions ||
1030              (uintptr_t)(dst + 1) != (uintptr_t)(desc + 1))) {
1031                 rte_errno = EINVAL;
1032                 return 0;
1033         }
1034         ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, dst, dst_size, &src, NULL);
1035         if (ret < 0)
1036                 return 0;
1037         ret += sizeof(*desc) - sizeof(*dst);
1038         rte_memcpy(desc,
1039                    (&(struct rte_flow_desc){
1040                         .size = ret,
1041                         .attr = *attr,
1042                         .items = dst_size ? dst->pattern : NULL,
1043                         .actions = dst_size ? dst->actions : NULL,
1044                    }),
1045                    len > sizeof(*desc) ? sizeof(*desc) : len);
1046         return ret;
1047 }
1048
1049 /**
1050  * Expand RSS flows into several possible flows according to the RSS hash
1051  * fields requested and the driver capabilities.
1052  */
1053 int
1054 rte_flow_expand_rss(struct rte_flow_expand_rss *buf, size_t size,
1055                     const struct rte_flow_item *pattern, uint64_t types,
1056                     const struct rte_flow_expand_node graph[],
1057                     int graph_root_index)
1058 {
1059         const int elt_n = 8;
1060         const struct rte_flow_item *item;
1061         const struct rte_flow_expand_node *node = &graph[graph_root_index];
1062         const int *next_node;
1063         const int *stack[elt_n];
1064         int stack_pos = 0;
1065         struct rte_flow_item flow_items[elt_n];
1066         unsigned int i;
1067         size_t lsize;
1068         size_t user_pattern_size = 0;
1069         void *addr = NULL;
1070         const struct rte_flow_expand_node *next = NULL;
1071         struct rte_flow_item missed_item;
1072         int missed = 0;
1073         int elt = 0;
1074         const struct rte_flow_item *last_item = NULL;
1075
1076         memset(&missed_item, 0, sizeof(missed_item));
1077         lsize = offsetof(struct rte_flow_expand_rss, entry) +
1078                 elt_n * sizeof(buf->entry[0]);
1079         if (lsize <= size) {
1080                 buf->entry[0].priority = 0;
1081                 buf->entry[0].pattern = (void *)&buf->entry[elt_n];
1082                 buf->entries = 0;
1083                 addr = buf->entry[0].pattern;
1084         }
1085         for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
1086                 if (item->type != RTE_FLOW_ITEM_TYPE_VOID)
1087                         last_item = item;
1088                 for (i = 0; node->next && node->next[i]; ++i) {
1089                         next = &graph[node->next[i]];
1090                         if (next->type == item->type)
1091                                 break;
1092                 }
1093                 if (next)
1094                         node = next;
1095                 user_pattern_size += sizeof(*item);
1096         }
1097         user_pattern_size += sizeof(*item); /* Handle END item. */
1098         lsize += user_pattern_size;
1099         /* Copy the user pattern in the first entry of the buffer. */
1100         if (lsize <= size) {
1101                 rte_memcpy(addr, pattern, user_pattern_size);
1102                 addr = (void *)(((uintptr_t)addr) + user_pattern_size);
1103                 buf->entries = 1;
1104         }
1105         /* Start expanding. */
1106         memset(flow_items, 0, sizeof(flow_items));
1107         user_pattern_size -= sizeof(*item);
1108         /*
1109          * Check if the last valid item has spec set
1110          * and need complete pattern.
1111          */
1112         missed_item.type = rte_flow_expand_rss_item_complete(last_item);
1113         if (missed_item.type != RTE_FLOW_ITEM_TYPE_VOID) {
1114                 next = NULL;
1115                 missed = 1;
1116                 for (i = 0; node->next && node->next[i]; ++i) {
1117                         next = &graph[node->next[i]];
1118                         if (next->type == missed_item.type) {
1119                                 flow_items[0].type = missed_item.type;
1120                                 flow_items[1].type = RTE_FLOW_ITEM_TYPE_END;
1121                                 break;
1122                         }
1123                         next = NULL;
1124                 }
1125         }
1126         if (next && missed) {
1127                 elt = 2; /* missed item + item end. */
1128                 node = next;
1129                 lsize += elt * sizeof(*item) + user_pattern_size;
1130                 if ((node->rss_types & types) && lsize <= size) {
1131                         buf->entry[buf->entries].priority = 1;
1132                         buf->entry[buf->entries].pattern = addr;
1133                         buf->entries++;
1134                         rte_memcpy(addr, buf->entry[0].pattern,
1135                                    user_pattern_size);
1136                         addr = (void *)(((uintptr_t)addr) + user_pattern_size);
1137                         rte_memcpy(addr, flow_items, elt * sizeof(*item));
1138                         addr = (void *)(((uintptr_t)addr) +
1139                                         elt * sizeof(*item));
1140                 }
1141         }
1142         memset(flow_items, 0, sizeof(flow_items));
1143         next_node = node->next;
1144         stack[stack_pos] = next_node;
1145         node = next_node ? &graph[*next_node] : NULL;
1146         while (node) {
1147                 flow_items[stack_pos].type = node->type;
1148                 if (node->rss_types & types) {
1149                         /*
1150                          * compute the number of items to copy from the
1151                          * expansion and copy it.
1152                          * When the stack_pos is 0, there are 1 element in it,
1153                          * plus the addition END item.
1154                          */
1155                         elt = stack_pos + 2;
1156                         flow_items[stack_pos + 1].type = RTE_FLOW_ITEM_TYPE_END;
1157                         lsize += elt * sizeof(*item) + user_pattern_size;
1158                         if (lsize <= size) {
1159                                 size_t n = elt * sizeof(*item);
1160
1161                                 buf->entry[buf->entries].priority =
1162                                         stack_pos + 1 + missed;
1163                                 buf->entry[buf->entries].pattern = addr;
1164                                 buf->entries++;
1165                                 rte_memcpy(addr, buf->entry[0].pattern,
1166                                            user_pattern_size);
1167                                 addr = (void *)(((uintptr_t)addr) +
1168                                                 user_pattern_size);
1169                                 rte_memcpy(addr, &missed_item,
1170                                            missed * sizeof(*item));
1171                                 addr = (void *)(((uintptr_t)addr) +
1172                                         missed * sizeof(*item));
1173                                 rte_memcpy(addr, flow_items, n);
1174                                 addr = (void *)(((uintptr_t)addr) + n);
1175                         }
1176                 }
1177                 /* Go deeper. */
1178                 if (node->next) {
1179                         next_node = node->next;
1180                         if (stack_pos++ == elt_n) {
1181                                 rte_errno = E2BIG;
1182                                 return -rte_errno;
1183                         }
1184                         stack[stack_pos] = next_node;
1185                 } else if (*(next_node + 1)) {
1186                         /* Follow up with the next possibility. */
1187                         ++next_node;
1188                 } else {
1189                         /* Move to the next path. */
1190                         if (stack_pos)
1191                                 next_node = stack[--stack_pos];
1192                         next_node++;
1193                         stack[stack_pos] = next_node;
1194                 }
1195                 node = *next_node ? &graph[*next_node] : NULL;
1196         };
1197         /* no expanded flows but we have missed item, create one rule for it */
1198         if (buf->entries == 1 && missed != 0) {
1199                 elt = 2;
1200                 lsize += elt * sizeof(*item) + user_pattern_size;
1201                 if (lsize <= size) {
1202                         buf->entry[buf->entries].priority = 1;
1203                         buf->entry[buf->entries].pattern = addr;
1204                         buf->entries++;
1205                         flow_items[0].type = missed_item.type;
1206                         flow_items[1].type = RTE_FLOW_ITEM_TYPE_END;
1207                         rte_memcpy(addr, buf->entry[0].pattern,
1208                                    user_pattern_size);
1209                         addr = (void *)(((uintptr_t)addr) + user_pattern_size);
1210                         rte_memcpy(addr, flow_items, elt * sizeof(*item));
1211                         addr = (void *)(((uintptr_t)addr) +
1212                                         elt * sizeof(*item));
1213                 }
1214         }
1215         return lsize;
1216 }