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