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