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