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