885a7ff9a4f02ad2925c7690b2b64da850cb2d6c
[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         MK_FLOW_ITEM(L2TPV3OIP, sizeof(struct rte_flow_item_l2tpv3oip)),
97 };
98
99 /** Generate flow_action[] entry. */
100 #define MK_FLOW_ACTION(t, s) \
101         [RTE_FLOW_ACTION_TYPE_ ## t] = { \
102                 .name = # t, \
103                 .size = s, \
104         }
105
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)),
174 };
175
176 int
177 rte_flow_dynf_metadata_register(void)
178 {
179         int offset;
180         int flag;
181
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),
186         };
187         static const struct rte_mbuf_dynflag desc_flag = {
188                 .name = RTE_MBUF_DYNFLAG_METADATA_NAME,
189         };
190
191         offset = rte_mbuf_dynfield_register(&desc_offs);
192         if (offset < 0)
193                 goto error;
194         flag = rte_mbuf_dynflag_register(&desc_flag);
195         if (flag < 0)
196                 goto error;
197         rte_flow_dynf_metadata_offs = offset;
198         rte_flow_dynf_metadata_mask = (1ULL << flag);
199         return 0;
200
201 error:
202         rte_flow_dynf_metadata_offs = -1;
203         rte_flow_dynf_metadata_mask = 0ULL;
204         return -rte_errno;
205 }
206
207 static int
208 flow_err(uint16_t port_id, int ret, struct rte_flow_error *error)
209 {
210         if (ret == 0)
211                 return 0;
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));
216         return ret;
217 }
218
219 static enum rte_flow_item_type
220 rte_flow_expand_rss_item_complete(const struct rte_flow_item *item)
221 {
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;
227
228         if (item == NULL || item->spec == NULL)
229                 return ret;
230         switch (item->type) {
231         case RTE_FLOW_ITEM_TYPE_ETH:
232                 if (item->mask)
233                         ether_type_m = ((const struct rte_flow_item_eth *)
234                                                 (item->mask))->type;
235                 else
236                         ether_type_m = rte_flow_item_eth_mask.type;
237                 if (ether_type_m != RTE_BE16(0xFFFF))
238                         break;
239                 ether_type = ((const struct rte_flow_item_eth *)
240                                 (item->spec))->type;
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;
247                 break;
248         case RTE_FLOW_ITEM_TYPE_VLAN:
249                 if (item->mask)
250                         ether_type_m = ((const struct rte_flow_item_vlan *)
251                                                 (item->mask))->inner_type;
252                 else
253                         ether_type_m = rte_flow_item_vlan_mask.inner_type;
254                 if (ether_type_m != RTE_BE16(0xFFFF))
255                         break;
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;
264                 break;
265         case RTE_FLOW_ITEM_TYPE_IPV4:
266                 if (item->mask)
267                         ip_next_proto_m = ((const struct rte_flow_item_ipv4 *)
268                                         (item->mask))->hdr.next_proto_id;
269                 else
270                         ip_next_proto_m =
271                                 rte_flow_item_ipv4_mask.hdr.next_proto_id;
272                 if (ip_next_proto_m != 0xFF)
273                         break;
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;
284                 break;
285         case RTE_FLOW_ITEM_TYPE_IPV6:
286                 if (item->mask)
287                         ip_next_proto_m = ((const struct rte_flow_item_ipv6 *)
288                                                 (item->mask))->hdr.proto;
289                 else
290                         ip_next_proto_m =
291                                 rte_flow_item_ipv6_mask.hdr.proto;
292                 if (ip_next_proto_m != 0xFF)
293                         break;
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;
304                 break;
305         default:
306                 ret = RTE_FLOW_ITEM_TYPE_VOID;
307                 break;
308         }
309         return ret;
310 }
311
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)
315 {
316         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
317         const struct rte_flow_ops *ops;
318         int code;
319
320         if (unlikely(!rte_eth_dev_is_valid_port(port_id)))
321                 code = ENODEV;
322         else if (unlikely(!dev->dev_ops->filter_ctrl ||
323                           dev->dev_ops->filter_ctrl(dev,
324                                                     RTE_ETH_FILTER_GENERIC,
325                                                     RTE_ETH_FILTER_GET,
326                                                     &ops) ||
327                           !ops))
328                 code = ENOSYS;
329         else
330                 return ops;
331         rte_flow_error_set(error, code, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
332                            NULL, rte_strerror(code));
333         return NULL;
334 }
335
336 /* Check whether a flow rule can be created on a given port. */
337 int
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)
343 {
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];
346
347         if (unlikely(!ops))
348                 return -rte_errno;
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));
355 }
356
357 /* Create a flow rule on a given port. */
358 struct rte_flow *
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)
364 {
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);
368
369         if (unlikely(!ops))
370                 return NULL;
371         if (likely(!!ops->create)) {
372                 flow = ops->create(dev, attr, pattern, actions, error);
373                 if (flow == NULL)
374                         flow_err(port_id, -rte_errno, error);
375                 return flow;
376         }
377         rte_flow_error_set(error, ENOSYS, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
378                            NULL, rte_strerror(ENOSYS));
379         return NULL;
380 }
381
382 /* Destroy a flow rule on a given port. */
383 int
384 rte_flow_destroy(uint16_t port_id,
385                  struct rte_flow *flow,
386                  struct rte_flow_error *error)
387 {
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);
390
391         if (unlikely(!ops))
392                 return -rte_errno;
393         if (likely(!!ops->destroy))
394                 return flow_err(port_id, ops->destroy(dev, flow, error),
395                                 error);
396         return rte_flow_error_set(error, ENOSYS,
397                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
398                                   NULL, rte_strerror(ENOSYS));
399 }
400
401 /* Destroy all flow rules associated with a port. */
402 int
403 rte_flow_flush(uint16_t port_id,
404                struct rte_flow_error *error)
405 {
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);
408
409         if (unlikely(!ops))
410                 return -rte_errno;
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));
416 }
417
418 /* Query an existing flow rule. */
419 int
420 rte_flow_query(uint16_t port_id,
421                struct rte_flow *flow,
422                const struct rte_flow_action *action,
423                void *data,
424                struct rte_flow_error *error)
425 {
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);
428
429         if (!ops)
430                 return -rte_errno;
431         if (likely(!!ops->query))
432                 return flow_err(port_id, ops->query(dev, flow, action, data,
433                                                     error), error);
434         return rte_flow_error_set(error, ENOSYS,
435                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
436                                   NULL, rte_strerror(ENOSYS));
437 }
438
439 /* Restrict ingress traffic to the defined flow rules. */
440 int
441 rte_flow_isolate(uint16_t port_id,
442                  int set,
443                  struct rte_flow_error *error)
444 {
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);
447
448         if (!ops)
449                 return -rte_errno;
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));
455 }
456
457 /* Initialize flow error structure. */
458 int
459 rte_flow_error_set(struct rte_flow_error *error,
460                    int code,
461                    enum rte_flow_error_type type,
462                    const void *cause,
463                    const char *message)
464 {
465         if (error) {
466                 *error = (struct rte_flow_error){
467                         .type = type,
468                         .cause = cause,
469                         .message = message,
470                 };
471         }
472         rte_errno = code;
473         return -code;
474 }
475
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,
481 };
482
483 /**
484  * Copy pattern item specification.
485  *
486  * @param[out] buf
487  *   Output buffer. Can be NULL if @p size is zero.
488  * @param size
489  *   Size of @p buf in bytes.
490  * @param[in] item
491  *   Pattern item to copy specification from.
492  * @param type
493  *   Specification selector for either @p spec, @p last or @p mask.
494  *
495  * @return
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
498  *   enough.
499  */
500 static size_t
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)
504 {
505         size_t off;
506         const void *data =
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 :
510                 NULL;
511
512         switch (item->type) {
513                 union {
514                         const struct rte_flow_item_raw *raw;
515                 } spec;
516                 union {
517                         const struct rte_flow_item_raw *raw;
518                 } last;
519                 union {
520                         const struct rte_flow_item_raw *raw;
521                 } mask;
522                 union {
523                         const struct rte_flow_item_raw *raw;
524                 } src;
525                 union {
526                         struct rte_flow_item_raw *raw;
527                 } dst;
528                 size_t tmp;
529
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;
534                 src.raw = data;
535                 dst.raw = buf;
536                 rte_memcpy(dst.raw,
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,
544                            }),
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;
552                 else
553                         tmp = last.raw->length & mask.raw->length;
554                 if (tmp) {
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);
560                         off += tmp;
561                 }
562                 break;
563         default:
564                 off = rte_flow_desc_item[item->type].size;
565                 rte_memcpy(buf, data, (size > off ? off : size));
566                 break;
567         }
568         return off;
569 }
570
571 /**
572  * Copy action configuration.
573  *
574  * @param[out] buf
575  *   Output buffer. Can be NULL if @p size is zero.
576  * @param size
577  *   Size of @p buf in bytes.
578  * @param[in] action
579  *   Action to copy configuration from.
580  *
581  * @return
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
584  *   enough.
585  */
586 static size_t
587 rte_flow_conv_action_conf(void *buf, const size_t size,
588                           const struct rte_flow_action *action)
589 {
590         size_t off;
591
592         switch (action->type) {
593                 union {
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;
597                 } src;
598                 union {
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;
602                 } dst;
603                 size_t tmp;
604                 int ret;
605
606         case RTE_FLOW_ACTION_TYPE_RSS:
607                 src.rss = action->conf;
608                 dst.rss = buf;
609                 rte_memcpy(dst.rss,
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,
616                            }),
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),
625                                          src.rss->key, tmp);
626                         off += tmp;
627                 }
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);
635                         off += tmp;
636                 }
637                 break;
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,
645                                           definition) !=
646                                  offsetof(struct rte_flow_action_nvgre_encap,
647                                           definition));
648                 off = sizeof(*dst.vxlan_encap);
649                 if (src.vxlan_encap->definition) {
650                         off = RTE_ALIGN_CEIL
651                                 (off, sizeof(*dst.vxlan_encap->definition));
652                         ret = rte_flow_conv
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);
657                         if (ret < 0)
658                                 return 0;
659                         if (size >= off + ret)
660                                 dst.vxlan_encap->definition =
661                                         (void *)((uintptr_t)dst.vxlan_encap +
662                                                  off);
663                         off += ret;
664                 }
665                 break;
666         default:
667                 off = rte_flow_desc_action[action->type].size;
668                 rte_memcpy(buf, action->conf, (size > off ? off : size));
669                 break;
670         }
671         return off;
672 }
673
674 /**
675  * Copy a list of pattern items.
676  *
677  * @param[out] dst
678  *   Destination buffer. Can be NULL if @p size is zero.
679  * @param size
680  *   Size of @p dst in bytes.
681  * @param[in] src
682  *   Source pattern items.
683  * @param num
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.
687  * @param[out] error
688  *   Perform verbose error reporting if not NULL.
689  *
690  * @return
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.
695  */
696 static int
697 rte_flow_conv_pattern(struct rte_flow_item *dst,
698                       const size_t size,
699                       const struct rte_flow_item *src,
700                       unsigned int num,
701                       struct rte_flow_error *error)
702 {
703         uintptr_t data = (uintptr_t)dst;
704         size_t off;
705         size_t ret;
706         unsigned int i;
707
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){
716                                 .type = src->type,
717                         };
718                 off += sizeof(*dst);
719                 if (!src->type)
720                         num = i + 1;
721         }
722         num = i;
723         src -= num;
724         dst -= num;
725         do {
726                 if (src->spec) {
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);
734                         off += ret;
735
736                 }
737                 if (src->last) {
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);
745                         off += ret;
746                 }
747                 if (src->mask) {
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);
755                         off += ret;
756                 }
757                 ++src;
758                 ++dst;
759         } while (--num);
760         return off;
761 }
762
763 /**
764  * Copy a list of actions.
765  *
766  * @param[out] dst
767  *   Destination buffer. Can be NULL if @p size is zero.
768  * @param size
769  *   Size of @p dst in bytes.
770  * @param[in] src
771  *   Source actions.
772  * @param num
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.
776  * @param[out] error
777  *   Perform verbose error reporting if not NULL.
778  *
779  * @return
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
783  *   rte_errno is set.
784  */
785 static int
786 rte_flow_conv_actions(struct rte_flow_action *dst,
787                       const size_t size,
788                       const struct rte_flow_action *src,
789                       unsigned int num,
790                       struct rte_flow_error *error)
791 {
792         uintptr_t data = (uintptr_t)dst;
793         size_t off;
794         size_t ret;
795         unsigned int i;
796
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){
805                                 .type = src->type,
806                         };
807                 off += sizeof(*dst);
808                 if (!src->type)
809                         num = i + 1;
810         }
811         num = i;
812         src -= num;
813         dst -= num;
814         do {
815                 if (src->conf) {
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);
822                         off += ret;
823                 }
824                 ++src;
825                 ++dst;
826         } while (--num);
827         return off;
828 }
829
830 /**
831  * Copy flow rule components.
832  *
833  * This comprises the flow rule descriptor itself, attributes, pattern and
834  * actions list. NULL components in @p src are skipped.
835  *
836  * @param[out] dst
837  *   Destination buffer. Can be NULL if @p size is zero.
838  * @param size
839  *   Size of @p dst in bytes.
840  * @param[in] src
841  *   Source flow rule descriptor.
842  * @param[out] error
843  *   Perform verbose error reporting if not NULL.
844  *
845  * @return
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.
850  */
851 static int
852 rte_flow_conv_rule(struct rte_flow_conv_rule *dst,
853                    const size_t size,
854                    const struct rte_flow_conv_rule *src,
855                    struct rte_flow_error *error)
856 {
857         size_t off;
858         int ret;
859
860         rte_memcpy(dst,
861                    (&(struct rte_flow_conv_rule){
862                         .attr = NULL,
863                         .pattern = NULL,
864                         .actions = NULL,
865                    }),
866                    size > sizeof(*dst) ? sizeof(*dst) : size);
867         off = sizeof(*dst);
868         if (src->attr_ro) {
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);
875         }
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);
881                 if (ret < 0)
882                         return ret;
883                 if (size && size >= off + (size_t)ret)
884                         dst->pattern = (void *)((uintptr_t)dst + off);
885                 off += ret;
886         }
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);
892                 if (ret < 0)
893                         return ret;
894                 if (size >= off + (size_t)ret)
895                         dst->actions = (void *)((uintptr_t)dst + off);
896                 off += ret;
897         }
898         return off;
899 }
900
901 /**
902  * Retrieve the name of a pattern item/action type.
903  *
904  * @param is_action
905  *   Nonzero when @p src represents an action type instead of a pattern item
906  *   type.
907  * @param is_ptr
908  *   Nonzero to write string address instead of contents into @p dst.
909  * @param[out] dst
910  *   Destination buffer. Can be NULL if @p size is zero.
911  * @param size
912  *   Size of @p dst in bytes.
913  * @param[in] src
914  *   Depending on @p is_action, source pattern item or action type cast as a
915  *   pointer.
916  * @param[out] error
917  *   Perform verbose error reporting if not NULL.
918  *
919  * @return
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.
924  */
925 static int
926 rte_flow_conv_name(int is_action,
927                    int is_ptr,
928                    char *dst,
929                    const size_t size,
930                    const void *src,
931                    struct rte_flow_error *error)
932 {
933         struct desc_info {
934                 const struct rte_flow_desc_data *data;
935                 size_t num;
936         };
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), },
940         };
941         const struct desc_info *const info = &info_rep[!!is_action];
942         unsigned int type = (uintptr_t)src;
943
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");
948         if (!is_ptr)
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 **);
953 }
954
955 /** Helper function to convert flow API objects. */
956 int
957 rte_flow_conv(enum rte_flow_conv_op op,
958               void *dst,
959               size_t size,
960               const void *src,
961               struct rte_flow_error *error)
962 {
963         switch (op) {
964                 const struct rte_flow_attr *attr;
965
966         case RTE_FLOW_CONV_OP_NONE:
967                 return 0;
968         case RTE_FLOW_CONV_OP_ATTR:
969                 attr = src;
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);
992         }
993         return rte_flow_error_set
994                 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
995                  "unknown object conversion operation");
996 }
997
998 /** Store a full rte_flow description. */
999 size_t
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)
1004 {
1005         /*
1006          * Overlap struct rte_flow_conv with struct rte_flow_desc in order
1007          * to convert the former to the latter without wasting space.
1008          */
1009         struct rte_flow_conv_rule *dst =
1010                 len ?
1011                 (void *)((uintptr_t)desc +
1012                          (offsetof(struct rte_flow_desc, actions) -
1013                           offsetof(struct rte_flow_conv_rule, actions))) :
1014                 NULL;
1015         size_t dst_size =
1016                 len > sizeof(*desc) - sizeof(*dst) ?
1017                 len - (sizeof(*desc) - sizeof(*dst)) :
1018                 0;
1019         struct rte_flow_conv_rule src = {
1020                 .attr_ro = NULL,
1021                 .pattern_ro = items,
1022                 .actions_ro = actions,
1023         };
1024         int ret;
1025
1026         RTE_BUILD_BUG_ON(sizeof(struct rte_flow_desc) <
1027                          sizeof(struct rte_flow_conv_rule));
1028         if (dst_size &&
1029             (&dst->pattern != &desc->items ||
1030              &dst->actions != &desc->actions ||
1031              (uintptr_t)(dst + 1) != (uintptr_t)(desc + 1))) {
1032                 rte_errno = EINVAL;
1033                 return 0;
1034         }
1035         ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, dst, dst_size, &src, NULL);
1036         if (ret < 0)
1037                 return 0;
1038         ret += sizeof(*desc) - sizeof(*dst);
1039         rte_memcpy(desc,
1040                    (&(struct rte_flow_desc){
1041                         .size = ret,
1042                         .attr = *attr,
1043                         .items = dst_size ? dst->pattern : NULL,
1044                         .actions = dst_size ? dst->actions : NULL,
1045                    }),
1046                    len > sizeof(*desc) ? sizeof(*desc) : len);
1047         return ret;
1048 }
1049
1050 /**
1051  * Expand RSS flows into several possible flows according to the RSS hash
1052  * fields requested and the driver capabilities.
1053  */
1054 int
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)
1059 {
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];
1065         int stack_pos = 0;
1066         struct rte_flow_item flow_items[elt_n];
1067         unsigned int i;
1068         size_t lsize;
1069         size_t user_pattern_size = 0;
1070         void *addr = NULL;
1071         const struct rte_flow_expand_node *next = NULL;
1072         struct rte_flow_item missed_item;
1073         int missed = 0;
1074         int elt = 0;
1075         const struct rte_flow_item *last_item = NULL;
1076
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];
1083                 buf->entries = 0;
1084                 addr = buf->entry[0].pattern;
1085         }
1086         for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
1087                 if (item->type != RTE_FLOW_ITEM_TYPE_VOID)
1088                         last_item = item;
1089                 for (i = 0; node->next && node->next[i]; ++i) {
1090                         next = &graph[node->next[i]];
1091                         if (next->type == item->type)
1092                                 break;
1093                 }
1094                 if (next)
1095                         node = next;
1096                 user_pattern_size += sizeof(*item);
1097         }
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);
1104                 buf->entries = 1;
1105         }
1106         /* Start expanding. */
1107         memset(flow_items, 0, sizeof(flow_items));
1108         user_pattern_size -= sizeof(*item);
1109         /*
1110          * Check if the last valid item has spec set
1111          * and need complete pattern.
1112          */
1113         missed_item.type = rte_flow_expand_rss_item_complete(last_item);
1114         if (missed_item.type != RTE_FLOW_ITEM_TYPE_VOID) {
1115                 next = NULL;
1116                 missed = 1;
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;
1122                                 break;
1123                         }
1124                         next = NULL;
1125                 }
1126         }
1127         if (next && missed) {
1128                 elt = 2; /* missed item + item end. */
1129                 node = next;
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;
1134                         buf->entries++;
1135                         rte_memcpy(addr, buf->entry[0].pattern,
1136                                    user_pattern_size);
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));
1141                 }
1142         }
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;
1147         while (node) {
1148                 flow_items[stack_pos].type = node->type;
1149                 if (node->rss_types & types) {
1150                         /*
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.
1155                          */
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);
1161
1162                                 buf->entry[buf->entries].priority =
1163                                         stack_pos + 1 + missed;
1164                                 buf->entry[buf->entries].pattern = addr;
1165                                 buf->entries++;
1166                                 rte_memcpy(addr, buf->entry[0].pattern,
1167                                            user_pattern_size);
1168                                 addr = (void *)(((uintptr_t)addr) +
1169                                                 user_pattern_size);
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);
1176                         }
1177                 }
1178                 /* Go deeper. */
1179                 if (node->next) {
1180                         next_node = node->next;
1181                         if (stack_pos++ == elt_n) {
1182                                 rte_errno = E2BIG;
1183                                 return -rte_errno;
1184                         }
1185                         stack[stack_pos] = next_node;
1186                 } else if (*(next_node + 1)) {
1187                         /* Follow up with the next possibility. */
1188                         ++next_node;
1189                 } else {
1190                         /* Move to the next path. */
1191                         if (stack_pos)
1192                                 next_node = stack[--stack_pos];
1193                         next_node++;
1194                         stack[stack_pos] = next_node;
1195                 }
1196                 node = *next_node ? &graph[*next_node] : NULL;
1197         };
1198         /* no expanded flows but we have missed item, create one rule for it */
1199         if (buf->entries == 1 && missed != 0) {
1200                 elt = 2;
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;
1205                         buf->entries++;
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,
1209                                    user_pattern_size);
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));
1214                 }
1215         }
1216         return lsize;
1217 }
1218
1219 int
1220 rte_flow_dev_dump(uint16_t port_id, FILE *file, struct rte_flow_error *error)
1221 {
1222         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1223         const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
1224
1225         if (unlikely(!ops))
1226                 return -rte_errno;
1227         if (likely(!!ops->dev_dump))
1228                 return flow_err(port_id, ops->dev_dump(dev, file, error),
1229                                 error);
1230         return rte_flow_error_set(error, ENOSYS,
1231                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1232                                   NULL, rte_strerror(ENOSYS));
1233 }