ethdev: fix shallow copy of flow API RSS action
[dpdk.git] / lib / librte_ether / 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_ethdev.h"
15 #include "rte_flow_driver.h"
16 #include "rte_flow.h"
17
18 /**
19  * Flow elements description tables.
20  */
21 struct rte_flow_desc_data {
22         const char *name;
23         size_t size;
24 };
25
26 /** Generate flow_item[] entry. */
27 #define MK_FLOW_ITEM(t, s) \
28         [RTE_FLOW_ITEM_TYPE_ ## t] = { \
29                 .name = # t, \
30                 .size = s, \
31         }
32
33 /** Information about known flow pattern items. */
34 static const struct rte_flow_desc_data rte_flow_desc_item[] = {
35         MK_FLOW_ITEM(END, 0),
36         MK_FLOW_ITEM(VOID, 0),
37         MK_FLOW_ITEM(INVERT, 0),
38         MK_FLOW_ITEM(ANY, sizeof(struct rte_flow_item_any)),
39         MK_FLOW_ITEM(PF, 0),
40         MK_FLOW_ITEM(VF, sizeof(struct rte_flow_item_vf)),
41         MK_FLOW_ITEM(PORT, sizeof(struct rte_flow_item_port)),
42         MK_FLOW_ITEM(RAW, sizeof(struct rte_flow_item_raw)), /* +pattern[] */
43         MK_FLOW_ITEM(ETH, sizeof(struct rte_flow_item_eth)),
44         MK_FLOW_ITEM(VLAN, sizeof(struct rte_flow_item_vlan)),
45         MK_FLOW_ITEM(IPV4, sizeof(struct rte_flow_item_ipv4)),
46         MK_FLOW_ITEM(IPV6, sizeof(struct rte_flow_item_ipv6)),
47         MK_FLOW_ITEM(ICMP, sizeof(struct rte_flow_item_icmp)),
48         MK_FLOW_ITEM(UDP, sizeof(struct rte_flow_item_udp)),
49         MK_FLOW_ITEM(TCP, sizeof(struct rte_flow_item_tcp)),
50         MK_FLOW_ITEM(SCTP, sizeof(struct rte_flow_item_sctp)),
51         MK_FLOW_ITEM(VXLAN, sizeof(struct rte_flow_item_vxlan)),
52         MK_FLOW_ITEM(MPLS, sizeof(struct rte_flow_item_mpls)),
53         MK_FLOW_ITEM(GRE, sizeof(struct rte_flow_item_gre)),
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(GENEVE, sizeof(struct rte_flow_item_geneve)),
57 };
58
59 /** Generate flow_action[] entry. */
60 #define MK_FLOW_ACTION(t, s) \
61         [RTE_FLOW_ACTION_TYPE_ ## t] = { \
62                 .name = # t, \
63                 .size = s, \
64         }
65
66 /** Information about known flow actions. */
67 static const struct rte_flow_desc_data rte_flow_desc_action[] = {
68         MK_FLOW_ACTION(END, 0),
69         MK_FLOW_ACTION(VOID, 0),
70         MK_FLOW_ACTION(PASSTHRU, 0),
71         MK_FLOW_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
72         MK_FLOW_ACTION(FLAG, 0),
73         MK_FLOW_ACTION(QUEUE, sizeof(struct rte_flow_action_queue)),
74         MK_FLOW_ACTION(DROP, 0),
75         MK_FLOW_ACTION(COUNT, 0),
76         MK_FLOW_ACTION(DUP, sizeof(struct rte_flow_action_dup)),
77         MK_FLOW_ACTION(RSS, sizeof(struct rte_flow_action_rss)), /* +queue[] */
78         MK_FLOW_ACTION(PF, 0),
79         MK_FLOW_ACTION(VF, sizeof(struct rte_flow_action_vf)),
80 };
81
82 static int
83 flow_err(uint16_t port_id, int ret, struct rte_flow_error *error)
84 {
85         if (ret == 0)
86                 return 0;
87         if (rte_eth_dev_is_removed(port_id))
88                 return rte_flow_error_set(error, EIO,
89                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
90                                           NULL, rte_strerror(EIO));
91         return ret;
92 }
93
94 /* Get generic flow operations structure from a port. */
95 const struct rte_flow_ops *
96 rte_flow_ops_get(uint16_t port_id, struct rte_flow_error *error)
97 {
98         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
99         const struct rte_flow_ops *ops;
100         int code;
101
102         if (unlikely(!rte_eth_dev_is_valid_port(port_id)))
103                 code = ENODEV;
104         else if (unlikely(!dev->dev_ops->filter_ctrl ||
105                           dev->dev_ops->filter_ctrl(dev,
106                                                     RTE_ETH_FILTER_GENERIC,
107                                                     RTE_ETH_FILTER_GET,
108                                                     &ops) ||
109                           !ops))
110                 code = ENOSYS;
111         else
112                 return ops;
113         rte_flow_error_set(error, code, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
114                            NULL, rte_strerror(code));
115         return NULL;
116 }
117
118 /* Check whether a flow rule can be created on a given port. */
119 int
120 rte_flow_validate(uint16_t port_id,
121                   const struct rte_flow_attr *attr,
122                   const struct rte_flow_item pattern[],
123                   const struct rte_flow_action actions[],
124                   struct rte_flow_error *error)
125 {
126         const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
127         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
128
129         if (unlikely(!ops))
130                 return -rte_errno;
131         if (likely(!!ops->validate))
132                 return flow_err(port_id, ops->validate(dev, attr, pattern,
133                                                        actions, error), error);
134         return rte_flow_error_set(error, ENOSYS,
135                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
136                                   NULL, rte_strerror(ENOSYS));
137 }
138
139 /* Create a flow rule on a given port. */
140 struct rte_flow *
141 rte_flow_create(uint16_t port_id,
142                 const struct rte_flow_attr *attr,
143                 const struct rte_flow_item pattern[],
144                 const struct rte_flow_action actions[],
145                 struct rte_flow_error *error)
146 {
147         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
148         struct rte_flow *flow;
149         const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
150
151         if (unlikely(!ops))
152                 return NULL;
153         if (likely(!!ops->create)) {
154                 flow = ops->create(dev, attr, pattern, actions, error);
155                 if (flow == NULL)
156                         flow_err(port_id, -rte_errno, error);
157                 return flow;
158         }
159         rte_flow_error_set(error, ENOSYS, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
160                            NULL, rte_strerror(ENOSYS));
161         return NULL;
162 }
163
164 /* Destroy a flow rule on a given port. */
165 int
166 rte_flow_destroy(uint16_t port_id,
167                  struct rte_flow *flow,
168                  struct rte_flow_error *error)
169 {
170         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
171         const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
172
173         if (unlikely(!ops))
174                 return -rte_errno;
175         if (likely(!!ops->destroy))
176                 return flow_err(port_id, ops->destroy(dev, flow, error),
177                                 error);
178         return rte_flow_error_set(error, ENOSYS,
179                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
180                                   NULL, rte_strerror(ENOSYS));
181 }
182
183 /* Destroy all flow rules associated with a port. */
184 int
185 rte_flow_flush(uint16_t port_id,
186                struct rte_flow_error *error)
187 {
188         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
189         const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
190
191         if (unlikely(!ops))
192                 return -rte_errno;
193         if (likely(!!ops->flush))
194                 return flow_err(port_id, ops->flush(dev, error), error);
195         return rte_flow_error_set(error, ENOSYS,
196                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
197                                   NULL, rte_strerror(ENOSYS));
198 }
199
200 /* Query an existing flow rule. */
201 int
202 rte_flow_query(uint16_t port_id,
203                struct rte_flow *flow,
204                enum rte_flow_action_type action,
205                void *data,
206                struct rte_flow_error *error)
207 {
208         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
209         const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
210
211         if (!ops)
212                 return -rte_errno;
213         if (likely(!!ops->query))
214                 return flow_err(port_id, ops->query(dev, flow, action, data,
215                                                     error), error);
216         return rte_flow_error_set(error, ENOSYS,
217                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
218                                   NULL, rte_strerror(ENOSYS));
219 }
220
221 /* Restrict ingress traffic to the defined flow rules. */
222 int
223 rte_flow_isolate(uint16_t port_id,
224                  int set,
225                  struct rte_flow_error *error)
226 {
227         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
228         const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error);
229
230         if (!ops)
231                 return -rte_errno;
232         if (likely(!!ops->isolate))
233                 return flow_err(port_id, ops->isolate(dev, set, error), error);
234         return rte_flow_error_set(error, ENOSYS,
235                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
236                                   NULL, rte_strerror(ENOSYS));
237 }
238
239 /* Initialize flow error structure. */
240 int
241 rte_flow_error_set(struct rte_flow_error *error,
242                    int code,
243                    enum rte_flow_error_type type,
244                    const void *cause,
245                    const char *message)
246 {
247         if (error) {
248                 *error = (struct rte_flow_error){
249                         .type = type,
250                         .cause = cause,
251                         .message = message,
252                 };
253         }
254         rte_errno = code;
255         return -code;
256 }
257
258 /** Pattern item specification types. */
259 enum item_spec_type {
260         ITEM_SPEC,
261         ITEM_LAST,
262         ITEM_MASK,
263 };
264
265 /** Compute storage space needed by item specification and copy it. */
266 static size_t
267 flow_item_spec_copy(void *buf, const struct rte_flow_item *item,
268                     enum item_spec_type type)
269 {
270         size_t size = 0;
271         const void *item_spec =
272                 type == ITEM_SPEC ? item->spec :
273                 type == ITEM_LAST ? item->last :
274                 type == ITEM_MASK ? item->mask :
275                 NULL;
276
277         if (!item_spec)
278                 goto empty;
279         switch (item->type) {
280                 union {
281                         const struct rte_flow_item_raw *raw;
282                 } src;
283                 union {
284                         struct rte_flow_item_raw *raw;
285                 } dst;
286
287         case RTE_FLOW_ITEM_TYPE_RAW:
288                 src.raw = item_spec;
289                 dst.raw = buf;
290                 size = offsetof(struct rte_flow_item_raw, pattern) +
291                         src.raw->length * sizeof(*src.raw->pattern);
292                 if (dst.raw)
293                         memcpy(dst.raw, src.raw, size);
294                 break;
295         default:
296                 size = rte_flow_desc_item[item->type].size;
297                 if (buf)
298                         memcpy(buf, item_spec, size);
299                 break;
300         }
301 empty:
302         return RTE_ALIGN_CEIL(size, sizeof(double));
303 }
304
305 /** Compute storage space needed by action configuration and copy it. */
306 static size_t
307 flow_action_conf_copy(void *buf, const struct rte_flow_action *action)
308 {
309         size_t size = 0;
310
311         if (!action->conf)
312                 goto empty;
313         switch (action->type) {
314                 union {
315                         const struct rte_flow_action_rss *rss;
316                 } src;
317                 union {
318                         struct rte_flow_action_rss *rss;
319                 } dst;
320                 size_t off;
321
322         case RTE_FLOW_ACTION_TYPE_RSS:
323                 src.rss = action->conf;
324                 dst.rss = buf;
325                 off = 0;
326                 if (dst.rss)
327                         *dst.rss = (struct rte_flow_action_rss){
328                                 .num = src.rss->num,
329                         };
330                 off += offsetof(struct rte_flow_action_rss, queue);
331                 if (src.rss->num) {
332                         size = sizeof(*src.rss->queue) * src.rss->num;
333                         if (dst.rss)
334                                 memcpy(dst.rss->queue, src.rss->queue, size);
335                         off += size;
336                 }
337                 off = RTE_ALIGN_CEIL(off, sizeof(double));
338                 if (dst.rss) {
339                         dst.rss->rss_conf = (void *)((uintptr_t)dst.rss + off);
340                         *(struct rte_eth_rss_conf *)(uintptr_t)
341                                 dst.rss->rss_conf = (struct rte_eth_rss_conf){
342                                 .rss_key_len = src.rss->rss_conf->rss_key_len,
343                                 .rss_hf = src.rss->rss_conf->rss_hf,
344                         };
345                 }
346                 off += sizeof(*src.rss->rss_conf);
347                 if (src.rss->rss_conf->rss_key_len) {
348                         off = RTE_ALIGN_CEIL(off, sizeof(double));
349                         size = sizeof(*src.rss->rss_conf->rss_key) *
350                                 src.rss->rss_conf->rss_key_len;
351                         if (dst.rss) {
352                                 ((struct rte_eth_rss_conf *)(uintptr_t)
353                                  dst.rss->rss_conf)->rss_key =
354                                         (void *)((uintptr_t)dst.rss + off);
355                                 memcpy(dst.rss->rss_conf->rss_key,
356                                        src.rss->rss_conf->rss_key,
357                                        size);
358                         }
359                         off += size;
360                 }
361                 size = off;
362                 break;
363         default:
364                 size = rte_flow_desc_action[action->type].size;
365                 if (buf)
366                         memcpy(buf, action->conf, size);
367                 break;
368         }
369 empty:
370         return RTE_ALIGN_CEIL(size, sizeof(double));
371 }
372
373 /** Store a full rte_flow description. */
374 size_t
375 rte_flow_copy(struct rte_flow_desc *desc, size_t len,
376               const struct rte_flow_attr *attr,
377               const struct rte_flow_item *items,
378               const struct rte_flow_action *actions)
379 {
380         struct rte_flow_desc *fd = NULL;
381         size_t tmp;
382         size_t off1 = 0;
383         size_t off2 = 0;
384         size_t size = 0;
385
386 store:
387         if (items) {
388                 const struct rte_flow_item *item;
389
390                 item = items;
391                 if (fd)
392                         fd->items = (void *)&fd->data[off1];
393                 do {
394                         struct rte_flow_item *dst = NULL;
395
396                         if ((size_t)item->type >=
397                                 RTE_DIM(rte_flow_desc_item) ||
398                             !rte_flow_desc_item[item->type].name) {
399                                 rte_errno = ENOTSUP;
400                                 return 0;
401                         }
402                         if (fd)
403                                 dst = memcpy(fd->data + off1, item,
404                                              sizeof(*item));
405                         off1 += sizeof(*item);
406                         if (item->spec) {
407                                 if (fd)
408                                         dst->spec = fd->data + off2;
409                                 off2 += flow_item_spec_copy
410                                         (fd ? fd->data + off2 : NULL, item,
411                                          ITEM_SPEC);
412                         }
413                         if (item->last) {
414                                 if (fd)
415                                         dst->last = fd->data + off2;
416                                 off2 += flow_item_spec_copy
417                                         (fd ? fd->data + off2 : NULL, item,
418                                          ITEM_LAST);
419                         }
420                         if (item->mask) {
421                                 if (fd)
422                                         dst->mask = fd->data + off2;
423                                 off2 += flow_item_spec_copy
424                                         (fd ? fd->data + off2 : NULL, item,
425                                          ITEM_MASK);
426                         }
427                         off2 = RTE_ALIGN_CEIL(off2, sizeof(double));
428                 } while ((item++)->type != RTE_FLOW_ITEM_TYPE_END);
429                 off1 = RTE_ALIGN_CEIL(off1, sizeof(double));
430         }
431         if (actions) {
432                 const struct rte_flow_action *action;
433
434                 action = actions;
435                 if (fd)
436                         fd->actions = (void *)&fd->data[off1];
437                 do {
438                         struct rte_flow_action *dst = NULL;
439
440                         if ((size_t)action->type >=
441                                 RTE_DIM(rte_flow_desc_action) ||
442                             !rte_flow_desc_action[action->type].name) {
443                                 rte_errno = ENOTSUP;
444                                 return 0;
445                         }
446                         if (fd)
447                                 dst = memcpy(fd->data + off1, action,
448                                              sizeof(*action));
449                         off1 += sizeof(*action);
450                         if (action->conf) {
451                                 if (fd)
452                                         dst->conf = fd->data + off2;
453                                 off2 += flow_action_conf_copy
454                                         (fd ? fd->data + off2 : NULL, action);
455                         }
456                         off2 = RTE_ALIGN_CEIL(off2, sizeof(double));
457                 } while ((action++)->type != RTE_FLOW_ACTION_TYPE_END);
458         }
459         if (fd != NULL)
460                 return size;
461         off1 = RTE_ALIGN_CEIL(off1, sizeof(double));
462         tmp = RTE_ALIGN_CEIL(offsetof(struct rte_flow_desc, data),
463                              sizeof(double));
464         size = tmp + off1 + off2;
465         if (size > len)
466                 return size;
467         fd = desc;
468         if (fd != NULL) {
469                 *fd = (const struct rte_flow_desc) {
470                         .size = size,
471                         .attr = *attr,
472                 };
473                 tmp -= offsetof(struct rte_flow_desc, data);
474                 off2 = tmp + off1;
475                 off1 = tmp;
476                 goto store;
477         }
478         return 0;
479 }