1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Chelsio Communications.
6 #include "cxgbe_flow.h"
8 #define __CXGBE_FILL_FS(__v, __m, fs, elem, e) \
10 if (!((fs)->val.elem || (fs)->mask.elem)) { \
11 (fs)->val.elem = (__v); \
12 (fs)->mask.elem = (__m); \
14 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, \
15 NULL, "a filter can be specified" \
20 #define __CXGBE_FILL_FS_MEMCPY(__v, __m, fs, elem) \
22 memcpy(&(fs)->val.elem, &(__v), sizeof(__v)); \
23 memcpy(&(fs)->mask.elem, &(__m), sizeof(__m)); \
26 #define CXGBE_FILL_FS(v, m, elem) \
27 __CXGBE_FILL_FS(v, m, fs, elem, e)
29 #define CXGBE_FILL_FS_MEMCPY(v, m, elem) \
30 __CXGBE_FILL_FS_MEMCPY(v, m, fs, elem)
33 cxgbe_validate_item(const struct rte_flow_item *i, struct rte_flow_error *e)
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");
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.
46 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
47 i, "last is not supported by chelsio pmd");
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)
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;
60 mask = umask ? umask : (const struct rte_flow_item_udp *)dmask;
62 if (mask->hdr.dgram_len || mask->hdr.dgram_cksum)
63 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
65 "udp: only src/dst port supported");
67 CXGBE_FILL_FS(IPPROTO_UDP, 0xff, proto);
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);
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)
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;
86 mask = umask ? umask : (const struct rte_flow_item_tcp *)dmask;
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 ||
91 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
93 "tcp: only src/dst port supported");
95 CXGBE_FILL_FS(IPPROTO_TCP, 0xff, proto);
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);
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)
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;
114 mask = umask ? umask : (const struct rte_flow_item_ipv4 *)dmask;
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");
120 fs->type = FILTER_TYPE_IPV4;
121 CXGBE_FILL_FS(ETHER_TYPE_IPv4, 0xffff, ethtype);
123 return 0; /* ipv4 wild card */
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);
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)
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;
141 mask = umask ? umask : (const struct rte_flow_item_ipv6 *)dmask;
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,
147 "tc/flow/hop are not supported");
149 fs->type = FILTER_TYPE_IPV6;
150 CXGBE_FILL_FS(ETHER_TYPE_IPv6, 0xffff, ethtype);
152 return 0; /* ipv6 wild card */
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);
162 cxgbe_rtef_parse_attr(struct rte_flow *flow, const struct rte_flow_attr *attr,
163 struct rte_flow_error *e)
166 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR,
167 attr, "attribute:<egress> is"
170 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR,
171 attr, "group parameter is"
174 flow->fidx = attr->priority ? attr->priority - 1 : FILTER_ID_MAX;
179 static inline int check_rxq(struct rte_eth_dev *dev, uint16_t rxq)
181 struct port_info *pi = ethdev2pinfo(dev);
183 if (rxq > pi->n_rx_qsets)
188 static int cxgbe_validate_fidxondel(struct filter_entry *f, unsigned int fidx)
190 struct adapter *adap = ethdev2adap(f->dev);
191 struct ch_filter_specification fs = f->fs;
193 if (fidx >= adap->tids.nftids) {
194 dev_err(adap, "invalid flow index %d.\n", fidx);
197 if (!is_filter_set(&adap->tids, fidx, fs.type)) {
198 dev_err(adap, "Already free fidx:%d f:%p\n", fidx, f);
206 cxgbe_validate_fidxonadd(struct ch_filter_specification *fs,
207 struct adapter *adap, unsigned int fidx)
209 if (is_filter_set(&adap->tids, fidx, fs->type)) {
210 dev_err(adap, "filter index: %d is busy.\n", fidx);
213 if (fidx >= adap->tids.nftids) {
214 dev_err(adap, "filter index (%u) >= max(%u)\n",
215 fidx, adap->tids.nftids);
223 cxgbe_verify_fidx(struct rte_flow *flow, unsigned int fidx, uint8_t del)
225 return del ? cxgbe_validate_fidxondel(flow->f, fidx) :
226 cxgbe_validate_fidxonadd(&flow->fs,
227 ethdev2adap(flow->dev), fidx);
230 static int cxgbe_get_fidx(struct rte_flow *flow, unsigned int *fidx)
232 struct ch_filter_specification *fs = &flow->fs;
233 struct adapter *adap = ethdev2adap(flow->dev);
235 /* For tcam get the next available slot, if default value specified */
236 if (flow->fidx == FILTER_ID_MAX) {
239 idx = cxgbe_alloc_ftid(adap, fs->type);
241 dev_err(adap, "unable to get a filter index in tcam\n");
244 *fidx = (unsigned int)idx;
253 cxgbe_rtef_parse_actions(struct rte_flow *flow,
254 const struct rte_flow_action action[],
255 struct rte_flow_error *e)
257 struct ch_filter_specification *fs = &flow->fs;
258 const struct rte_flow_action_queue *q;
259 const struct rte_flow_action *a;
262 for (a = action; a->type != RTE_FLOW_ACTION_TYPE_END; a++) {
264 case RTE_FLOW_ACTION_TYPE_VOID:
266 case RTE_FLOW_ACTION_TYPE_DROP:
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;
273 case RTE_FLOW_ACTION_TYPE_QUEUE:
274 q = (const struct rte_flow_action_queue *)a->conf;
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,
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;
291 case RTE_FLOW_ACTION_TYPE_COUNT:
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");
305 struct chrte_fparse parseitem[] = {
306 [RTE_FLOW_ITEM_TYPE_IPV4] = {
307 .fptr = ch_rte_parsetype_ipv4,
308 .dmask = &rte_flow_item_ipv4_mask,
311 [RTE_FLOW_ITEM_TYPE_IPV6] = {
312 .fptr = ch_rte_parsetype_ipv6,
313 .dmask = &rte_flow_item_ipv6_mask,
316 [RTE_FLOW_ITEM_TYPE_UDP] = {
317 .fptr = ch_rte_parsetype_udp,
318 .dmask = &rte_flow_item_udp_mask,
321 [RTE_FLOW_ITEM_TYPE_TCP] = {
322 .fptr = ch_rte_parsetype_tcp,
323 .dmask = &rte_flow_item_tcp_mask,
328 cxgbe_rtef_parse_items(struct rte_flow *flow,
329 const struct rte_flow_item items[],
330 struct rte_flow_error *e)
332 const struct rte_flow_item *i;
333 char repeat[ARRAY_SIZE(parseitem)] = {0};
335 for (i = items; i->type != RTE_FLOW_ITEM_TYPE_END; i++) {
336 struct chrte_fparse *idx = &flow->item_parser[i->type];
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");
345 case RTE_FLOW_ITEM_TYPE_VOID:
348 /* check if item is repeated */
350 return rte_flow_error_set(e, ENOTSUP,
351 RTE_FLOW_ERROR_TYPE_ITEM, i,
352 "parse items cannot be repeated (except void)");
355 /* validate the item */
356 ret = cxgbe_validate_item(i, e);
360 if (!idx || !idx->fptr) {
361 return rte_flow_error_set(e, ENOTSUP,
362 RTE_FLOW_ERROR_TYPE_ITEM, i,
363 "Item not supported");
365 ret = idx->fptr(idx->dmask, i, &flow->fs, e);
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)
384 /* parse user request into ch_filter_specification */
385 ret = cxgbe_rtef_parse_attr(flow, attr, e);
388 ret = cxgbe_rtef_parse_items(flow, item, e);
391 return cxgbe_rtef_parse_actions(flow, action, e);
394 static int __cxgbe_flow_create(struct rte_eth_dev *dev, struct rte_flow *flow)
396 struct ch_filter_specification *fs = &flow->fs;
397 struct adapter *adap = ethdev2adap(dev);
398 struct filter_ctx ctx;
402 if (cxgbe_get_fidx(flow, &fidx))
404 if (cxgbe_verify_fidx(flow, fidx, 0))
407 t4_init_completion(&ctx.completion);
408 /* go create the filter */
409 err = cxgbe_set_filter(dev, fidx, fs, &ctx);
411 dev_err(adap, "Error %d while creating filter.\n", err);
415 /* Poll the FW for reply */
416 err = cxgbe_poll_for_completion(&adap->sge.fw_evtq,
421 dev_err(adap, "Filter set operation timed out (%d)\n", err);
425 dev_err(adap, "Hardware error %d while creating the filter.\n",
431 flow->f = &adap->tids.ftid_tab[fidx];
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)
443 struct rte_flow *flow;
446 flow = t4_os_alloc(sizeof(struct rte_flow));
448 rte_flow_error_set(e, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
449 NULL, "Unable to allocate memory for"
454 flow->item_parser = parseitem;
457 if (cxgbe_flow_parse(flow, attr, item, action, e)) {
462 /* go, interact with cxgbe_filter */
463 ret = __cxgbe_flow_create(dev, flow);
465 rte_flow_error_set(e, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
466 NULL, "Unable to create flow rule");
471 flow->f->private = flow; /* Will be used during flush */
476 static int __cxgbe_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
478 struct adapter *adap = ethdev2adap(dev);
479 struct filter_entry *f = flow->f;
480 struct ch_filter_specification *fs;
481 struct filter_ctx ctx;
485 if (cxgbe_verify_fidx(flow, flow->fidx, 1))
488 t4_init_completion(&ctx.completion);
489 err = cxgbe_del_filter(dev, flow->fidx, fs, &ctx);
491 dev_err(adap, "Error %d while deleting filter.\n", err);
495 /* Poll the FW for reply */
496 err = cxgbe_poll_for_completion(&adap->sge.fw_evtq,
501 dev_err(adap, "Filter delete operation timed out (%d)\n", err);
505 dev_err(adap, "Hardware error %d while deleting the filter.\n",
514 cxgbe_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
515 struct rte_flow_error *e)
519 ret = __cxgbe_flow_destroy(dev, flow);
521 return rte_flow_error_set(e, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
522 flow, "error destroying filter.");
527 static int __cxgbe_flow_query(struct rte_flow *flow, u64 *count,
530 struct adapter *adap = ethdev2adap(flow->dev);
531 unsigned int fidx = flow->fidx;
534 ret = cxgbe_get_filter_count(adap, fidx, count, 0);
537 return cxgbe_get_filter_count(adap, fidx, byte_count, 1);
541 cxgbe_flow_query(struct rte_eth_dev *dev, struct rte_flow *flow,
542 const struct rte_flow_action *action, void *data,
543 struct rte_flow_error *e)
545 struct ch_filter_specification fs;
546 struct rte_flow_query_count *c;
547 struct filter_entry *f;
555 if (action->type != RTE_FLOW_ACTION_TYPE_COUNT)
556 return rte_flow_error_set(e, ENOTSUP,
557 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
558 "only count supported for query");
561 * This is a valid operation, Since we are allowed to do chelsio
562 * specific operations in rte side of our code but not vise-versa
564 * So, fs can be queried/modified here BUT rte_flow_query_count
565 * cannot be worked on by the lower layer since we want to maintain
566 * it as rte_flow agnostic.
569 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
570 &fs, "filter hit counters were not"
571 " enabled during filter creation");
573 c = (struct rte_flow_query_count *)data;
574 ret = __cxgbe_flow_query(flow, &c->hits, &c->bytes);
576 return rte_flow_error_set(e, -ret, RTE_FLOW_ERROR_TYPE_ACTION,
577 f, "cxgbe pmd failed to"
580 /* Query was successful */
584 return 0; /* success / partial_success */
588 cxgbe_flow_validate(struct rte_eth_dev *dev,
589 const struct rte_flow_attr *attr,
590 const struct rte_flow_item item[],
591 const struct rte_flow_action action[],
592 struct rte_flow_error *e)
594 struct adapter *adap = ethdev2adap(dev);
595 struct rte_flow *flow;
599 flow = t4_os_alloc(sizeof(struct rte_flow));
601 return rte_flow_error_set(e, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
603 "Unable to allocate memory for filter_entry");
605 flow->item_parser = parseitem;
608 ret = cxgbe_flow_parse(flow, attr, item, action, e);
614 if (validate_filter(adap, &flow->fs)) {
616 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE,
618 "validation failed. Check f/w config file.");
621 if (cxgbe_get_fidx(flow, &fidx)) {
623 return rte_flow_error_set(e, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
624 NULL, "no memory in tcam.");
627 if (cxgbe_verify_fidx(flow, fidx, 0)) {
629 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE,
630 NULL, "validation failed");
638 * @ret : > 0 filter destroyed succsesfully
639 * < 0 error destroying filter
640 * == 1 filter not active / not found
643 cxgbe_check_n_destroy(struct filter_entry *f, struct rte_eth_dev *dev,
644 struct rte_flow_error *e)
646 if (f && (f->valid || f->pending) &&
647 f->dev == dev && /* Only if user has asked for this port */
648 f->private) /* We (rte_flow) created this filter */
649 return cxgbe_flow_destroy(dev, (struct rte_flow *)f->private,
654 static int cxgbe_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *e)
656 struct adapter *adap = ethdev2adap(dev);
660 if (adap->tids.ftid_tab) {
661 struct filter_entry *f = &adap->tids.ftid_tab[0];
663 for (i = 0; i < adap->tids.nftids; i++, f++) {
664 ret = cxgbe_check_n_destroy(f, dev, e);
670 return ret >= 0 ? 0 : ret;
673 static const struct rte_flow_ops cxgbe_flow_ops = {
674 .validate = cxgbe_flow_validate,
675 .create = cxgbe_flow_create,
676 .destroy = cxgbe_flow_destroy,
677 .flush = cxgbe_flow_flush,
678 .query = cxgbe_flow_query,
683 cxgbe_dev_filter_ctrl(struct rte_eth_dev *dev,
684 enum rte_filter_type filter_type,
685 enum rte_filter_op filter_op,
691 switch (filter_type) {
692 case RTE_ETH_FILTER_GENERIC:
693 if (filter_op != RTE_ETH_FILTER_GET)
695 *(const void **)arg = &cxgbe_flow_ops;