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