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