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