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