net/cxgbe: implement flow create operation
[dpdk.git] / drivers / net / cxgbe / cxgbe_flow.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Chelsio Communications.
3  * All rights reserved.
4  */
5 #include "common.h"
6 #include "cxgbe_flow.h"
7
8 #define __CXGBE_FILL_FS(__v, __m, fs, elem, e) \
9 do { \
10         if (!((fs)->val.elem || (fs)->mask.elem)) { \
11                 (fs)->val.elem = (__v); \
12                 (fs)->mask.elem = (__m); \
13         } else { \
14                 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, \
15                                           NULL, "a filter can be specified" \
16                                           " only once"); \
17         } \
18 } while (0)
19
20 #define __CXGBE_FILL_FS_MEMCPY(__v, __m, fs, elem) \
21 do { \
22         memcpy(&(fs)->val.elem, &(__v), sizeof(__v)); \
23         memcpy(&(fs)->mask.elem, &(__m), sizeof(__m)); \
24 } while (0)
25
26 #define CXGBE_FILL_FS(v, m, elem) \
27         __CXGBE_FILL_FS(v, m, fs, elem, e)
28
29 #define CXGBE_FILL_FS_MEMCPY(v, m, elem) \
30         __CXGBE_FILL_FS_MEMCPY(v, m, fs, elem)
31
32 static int
33 cxgbe_validate_item(const struct rte_flow_item *i, struct rte_flow_error *e)
34 {
35         /* rte_flow specification does not allow it. */
36         if (!i->spec && (i->mask ||  i->last))
37                 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
38                                    i, "last or mask given without spec");
39         /*
40          * We don't support it.
41          * Although, we can support values in last as 0's or last == spec.
42          * But this will not provide user with any additional functionality
43          * and will only increase the complexity for us.
44          */
45         if (i->last)
46                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
47                                    i, "last is not supported by chelsio pmd");
48         return 0;
49 }
50
51 static int
52 ch_rte_parsetype_udp(const void *dmask, const struct rte_flow_item *item,
53                      struct ch_filter_specification *fs,
54                      struct rte_flow_error *e)
55 {
56         const struct rte_flow_item_udp *val = item->spec;
57         const struct rte_flow_item_udp *umask = item->mask;
58         const struct rte_flow_item_udp *mask;
59
60         mask = umask ? umask : (const struct rte_flow_item_udp *)dmask;
61
62         if (mask->hdr.dgram_len || mask->hdr.dgram_cksum)
63                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
64                                           item,
65                                           "udp: only src/dst port supported");
66
67         CXGBE_FILL_FS(IPPROTO_UDP, 0xff, proto);
68         if (!val)
69                 return 0;
70         CXGBE_FILL_FS(be16_to_cpu(val->hdr.src_port),
71                       be16_to_cpu(mask->hdr.src_port), fport);
72         CXGBE_FILL_FS(be16_to_cpu(val->hdr.dst_port),
73                       be16_to_cpu(mask->hdr.dst_port), lport);
74         return 0;
75 }
76
77 static int
78 ch_rte_parsetype_tcp(const void *dmask, const struct rte_flow_item *item,
79                      struct ch_filter_specification *fs,
80                      struct rte_flow_error *e)
81 {
82         const struct rte_flow_item_tcp *val = item->spec;
83         const struct rte_flow_item_tcp *umask = item->mask;
84         const struct rte_flow_item_tcp *mask;
85
86         mask = umask ? umask : (const struct rte_flow_item_tcp *)dmask;
87
88         if (mask->hdr.sent_seq || mask->hdr.recv_ack || mask->hdr.data_off ||
89             mask->hdr.tcp_flags || mask->hdr.rx_win || mask->hdr.cksum ||
90             mask->hdr.tcp_urp)
91                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
92                                           item,
93                                           "tcp: only src/dst port supported");
94
95         CXGBE_FILL_FS(IPPROTO_TCP, 0xff, proto);
96         if (!val)
97                 return 0;
98         CXGBE_FILL_FS(be16_to_cpu(val->hdr.src_port),
99                       be16_to_cpu(mask->hdr.src_port), fport);
100         CXGBE_FILL_FS(be16_to_cpu(val->hdr.dst_port),
101                       be16_to_cpu(mask->hdr.dst_port), lport);
102         return 0;
103 }
104
105 static int
106 ch_rte_parsetype_ipv4(const void *dmask, const struct rte_flow_item *item,
107                       struct ch_filter_specification *fs,
108                       struct rte_flow_error *e)
109 {
110         const struct rte_flow_item_ipv4 *val = item->spec;
111         const struct rte_flow_item_ipv4 *umask = item->mask;
112         const struct rte_flow_item_ipv4 *mask;
113
114         mask = umask ? umask : (const struct rte_flow_item_ipv4 *)dmask;
115
116         if (mask->hdr.time_to_live || mask->hdr.type_of_service)
117                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
118                                           item, "ttl/tos are not supported");
119
120         fs->type = FILTER_TYPE_IPV4;
121         CXGBE_FILL_FS(ETHER_TYPE_IPv4, 0xffff, ethtype);
122         if (!val)
123                 return 0; /* ipv4 wild card */
124
125         CXGBE_FILL_FS(val->hdr.next_proto_id, mask->hdr.next_proto_id, proto);
126         CXGBE_FILL_FS_MEMCPY(val->hdr.dst_addr, mask->hdr.dst_addr, lip);
127         CXGBE_FILL_FS_MEMCPY(val->hdr.src_addr, mask->hdr.src_addr, fip);
128
129         return 0;
130 }
131
132 static int
133 ch_rte_parsetype_ipv6(const void *dmask, const struct rte_flow_item *item,
134                       struct ch_filter_specification *fs,
135                       struct rte_flow_error *e)
136 {
137         const struct rte_flow_item_ipv6 *val = item->spec;
138         const struct rte_flow_item_ipv6 *umask = item->mask;
139         const struct rte_flow_item_ipv6 *mask;
140
141         mask = umask ? umask : (const struct rte_flow_item_ipv6 *)dmask;
142
143         if (mask->hdr.vtc_flow ||
144             mask->hdr.payload_len || mask->hdr.hop_limits)
145                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
146                                           item,
147                                           "tc/flow/hop are not supported");
148
149         fs->type = FILTER_TYPE_IPV6;
150         CXGBE_FILL_FS(ETHER_TYPE_IPv6, 0xffff, ethtype);
151         if (!val)
152                 return 0; /* ipv6 wild card */
153
154         CXGBE_FILL_FS(val->hdr.proto, mask->hdr.proto, proto);
155         CXGBE_FILL_FS_MEMCPY(val->hdr.dst_addr, mask->hdr.dst_addr, lip);
156         CXGBE_FILL_FS_MEMCPY(val->hdr.src_addr, mask->hdr.src_addr, fip);
157
158         return 0;
159 }
160
161 static int
162 cxgbe_rtef_parse_attr(struct rte_flow *flow, const struct rte_flow_attr *attr,
163                       struct rte_flow_error *e)
164 {
165         if (attr->egress)
166                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR,
167                                           attr, "attribute:<egress> is"
168                                           " not supported !");
169         if (attr->group > 0)
170                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR,
171                                           attr, "group parameter is"
172                                           " not supported.");
173
174         flow->fidx = attr->priority ? attr->priority - 1 : FILTER_ID_MAX;
175
176         return 0;
177 }
178
179 static inline int check_rxq(struct rte_eth_dev *dev, uint16_t rxq)
180 {
181         struct port_info *pi = ethdev2pinfo(dev);
182
183         if (rxq > pi->n_rx_qsets)
184                 return -EINVAL;
185         return 0;
186 }
187
188 static int cxgbe_validate_fidxondel(struct filter_entry *f, unsigned int fidx)
189 {
190         struct adapter *adap = ethdev2adap(f->dev);
191         struct ch_filter_specification fs = f->fs;
192
193         if (fidx >= adap->tids.nftids) {
194                 dev_err(adap, "invalid flow index %d.\n", fidx);
195                 return -EINVAL;
196         }
197         if (!is_filter_set(&adap->tids, fidx, fs.type)) {
198                 dev_err(adap, "Already free fidx:%d f:%p\n", fidx, f);
199                 return -EINVAL;
200         }
201
202         return 0;
203 }
204
205 static int
206 cxgbe_validate_fidxonadd(struct ch_filter_specification *fs,
207                          struct adapter *adap, unsigned int fidx)
208 {
209         if (is_filter_set(&adap->tids, fidx, fs->type)) {
210                 dev_err(adap, "filter index: %d is busy.\n", fidx);
211                 return -EBUSY;
212         }
213         if (fidx >= adap->tids.nftids) {
214                 dev_err(adap, "filter index (%u) >= max(%u)\n",
215                         fidx, adap->tids.nftids);
216                 return -ERANGE;
217         }
218
219         return 0;
220 }
221
222 static int
223 cxgbe_verify_fidx(struct rte_flow *flow, unsigned int fidx, uint8_t del)
224 {
225         return del ? cxgbe_validate_fidxondel(flow->f, fidx) :
226                 cxgbe_validate_fidxonadd(&flow->fs,
227                                          ethdev2adap(flow->dev), fidx);
228 }
229
230 static int cxgbe_get_fidx(struct rte_flow *flow, unsigned int *fidx)
231 {
232         struct ch_filter_specification *fs = &flow->fs;
233         struct adapter *adap = ethdev2adap(flow->dev);
234
235         /* For tcam get the next available slot, if default value specified */
236         if (flow->fidx == FILTER_ID_MAX) {
237                 int idx;
238
239                 idx = cxgbe_alloc_ftid(adap, fs->type);
240                 if (idx < 0) {
241                         dev_err(adap, "unable to get a filter index in tcam\n");
242                         return -ENOMEM;
243                 }
244                 *fidx = (unsigned int)idx;
245         } else {
246                 *fidx = flow->fidx;
247         }
248
249         return 0;
250 }
251
252 static int
253 cxgbe_rtef_parse_actions(struct rte_flow *flow,
254                          const struct rte_flow_action action[],
255                          struct rte_flow_error *e)
256 {
257         struct ch_filter_specification *fs = &flow->fs;
258         const struct rte_flow_action_queue *q;
259         const struct rte_flow_action *a;
260         char abit = 0;
261
262         for (a = action; a->type != RTE_FLOW_ACTION_TYPE_END; a++) {
263                 switch (a->type) {
264                 case RTE_FLOW_ACTION_TYPE_VOID:
265                         continue;
266                 case RTE_FLOW_ACTION_TYPE_DROP:
267                         if (abit++)
268                                 return rte_flow_error_set(e, EINVAL,
269                                                 RTE_FLOW_ERROR_TYPE_ACTION, a,
270                                                 "specify only 1 pass/drop");
271                         fs->action = FILTER_DROP;
272                         break;
273                 case RTE_FLOW_ACTION_TYPE_QUEUE:
274                         q = (const struct rte_flow_action_queue *)a->conf;
275                         if (!q)
276                                 return rte_flow_error_set(e, EINVAL,
277                                                 RTE_FLOW_ERROR_TYPE_ACTION, q,
278                                                 "specify rx queue index");
279                         if (check_rxq(flow->dev, q->index))
280                                 return rte_flow_error_set(e, EINVAL,
281                                                 RTE_FLOW_ERROR_TYPE_ACTION, q,
282                                                 "Invalid rx queue");
283                         if (abit++)
284                                 return rte_flow_error_set(e, EINVAL,
285                                                 RTE_FLOW_ERROR_TYPE_ACTION, a,
286                                                 "specify only 1 pass/drop");
287                         fs->action = FILTER_PASS;
288                         fs->dirsteer = 1;
289                         fs->iq = q->index;
290                         break;
291                 case RTE_FLOW_ACTION_TYPE_COUNT:
292                         fs->hitcnts = 1;
293                         break;
294                 default:
295                         /* Not supported action : return error */
296                         return rte_flow_error_set(e, ENOTSUP,
297                                                   RTE_FLOW_ERROR_TYPE_ACTION,
298                                                   a, "Action not supported");
299                 }
300         }
301
302         return 0;
303 }
304
305 struct chrte_fparse parseitem[] = {
306         [RTE_FLOW_ITEM_TYPE_IPV4] = {
307                 .fptr  = ch_rte_parsetype_ipv4,
308                 .dmask = &rte_flow_item_ipv4_mask,
309         },
310
311         [RTE_FLOW_ITEM_TYPE_IPV6] = {
312                 .fptr  = ch_rte_parsetype_ipv6,
313                 .dmask = &rte_flow_item_ipv6_mask,
314         },
315
316         [RTE_FLOW_ITEM_TYPE_UDP] = {
317                 .fptr  = ch_rte_parsetype_udp,
318                 .dmask = &rte_flow_item_udp_mask,
319         },
320
321         [RTE_FLOW_ITEM_TYPE_TCP] = {
322                 .fptr  = ch_rte_parsetype_tcp,
323                 .dmask = &rte_flow_item_tcp_mask,
324         },
325 };
326
327 static int
328 cxgbe_rtef_parse_items(struct rte_flow *flow,
329                        const struct rte_flow_item items[],
330                        struct rte_flow_error *e)
331 {
332         const struct rte_flow_item *i;
333         char repeat[ARRAY_SIZE(parseitem)] = {0};
334
335         for (i = items; i->type != RTE_FLOW_ITEM_TYPE_END; i++) {
336                 struct chrte_fparse *idx = &flow->item_parser[i->type];
337                 int ret;
338
339                 if (i->type > ARRAY_SIZE(parseitem))
340                         return rte_flow_error_set(e, ENOTSUP,
341                                                   RTE_FLOW_ERROR_TYPE_ITEM,
342                                                   i, "Item not supported");
343
344                 switch (i->type) {
345                 case RTE_FLOW_ITEM_TYPE_VOID:
346                         continue;
347                 default:
348                         /* check if item is repeated */
349                         if (repeat[i->type])
350                                 return rte_flow_error_set(e, ENOTSUP,
351                                                 RTE_FLOW_ERROR_TYPE_ITEM, i,
352                                                 "parse items cannot be repeated (except void)");
353                         repeat[i->type] = 1;
354
355                         /* validate the item */
356                         ret = cxgbe_validate_item(i, e);
357                         if (ret)
358                                 return ret;
359
360                         if (!idx || !idx->fptr) {
361                                 return rte_flow_error_set(e, ENOTSUP,
362                                                 RTE_FLOW_ERROR_TYPE_ITEM, i,
363                                                 "Item not supported");
364                         } else {
365                                 ret = idx->fptr(idx->dmask, i, &flow->fs, e);
366                                 if (ret)
367                                         return ret;
368                         }
369                 }
370         }
371
372         return 0;
373 }
374
375 static int
376 cxgbe_flow_parse(struct rte_flow *flow,
377                  const struct rte_flow_attr *attr,
378                  const struct rte_flow_item item[],
379                  const struct rte_flow_action action[],
380                  struct rte_flow_error *e)
381 {
382         int ret;
383
384         /* parse user request into ch_filter_specification */
385         ret = cxgbe_rtef_parse_attr(flow, attr, e);
386         if (ret)
387                 return ret;
388         ret = cxgbe_rtef_parse_items(flow, item, e);
389         if (ret)
390                 return ret;
391         return cxgbe_rtef_parse_actions(flow, action, e);
392 }
393
394 static int __cxgbe_flow_create(struct rte_eth_dev *dev, struct rte_flow *flow)
395 {
396         struct ch_filter_specification *fs = &flow->fs;
397         struct adapter *adap = ethdev2adap(dev);
398         struct filter_ctx ctx;
399         unsigned int fidx;
400         int err;
401
402         if (cxgbe_get_fidx(flow, &fidx))
403                 return -ENOMEM;
404         if (cxgbe_verify_fidx(flow, fidx, 0))
405                 return -1;
406
407         t4_init_completion(&ctx.completion);
408         /* go create the filter */
409         err = cxgbe_set_filter(dev, fidx, fs, &ctx);
410         if (err) {
411                 dev_err(adap, "Error %d while creating filter.\n", err);
412                 return err;
413         }
414
415         /* Poll the FW for reply */
416         err = cxgbe_poll_for_completion(&adap->sge.fw_evtq,
417                                         CXGBE_FLOW_POLL_US,
418                                         CXGBE_FLOW_POLL_CNT,
419                                         &ctx.completion);
420         if (err) {
421                 dev_err(adap, "Filter set operation timed out (%d)\n", err);
422                 return err;
423         }
424         if (ctx.result) {
425                 dev_err(adap, "Hardware error %d while creating the filter.\n",
426                         ctx.result);
427                 return ctx.result;
428         }
429
430         flow->fidx = fidx;
431         flow->f = &adap->tids.ftid_tab[fidx];
432
433         return 0;
434 }
435
436 static struct rte_flow *
437 cxgbe_flow_create(struct rte_eth_dev *dev,
438                   const struct rte_flow_attr *attr,
439                   const struct rte_flow_item item[],
440                   const struct rte_flow_action action[],
441                   struct rte_flow_error *e)
442 {
443         struct rte_flow *flow;
444         int ret;
445
446         flow = t4_os_alloc(sizeof(struct rte_flow));
447         if (!flow) {
448                 rte_flow_error_set(e, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
449                                    NULL, "Unable to allocate memory for"
450                                    " filter_entry");
451                 return NULL;
452         }
453
454         flow->item_parser = parseitem;
455         flow->dev = dev;
456
457         if (cxgbe_flow_parse(flow, attr, item, action, e)) {
458                 t4_os_free(flow);
459                 return NULL;
460         }
461
462         /* go, interact with cxgbe_filter */
463         ret = __cxgbe_flow_create(dev, flow);
464         if (ret) {
465                 rte_flow_error_set(e, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
466                                    NULL, "Unable to create flow rule");
467                 t4_os_free(flow);
468                 return NULL;
469         }
470
471         return flow;
472 }
473
474 static int
475 cxgbe_flow_validate(struct rte_eth_dev *dev,
476                     const struct rte_flow_attr *attr,
477                     const struct rte_flow_item item[],
478                     const struct rte_flow_action action[],
479                     struct rte_flow_error *e)
480 {
481         struct adapter *adap = ethdev2adap(dev);
482         struct rte_flow *flow;
483         unsigned int fidx;
484         int ret;
485
486         flow = t4_os_alloc(sizeof(struct rte_flow));
487         if (!flow)
488                 return rte_flow_error_set(e, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
489                                 NULL,
490                                 "Unable to allocate memory for filter_entry");
491
492         flow->item_parser = parseitem;
493         flow->dev = dev;
494
495         ret = cxgbe_flow_parse(flow, attr, item, action, e);
496         if (ret) {
497                 t4_os_free(flow);
498                 return ret;
499         }
500
501         if (validate_filter(adap, &flow->fs)) {
502                 t4_os_free(flow);
503                 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE,
504                                 NULL,
505                                 "validation failed. Check f/w config file.");
506         }
507
508         if (cxgbe_get_fidx(flow, &fidx)) {
509                 t4_os_free(flow);
510                 return rte_flow_error_set(e, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
511                                           NULL, "no memory in tcam.");
512         }
513
514         if (cxgbe_verify_fidx(flow, fidx, 0)) {
515                 t4_os_free(flow);
516                 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE,
517                                           NULL, "validation failed");
518         }
519
520         t4_os_free(flow);
521         return 0;
522 }
523
524 static const struct rte_flow_ops cxgbe_flow_ops = {
525         .validate       = cxgbe_flow_validate,
526         .create         = cxgbe_flow_create,
527         .destroy        = NULL,
528         .flush          = NULL,
529         .query          = NULL,
530         .isolate        = NULL,
531 };
532
533 int
534 cxgbe_dev_filter_ctrl(struct rte_eth_dev *dev,
535                       enum rte_filter_type filter_type,
536                       enum rte_filter_op filter_op,
537                       void *arg)
538 {
539         int ret = 0;
540
541         RTE_SET_USED(dev);
542         switch (filter_type) {
543         case RTE_ETH_FILTER_GENERIC:
544                 if (filter_op != RTE_ETH_FILTER_GET)
545                         return -EINVAL;
546                 *(const void **)arg = &cxgbe_flow_ops;
547                 break;
548         default:
549                 ret = -ENOTSUP;
550                 break;
551         }
552         return ret;
553 }