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