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