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