1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Chelsio Communications.
5 #include "base/common.h"
6 #include "cxgbe_flow.h"
8 #define __CXGBE_FILL_FS(__v, __m, fs, elem, e) \
10 if ((fs)->mask.elem && ((fs)->val.elem != (__v))) \
11 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, \
12 NULL, "Redefined match item with" \
13 " different values found"); \
14 (fs)->val.elem = (__v); \
15 (fs)->mask.elem = (__m); \
18 #define __CXGBE_FILL_FS_MEMCPY(__v, __m, fs, elem) \
20 memcpy(&(fs)->val.elem, &(__v), sizeof(__v)); \
21 memcpy(&(fs)->mask.elem, &(__m), sizeof(__m)); \
24 #define CXGBE_FILL_FS(v, m, elem) \
25 __CXGBE_FILL_FS(v, m, fs, elem, e)
27 #define CXGBE_FILL_FS_MEMCPY(v, m, elem) \
28 __CXGBE_FILL_FS_MEMCPY(v, m, fs, elem)
31 cxgbe_validate_item(const struct rte_flow_item *i, struct rte_flow_error *e)
33 /* rte_flow specification does not allow it. */
34 if (!i->spec && (i->mask || i->last))
35 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
36 i, "last or mask given without spec");
38 * We don't support it.
39 * Although, we can support values in last as 0's or last == spec.
40 * But this will not provide user with any additional functionality
41 * and will only increase the complexity for us.
44 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
45 i, "last is not supported by chelsio pmd");
50 * Apart from the 4-tuple IPv4/IPv6 - TCP/UDP information,
51 * there's only 40-bits available to store match fields.
52 * So, to save space, optimize filter spec for some common
53 * known fields that hardware can parse against incoming
54 * packets automatically.
57 cxgbe_tweak_filter_spec(struct adapter *adap,
58 struct ch_filter_specification *fs)
60 /* Save 16-bit ethertype field space, by setting corresponding
61 * 1-bit flags in the filter spec for common known ethertypes.
62 * When hardware sees these flags, it automatically infers and
63 * matches incoming packets against the corresponding ethertype.
65 if (fs->mask.ethtype == 0xffff) {
66 switch (fs->val.ethtype) {
67 case RTE_ETHER_TYPE_IPV4:
68 if (adap->params.tp.ethertype_shift < 0) {
69 fs->type = FILTER_TYPE_IPV4;
74 case RTE_ETHER_TYPE_IPV6:
75 if (adap->params.tp.ethertype_shift < 0) {
76 fs->type = FILTER_TYPE_IPV6;
81 case RTE_ETHER_TYPE_VLAN:
82 if (adap->params.tp.ethertype_shift < 0 &&
83 adap->params.tp.vlan_shift >= 0) {
84 fs->val.ivlan_vld = 1;
85 fs->mask.ivlan_vld = 1;
90 case RTE_ETHER_TYPE_QINQ:
91 if (adap->params.tp.ethertype_shift < 0 &&
92 adap->params.tp.vnic_shift >= 0) {
93 fs->val.ovlan_vld = 1;
94 fs->mask.ovlan_vld = 1;
106 cxgbe_fill_filter_region(struct adapter *adap,
107 struct ch_filter_specification *fs)
109 struct tp_params *tp = &adap->params.tp;
110 u64 hash_filter_mask = tp->hash_filter_mask;
115 if (!is_hashfilter(adap))
119 uint8_t biton[16] = {0xff, 0xff, 0xff, 0xff,
120 0xff, 0xff, 0xff, 0xff,
121 0xff, 0xff, 0xff, 0xff,
122 0xff, 0xff, 0xff, 0xff};
123 uint8_t bitoff[16] = {0};
125 if (!memcmp(fs->val.lip, bitoff, sizeof(bitoff)) ||
126 !memcmp(fs->val.fip, bitoff, sizeof(bitoff)) ||
127 memcmp(fs->mask.lip, biton, sizeof(biton)) ||
128 memcmp(fs->mask.fip, biton, sizeof(biton)))
131 uint32_t biton = 0xffffffff;
132 uint32_t bitoff = 0x0U;
134 if (!memcmp(fs->val.lip, &bitoff, sizeof(bitoff)) ||
135 !memcmp(fs->val.fip, &bitoff, sizeof(bitoff)) ||
136 memcmp(fs->mask.lip, &biton, sizeof(biton)) ||
137 memcmp(fs->mask.fip, &biton, sizeof(biton)))
141 if (!fs->val.lport || fs->mask.lport != 0xffff)
143 if (!fs->val.fport || fs->mask.fport != 0xffff)
146 if (tp->protocol_shift >= 0)
147 ntuple_mask |= (u64)fs->mask.proto << tp->protocol_shift;
148 if (tp->ethertype_shift >= 0)
149 ntuple_mask |= (u64)fs->mask.ethtype << tp->ethertype_shift;
150 if (tp->port_shift >= 0)
151 ntuple_mask |= (u64)fs->mask.iport << tp->port_shift;
152 if (tp->macmatch_shift >= 0)
153 ntuple_mask |= (u64)fs->mask.macidx << tp->macmatch_shift;
154 if (tp->vlan_shift >= 0 && fs->mask.ivlan_vld)
155 ntuple_mask |= (u64)(F_FT_VLAN_VLD | fs->mask.ivlan) <<
157 if (tp->vnic_shift >= 0) {
158 if (fs->mask.ovlan_vld)
159 ntuple_mask |= (u64)(fs->val.ovlan_vld << 16 |
160 fs->mask.ovlan) << tp->vnic_shift;
161 else if (fs->mask.pfvf_vld)
162 ntuple_mask |= (u64)(fs->mask.pfvf_vld << 16 |
164 fs->mask.vf) << tp->vnic_shift;
166 if (tp->tos_shift >= 0)
167 ntuple_mask |= (u64)fs->mask.tos << tp->tos_shift;
169 if (ntuple_mask != hash_filter_mask)
172 fs->cap = 1; /* use hash region */
176 ch_rte_parsetype_eth(const void *dmask, const struct rte_flow_item *item,
177 struct ch_filter_specification *fs,
178 struct rte_flow_error *e)
180 const struct rte_flow_item_eth *spec = item->spec;
181 const struct rte_flow_item_eth *umask = item->mask;
182 const struct rte_flow_item_eth *mask;
184 /* If user has not given any mask, then use chelsio supported mask. */
185 mask = umask ? umask : (const struct rte_flow_item_eth *)dmask;
190 /* we don't support SRC_MAC filtering*/
191 if (!rte_is_zero_ether_addr(&spec->src) ||
192 (umask && !rte_is_zero_ether_addr(&umask->src)))
193 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
195 "src mac filtering not supported");
197 if (!rte_is_zero_ether_addr(&spec->dst) ||
198 (umask && !rte_is_zero_ether_addr(&umask->dst))) {
199 CXGBE_FILL_FS(0, 0x1ff, macidx);
200 CXGBE_FILL_FS_MEMCPY(spec->dst.addr_bytes, mask->dst.addr_bytes,
204 if (spec->type || (umask && umask->type))
205 CXGBE_FILL_FS(be16_to_cpu(spec->type),
206 be16_to_cpu(mask->type), ethtype);
212 ch_rte_parsetype_port(const void *dmask, const struct rte_flow_item *item,
213 struct ch_filter_specification *fs,
214 struct rte_flow_error *e)
216 const struct rte_flow_item_phy_port *val = item->spec;
217 const struct rte_flow_item_phy_port *umask = item->mask;
218 const struct rte_flow_item_phy_port *mask;
220 mask = umask ? umask : (const struct rte_flow_item_phy_port *)dmask;
223 return 0; /* Wildcard, match all physical ports */
225 if (val->index > 0x7)
226 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
228 "port index up to 0x7 is supported");
230 if (val->index || (umask && umask->index))
231 CXGBE_FILL_FS(val->index, mask->index, iport);
237 ch_rte_parsetype_vlan(const void *dmask, const struct rte_flow_item *item,
238 struct ch_filter_specification *fs,
239 struct rte_flow_error *e)
241 const struct rte_flow_item_vlan *spec = item->spec;
242 const struct rte_flow_item_vlan *umask = item->mask;
243 const struct rte_flow_item_vlan *mask;
245 /* If user has not given any mask, then use chelsio supported mask. */
246 mask = umask ? umask : (const struct rte_flow_item_vlan *)dmask;
248 /* If ethertype is already set and is not VLAN (0x8100) or
249 * QINQ(0x88A8), then don't proceed further. Otherwise,
250 * reset the outer ethertype, so that it can be replaced by
251 * innermost ethertype. Note that hardware will automatically
252 * match against VLAN or QINQ packets, based on 'ivlan_vld' or
253 * 'ovlan_vld' bit set in Chelsio filter spec, respectively.
255 if (fs->mask.ethtype) {
256 if (fs->val.ethtype != RTE_ETHER_TYPE_VLAN &&
257 fs->val.ethtype != RTE_ETHER_TYPE_QINQ)
258 return rte_flow_error_set(e, EINVAL,
259 RTE_FLOW_ERROR_TYPE_ITEM,
261 "Ethertype must be 0x8100 or 0x88a8");
264 if (fs->val.ethtype == RTE_ETHER_TYPE_QINQ) {
265 CXGBE_FILL_FS(1, 1, ovlan_vld);
267 if (spec->tci || (umask && umask->tci))
268 CXGBE_FILL_FS(be16_to_cpu(spec->tci),
269 be16_to_cpu(mask->tci), ovlan);
270 fs->mask.ethtype = 0;
274 CXGBE_FILL_FS(1, 1, ivlan_vld);
276 if (spec->tci || (umask && umask->tci))
277 CXGBE_FILL_FS(be16_to_cpu(spec->tci),
278 be16_to_cpu(mask->tci), ivlan);
279 fs->mask.ethtype = 0;
284 if (spec && (spec->inner_type || (umask && umask->inner_type)))
285 CXGBE_FILL_FS(be16_to_cpu(spec->inner_type),
286 be16_to_cpu(mask->inner_type), ethtype);
292 ch_rte_parsetype_pf(const void *dmask __rte_unused,
293 const struct rte_flow_item *item __rte_unused,
294 struct ch_filter_specification *fs,
295 struct rte_flow_error *e __rte_unused)
297 struct rte_flow *flow = (struct rte_flow *)fs->private;
298 struct rte_eth_dev *dev = flow->dev;
299 struct adapter *adap = ethdev2adap(dev);
301 CXGBE_FILL_FS(1, 1, pfvf_vld);
303 CXGBE_FILL_FS(adap->pf, 0x7, pf);
308 ch_rte_parsetype_vf(const void *dmask, const struct rte_flow_item *item,
309 struct ch_filter_specification *fs,
310 struct rte_flow_error *e)
312 const struct rte_flow_item_vf *umask = item->mask;
313 const struct rte_flow_item_vf *val = item->spec;
314 const struct rte_flow_item_vf *mask;
316 /* If user has not given any mask, then use chelsio supported mask. */
317 mask = umask ? umask : (const struct rte_flow_item_vf *)dmask;
319 CXGBE_FILL_FS(1, 1, pfvf_vld);
322 return 0; /* Wildcard, match all Vf */
324 if (val->id > UCHAR_MAX)
325 return rte_flow_error_set(e, EINVAL,
326 RTE_FLOW_ERROR_TYPE_ITEM,
330 if (val->id || (umask && umask->id))
331 CXGBE_FILL_FS(val->id, mask->id, vf);
337 ch_rte_parsetype_udp(const void *dmask, const struct rte_flow_item *item,
338 struct ch_filter_specification *fs,
339 struct rte_flow_error *e)
341 const struct rte_flow_item_udp *val = item->spec;
342 const struct rte_flow_item_udp *umask = item->mask;
343 const struct rte_flow_item_udp *mask;
345 mask = umask ? umask : (const struct rte_flow_item_udp *)dmask;
347 if (mask->hdr.dgram_len || mask->hdr.dgram_cksum)
348 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
350 "udp: only src/dst port supported");
352 CXGBE_FILL_FS(IPPROTO_UDP, 0xff, proto);
356 if (val->hdr.src_port || (umask && umask->hdr.src_port))
357 CXGBE_FILL_FS(be16_to_cpu(val->hdr.src_port),
358 be16_to_cpu(mask->hdr.src_port), fport);
360 if (val->hdr.dst_port || (umask && umask->hdr.dst_port))
361 CXGBE_FILL_FS(be16_to_cpu(val->hdr.dst_port),
362 be16_to_cpu(mask->hdr.dst_port), lport);
368 ch_rte_parsetype_tcp(const void *dmask, const struct rte_flow_item *item,
369 struct ch_filter_specification *fs,
370 struct rte_flow_error *e)
372 const struct rte_flow_item_tcp *val = item->spec;
373 const struct rte_flow_item_tcp *umask = item->mask;
374 const struct rte_flow_item_tcp *mask;
376 mask = umask ? umask : (const struct rte_flow_item_tcp *)dmask;
378 if (mask->hdr.sent_seq || mask->hdr.recv_ack || mask->hdr.data_off ||
379 mask->hdr.tcp_flags || mask->hdr.rx_win || mask->hdr.cksum ||
381 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
383 "tcp: only src/dst port supported");
385 CXGBE_FILL_FS(IPPROTO_TCP, 0xff, proto);
389 if (val->hdr.src_port || (umask && umask->hdr.src_port))
390 CXGBE_FILL_FS(be16_to_cpu(val->hdr.src_port),
391 be16_to_cpu(mask->hdr.src_port), fport);
393 if (val->hdr.dst_port || (umask && umask->hdr.dst_port))
394 CXGBE_FILL_FS(be16_to_cpu(val->hdr.dst_port),
395 be16_to_cpu(mask->hdr.dst_port), lport);
401 ch_rte_parsetype_ipv4(const void *dmask, const struct rte_flow_item *item,
402 struct ch_filter_specification *fs,
403 struct rte_flow_error *e)
405 const struct rte_flow_item_ipv4 *val = item->spec;
406 const struct rte_flow_item_ipv4 *umask = item->mask;
407 const struct rte_flow_item_ipv4 *mask;
409 mask = umask ? umask : (const struct rte_flow_item_ipv4 *)dmask;
411 if (mask->hdr.time_to_live)
412 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
413 item, "ttl is not supported");
415 if (fs->mask.ethtype &&
416 (fs->val.ethtype != RTE_ETHER_TYPE_IPV4))
417 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
419 "Couldn't find IPv4 ethertype");
420 fs->type = FILTER_TYPE_IPV4;
422 return 0; /* ipv4 wild card */
424 if (val->hdr.next_proto_id || (umask && umask->hdr.next_proto_id))
425 CXGBE_FILL_FS(val->hdr.next_proto_id, mask->hdr.next_proto_id,
428 if (val->hdr.dst_addr || (umask && umask->hdr.dst_addr))
429 CXGBE_FILL_FS_MEMCPY(val->hdr.dst_addr, mask->hdr.dst_addr,
432 if (val->hdr.src_addr || (umask && umask->hdr.src_addr))
433 CXGBE_FILL_FS_MEMCPY(val->hdr.src_addr, mask->hdr.src_addr,
436 if (val->hdr.type_of_service || (umask && umask->hdr.type_of_service))
437 CXGBE_FILL_FS(val->hdr.type_of_service,
438 mask->hdr.type_of_service, tos);
444 ch_rte_parsetype_ipv6(const void *dmask, const struct rte_flow_item *item,
445 struct ch_filter_specification *fs,
446 struct rte_flow_error *e)
448 const struct rte_flow_item_ipv6 *val = item->spec;
449 const struct rte_flow_item_ipv6 *umask = item->mask;
450 const struct rte_flow_item_ipv6 *mask;
451 u32 vtc_flow, vtc_flow_mask;
454 mask = umask ? umask : (const struct rte_flow_item_ipv6 *)dmask;
456 vtc_flow_mask = be32_to_cpu(mask->hdr.vtc_flow);
458 if (vtc_flow_mask & RTE_IPV6_HDR_FL_MASK ||
459 mask->hdr.payload_len || mask->hdr.hop_limits)
460 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
462 "flow/hop are not supported");
464 if (fs->mask.ethtype &&
465 (fs->val.ethtype != RTE_ETHER_TYPE_IPV6))
466 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
468 "Couldn't find IPv6 ethertype");
469 fs->type = FILTER_TYPE_IPV6;
471 return 0; /* ipv6 wild card */
473 if (val->hdr.proto || (umask && umask->hdr.proto))
474 CXGBE_FILL_FS(val->hdr.proto, mask->hdr.proto, proto);
476 vtc_flow = be32_to_cpu(val->hdr.vtc_flow);
477 if (val->hdr.vtc_flow || (umask && umask->hdr.vtc_flow))
478 CXGBE_FILL_FS((vtc_flow & RTE_IPV6_HDR_TC_MASK) >>
479 RTE_IPV6_HDR_TC_SHIFT,
480 (vtc_flow_mask & RTE_IPV6_HDR_TC_MASK) >>
481 RTE_IPV6_HDR_TC_SHIFT,
484 if (memcmp(val->hdr.dst_addr, z, sizeof(val->hdr.dst_addr)) ||
486 memcmp(umask->hdr.dst_addr, z, sizeof(umask->hdr.dst_addr))))
487 CXGBE_FILL_FS_MEMCPY(val->hdr.dst_addr, mask->hdr.dst_addr,
490 if (memcmp(val->hdr.src_addr, z, sizeof(val->hdr.src_addr)) ||
492 memcmp(umask->hdr.src_addr, z, sizeof(umask->hdr.src_addr))))
493 CXGBE_FILL_FS_MEMCPY(val->hdr.src_addr, mask->hdr.src_addr,
500 cxgbe_rtef_parse_attr(struct rte_flow *flow, const struct rte_flow_attr *attr,
501 struct rte_flow_error *e)
504 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR,
505 attr, "attribute:<egress> is"
508 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR,
509 attr, "group parameter is"
512 flow->fidx = attr->priority ? attr->priority - 1 : FILTER_ID_MAX;
517 static inline int check_rxq(struct rte_eth_dev *dev, uint16_t rxq)
519 struct port_info *pi = ethdev2pinfo(dev);
521 if (rxq > pi->n_rx_qsets)
526 static int cxgbe_validate_fidxondel(struct filter_entry *f, unsigned int fidx)
528 struct adapter *adap = ethdev2adap(f->dev);
529 struct ch_filter_specification fs = f->fs;
532 if (fidx >= adap->tids.nftids) {
533 dev_err(adap, "invalid flow index %d.\n", fidx);
537 nentries = cxgbe_filter_slots(adap, fs.type);
538 if (!cxgbe_is_filter_set(&adap->tids, fidx, nentries)) {
539 dev_err(adap, "Already free fidx:%d f:%p\n", fidx, f);
547 cxgbe_validate_fidxonadd(struct ch_filter_specification *fs,
548 struct adapter *adap, unsigned int fidx)
552 nentries = cxgbe_filter_slots(adap, fs->type);
553 if (cxgbe_is_filter_set(&adap->tids, fidx, nentries)) {
554 dev_err(adap, "filter index: %d is busy.\n", fidx);
558 if (fidx >= adap->tids.nftids) {
559 dev_err(adap, "filter index (%u) >= max(%u)\n",
560 fidx, adap->tids.nftids);
568 cxgbe_verify_fidx(struct rte_flow *flow, unsigned int fidx, uint8_t del)
571 return 0; /* Hash filters */
572 return del ? cxgbe_validate_fidxondel(flow->f, fidx) :
573 cxgbe_validate_fidxonadd(&flow->fs,
574 ethdev2adap(flow->dev), fidx);
577 static int cxgbe_get_fidx(struct rte_flow *flow, unsigned int *fidx)
579 struct ch_filter_specification *fs = &flow->fs;
580 struct adapter *adap = ethdev2adap(flow->dev);
582 /* For tcam get the next available slot, if default value specified */
583 if (flow->fidx == FILTER_ID_MAX) {
587 nentries = cxgbe_filter_slots(adap, fs->type);
588 idx = cxgbe_alloc_ftid(adap, nentries);
590 dev_err(adap, "unable to get a filter index in tcam\n");
593 *fidx = (unsigned int)idx;
602 cxgbe_get_flow_item_index(const struct rte_flow_item items[], u32 type)
604 const struct rte_flow_item *i;
605 int j, index = -ENOENT;
607 for (i = items, j = 0; i->type != RTE_FLOW_ITEM_TYPE_END; i++, j++) {
608 if (i->type == type) {
618 ch_rte_parse_nat(uint8_t nmode, struct ch_filter_specification *fs)
621 * BIT_0 = [src_ip], BIT_1 = [dst_ip]
622 * BIT_2 = [src_port], BIT_3 = [dst_port]
624 * Only below cases are supported as per our spec.
628 fs->nat_mode = NAT_MODE_NONE;
631 fs->nat_mode = NAT_MODE_DIP;
634 fs->nat_mode = NAT_MODE_SIP_SP;
637 fs->nat_mode = NAT_MODE_DIP_SIP_SP;
640 fs->nat_mode = NAT_MODE_DIP_DP;
643 fs->nat_mode = NAT_MODE_DIP_DP_SIP;
646 fs->nat_mode = NAT_MODE_DIP_DP_SP;
649 fs->nat_mode = NAT_MODE_ALL;
659 ch_rte_parse_atype_switch(const struct rte_flow_action *a,
660 const struct rte_flow_item items[],
662 struct ch_filter_specification *fs,
663 struct rte_flow_error *e)
665 const struct rte_flow_action_of_set_vlan_vid *vlanid;
666 const struct rte_flow_action_of_set_vlan_pcp *vlanpcp;
667 const struct rte_flow_action_of_push_vlan *pushvlan;
668 const struct rte_flow_action_set_ipv4 *ipv4;
669 const struct rte_flow_action_set_ipv6 *ipv6;
670 const struct rte_flow_action_set_tp *tp_port;
671 const struct rte_flow_action_phy_port *port;
672 const struct rte_flow_action_set_mac *mac;
677 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
678 vlanid = (const struct rte_flow_action_of_set_vlan_vid *)
680 /* If explicitly asked to push a new VLAN header,
681 * then don't set rewrite mode. Otherwise, the
682 * incoming VLAN packets will get their VLAN fields
683 * rewritten, instead of adding an additional outer
686 if (fs->newvlan != VLAN_INSERT)
687 fs->newvlan = VLAN_REWRITE;
688 tmp_vlan = fs->vlan & 0xe000;
689 fs->vlan = (be16_to_cpu(vlanid->vlan_vid) & 0xfff) | tmp_vlan;
691 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
692 vlanpcp = (const struct rte_flow_action_of_set_vlan_pcp *)
694 /* If explicitly asked to push a new VLAN header,
695 * then don't set rewrite mode. Otherwise, the
696 * incoming VLAN packets will get their VLAN fields
697 * rewritten, instead of adding an additional outer
700 if (fs->newvlan != VLAN_INSERT)
701 fs->newvlan = VLAN_REWRITE;
702 tmp_vlan = fs->vlan & 0xfff;
703 fs->vlan = (vlanpcp->vlan_pcp << 13) | tmp_vlan;
705 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
706 pushvlan = (const struct rte_flow_action_of_push_vlan *)
708 if (be16_to_cpu(pushvlan->ethertype) != RTE_ETHER_TYPE_VLAN)
709 return rte_flow_error_set(e, EINVAL,
710 RTE_FLOW_ERROR_TYPE_ACTION, a,
711 "only ethertype 0x8100 "
712 "supported for push vlan.");
713 fs->newvlan = VLAN_INSERT;
715 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
716 fs->newvlan = VLAN_REMOVE;
718 case RTE_FLOW_ACTION_TYPE_PHY_PORT:
719 port = (const struct rte_flow_action_phy_port *)a->conf;
720 fs->eport = port->index;
722 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
723 item_index = cxgbe_get_flow_item_index(items,
724 RTE_FLOW_ITEM_TYPE_IPV4);
726 return rte_flow_error_set(e, EINVAL,
727 RTE_FLOW_ERROR_TYPE_ACTION, a,
728 "No RTE_FLOW_ITEM_TYPE_IPV4 "
731 ipv4 = (const struct rte_flow_action_set_ipv4 *)a->conf;
732 memcpy(fs->nat_fip, &ipv4->ipv4_addr, sizeof(ipv4->ipv4_addr));
735 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
736 item_index = cxgbe_get_flow_item_index(items,
737 RTE_FLOW_ITEM_TYPE_IPV4);
739 return rte_flow_error_set(e, EINVAL,
740 RTE_FLOW_ERROR_TYPE_ACTION, a,
741 "No RTE_FLOW_ITEM_TYPE_IPV4 "
744 ipv4 = (const struct rte_flow_action_set_ipv4 *)a->conf;
745 memcpy(fs->nat_lip, &ipv4->ipv4_addr, sizeof(ipv4->ipv4_addr));
748 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
749 item_index = cxgbe_get_flow_item_index(items,
750 RTE_FLOW_ITEM_TYPE_IPV6);
752 return rte_flow_error_set(e, EINVAL,
753 RTE_FLOW_ERROR_TYPE_ACTION, a,
754 "No RTE_FLOW_ITEM_TYPE_IPV6 "
757 ipv6 = (const struct rte_flow_action_set_ipv6 *)a->conf;
758 memcpy(fs->nat_fip, ipv6->ipv6_addr, sizeof(ipv6->ipv6_addr));
761 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
762 item_index = cxgbe_get_flow_item_index(items,
763 RTE_FLOW_ITEM_TYPE_IPV6);
765 return rte_flow_error_set(e, EINVAL,
766 RTE_FLOW_ERROR_TYPE_ACTION, a,
767 "No RTE_FLOW_ITEM_TYPE_IPV6 "
770 ipv6 = (const struct rte_flow_action_set_ipv6 *)a->conf;
771 memcpy(fs->nat_lip, ipv6->ipv6_addr, sizeof(ipv6->ipv6_addr));
774 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
775 item_index = cxgbe_get_flow_item_index(items,
776 RTE_FLOW_ITEM_TYPE_TCP);
777 if (item_index < 0) {
779 cxgbe_get_flow_item_index(items,
780 RTE_FLOW_ITEM_TYPE_UDP);
782 return rte_flow_error_set(e, EINVAL,
783 RTE_FLOW_ERROR_TYPE_ACTION, a,
784 "No RTE_FLOW_ITEM_TYPE_TCP or "
785 "RTE_FLOW_ITEM_TYPE_UDP found");
788 tp_port = (const struct rte_flow_action_set_tp *)a->conf;
789 fs->nat_fport = be16_to_cpu(tp_port->port);
792 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
793 item_index = cxgbe_get_flow_item_index(items,
794 RTE_FLOW_ITEM_TYPE_TCP);
795 if (item_index < 0) {
797 cxgbe_get_flow_item_index(items,
798 RTE_FLOW_ITEM_TYPE_UDP);
800 return rte_flow_error_set(e, EINVAL,
801 RTE_FLOW_ERROR_TYPE_ACTION, a,
802 "No RTE_FLOW_ITEM_TYPE_TCP or "
803 "RTE_FLOW_ITEM_TYPE_UDP found");
806 tp_port = (const struct rte_flow_action_set_tp *)a->conf;
807 fs->nat_lport = be16_to_cpu(tp_port->port);
810 case RTE_FLOW_ACTION_TYPE_MAC_SWAP:
811 item_index = cxgbe_get_flow_item_index(items,
812 RTE_FLOW_ITEM_TYPE_ETH);
814 return rte_flow_error_set(e, EINVAL,
815 RTE_FLOW_ERROR_TYPE_ACTION, a,
816 "No RTE_FLOW_ITEM_TYPE_ETH "
820 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
821 item_index = cxgbe_get_flow_item_index(items,
822 RTE_FLOW_ITEM_TYPE_ETH);
824 return rte_flow_error_set(e, EINVAL,
825 RTE_FLOW_ERROR_TYPE_ACTION, a,
826 "No RTE_FLOW_ITEM_TYPE_ETH "
828 mac = (const struct rte_flow_action_set_mac *)a->conf;
831 memcpy(fs->smac, mac->mac_addr, sizeof(fs->smac));
833 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
834 item_index = cxgbe_get_flow_item_index(items,
835 RTE_FLOW_ITEM_TYPE_ETH);
837 return rte_flow_error_set(e, EINVAL,
838 RTE_FLOW_ERROR_TYPE_ACTION, a,
839 "No RTE_FLOW_ITEM_TYPE_ETH found");
840 mac = (const struct rte_flow_action_set_mac *)a->conf;
843 memcpy(fs->dmac, mac->mac_addr, sizeof(fs->dmac));
846 /* We are not supposed to come here */
847 return rte_flow_error_set(e, EINVAL,
848 RTE_FLOW_ERROR_TYPE_ACTION, a,
849 "Action not supported");
856 cxgbe_rtef_parse_actions(struct rte_flow *flow,
857 const struct rte_flow_item items[],
858 const struct rte_flow_action action[],
859 struct rte_flow_error *e)
861 struct ch_filter_specification *fs = &flow->fs;
862 uint8_t nmode = 0, nat_ipv4 = 0, nat_ipv6 = 0;
863 uint8_t vlan_set_vid = 0, vlan_set_pcp = 0;
864 const struct rte_flow_action_queue *q;
865 const struct rte_flow_action *a;
869 for (a = action; a->type != RTE_FLOW_ACTION_TYPE_END; a++) {
871 case RTE_FLOW_ACTION_TYPE_VOID:
873 case RTE_FLOW_ACTION_TYPE_DROP:
875 return rte_flow_error_set(e, EINVAL,
876 RTE_FLOW_ERROR_TYPE_ACTION, a,
877 "specify only 1 pass/drop");
878 fs->action = FILTER_DROP;
880 case RTE_FLOW_ACTION_TYPE_QUEUE:
881 q = (const struct rte_flow_action_queue *)a->conf;
883 return rte_flow_error_set(e, EINVAL,
884 RTE_FLOW_ERROR_TYPE_ACTION, q,
885 "specify rx queue index");
886 if (check_rxq(flow->dev, q->index))
887 return rte_flow_error_set(e, EINVAL,
888 RTE_FLOW_ERROR_TYPE_ACTION, q,
891 return rte_flow_error_set(e, EINVAL,
892 RTE_FLOW_ERROR_TYPE_ACTION, a,
893 "specify only 1 pass/drop");
894 fs->action = FILTER_PASS;
898 case RTE_FLOW_ACTION_TYPE_COUNT:
901 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
904 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
907 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
908 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
909 case RTE_FLOW_ACTION_TYPE_PHY_PORT:
910 case RTE_FLOW_ACTION_TYPE_MAC_SWAP:
911 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
912 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
915 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
916 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
919 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
920 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
921 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
922 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
924 /* We allow multiple switch actions, but switch is
925 * not compatible with either queue or drop
927 if (abit++ && fs->action != FILTER_SWITCH)
928 return rte_flow_error_set(e, EINVAL,
929 RTE_FLOW_ERROR_TYPE_ACTION, a,
930 "overlapping action specified");
931 if (nat_ipv4 && nat_ipv6)
932 return rte_flow_error_set(e, EINVAL,
933 RTE_FLOW_ERROR_TYPE_ACTION, a,
934 "Can't have one address ipv4 and the"
937 ret = ch_rte_parse_atype_switch(a, items, &nmode, fs,
941 fs->action = FILTER_SWITCH;
944 /* Not supported action : return error */
945 return rte_flow_error_set(e, ENOTSUP,
946 RTE_FLOW_ERROR_TYPE_ACTION,
947 a, "Action not supported");
951 if (fs->newvlan == VLAN_REWRITE && (!vlan_set_vid || !vlan_set_pcp))
952 return rte_flow_error_set(e, EINVAL,
953 RTE_FLOW_ERROR_TYPE_ACTION, a,
954 "Both OF_SET_VLAN_VID and "
955 "OF_SET_VLAN_PCP must be specified");
957 if (ch_rte_parse_nat(nmode, fs))
958 return rte_flow_error_set(e, EINVAL,
959 RTE_FLOW_ERROR_TYPE_ACTION, a,
960 "invalid settings for swich action");
964 static struct chrte_fparse parseitem[] = {
965 [RTE_FLOW_ITEM_TYPE_ETH] = {
966 .fptr = ch_rte_parsetype_eth,
967 .dmask = &(const struct rte_flow_item_eth){
968 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
969 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
974 [RTE_FLOW_ITEM_TYPE_PHY_PORT] = {
975 .fptr = ch_rte_parsetype_port,
976 .dmask = &(const struct rte_flow_item_phy_port){
981 [RTE_FLOW_ITEM_TYPE_VLAN] = {
982 .fptr = ch_rte_parsetype_vlan,
983 .dmask = &(const struct rte_flow_item_vlan){
985 .inner_type = 0xffff,
989 [RTE_FLOW_ITEM_TYPE_IPV4] = {
990 .fptr = ch_rte_parsetype_ipv4,
991 .dmask = &(const struct rte_flow_item_ipv4) {
993 .src_addr = RTE_BE32(0xffffffff),
994 .dst_addr = RTE_BE32(0xffffffff),
995 .type_of_service = 0xff,
1000 [RTE_FLOW_ITEM_TYPE_IPV6] = {
1001 .fptr = ch_rte_parsetype_ipv6,
1002 .dmask = &(const struct rte_flow_item_ipv6) {
1005 "\xff\xff\xff\xff\xff\xff\xff\xff"
1006 "\xff\xff\xff\xff\xff\xff\xff\xff",
1008 "\xff\xff\xff\xff\xff\xff\xff\xff"
1009 "\xff\xff\xff\xff\xff\xff\xff\xff",
1010 .vtc_flow = RTE_BE32(0xff000000),
1015 [RTE_FLOW_ITEM_TYPE_UDP] = {
1016 .fptr = ch_rte_parsetype_udp,
1017 .dmask = &rte_flow_item_udp_mask,
1020 [RTE_FLOW_ITEM_TYPE_TCP] = {
1021 .fptr = ch_rte_parsetype_tcp,
1022 .dmask = &rte_flow_item_tcp_mask,
1025 [RTE_FLOW_ITEM_TYPE_PF] = {
1026 .fptr = ch_rte_parsetype_pf,
1030 [RTE_FLOW_ITEM_TYPE_VF] = {
1031 .fptr = ch_rte_parsetype_vf,
1032 .dmask = &(const struct rte_flow_item_vf){
1039 cxgbe_rtef_parse_items(struct rte_flow *flow,
1040 const struct rte_flow_item items[],
1041 struct rte_flow_error *e)
1043 struct adapter *adap = ethdev2adap(flow->dev);
1044 const struct rte_flow_item *i;
1045 char repeat[ARRAY_SIZE(parseitem)] = {0};
1047 for (i = items; i->type != RTE_FLOW_ITEM_TYPE_END; i++) {
1048 struct chrte_fparse *idx;
1051 if (i->type >= ARRAY_SIZE(parseitem))
1052 return rte_flow_error_set(e, ENOTSUP,
1053 RTE_FLOW_ERROR_TYPE_ITEM,
1054 i, "Item not supported");
1057 case RTE_FLOW_ITEM_TYPE_VOID:
1060 /* check if item is repeated */
1061 if (repeat[i->type] &&
1062 i->type != RTE_FLOW_ITEM_TYPE_VLAN)
1063 return rte_flow_error_set(e, ENOTSUP,
1064 RTE_FLOW_ERROR_TYPE_ITEM, i,
1065 "parse items cannot be repeated(except void/vlan)");
1067 repeat[i->type] = 1;
1069 /* validate the item */
1070 ret = cxgbe_validate_item(i, e);
1074 idx = &flow->item_parser[i->type];
1075 if (!idx || !idx->fptr) {
1076 return rte_flow_error_set(e, ENOTSUP,
1077 RTE_FLOW_ERROR_TYPE_ITEM, i,
1078 "Item not supported");
1080 ret = idx->fptr(idx->dmask, i, &flow->fs, e);
1087 cxgbe_tweak_filter_spec(adap, &flow->fs);
1088 cxgbe_fill_filter_region(adap, &flow->fs);
1094 cxgbe_flow_parse(struct rte_flow *flow,
1095 const struct rte_flow_attr *attr,
1096 const struct rte_flow_item item[],
1097 const struct rte_flow_action action[],
1098 struct rte_flow_error *e)
1101 /* parse user request into ch_filter_specification */
1102 ret = cxgbe_rtef_parse_attr(flow, attr, e);
1105 ret = cxgbe_rtef_parse_items(flow, item, e);
1108 return cxgbe_rtef_parse_actions(flow, item, action, e);
1111 static int __cxgbe_flow_create(struct rte_eth_dev *dev, struct rte_flow *flow)
1113 struct ch_filter_specification *fs = &flow->fs;
1114 struct adapter *adap = ethdev2adap(dev);
1115 struct tid_info *t = &adap->tids;
1116 struct filter_ctx ctx;
1120 if (cxgbe_get_fidx(flow, &fidx))
1122 if (cxgbe_verify_fidx(flow, fidx, 0))
1125 t4_init_completion(&ctx.completion);
1126 /* go create the filter */
1127 err = cxgbe_set_filter(dev, fidx, fs, &ctx);
1129 dev_err(adap, "Error %d while creating filter.\n", err);
1133 /* Poll the FW for reply */
1134 err = cxgbe_poll_for_completion(&adap->sge.fw_evtq,
1136 CXGBE_FLOW_POLL_CNT,
1139 dev_err(adap, "Filter set operation timed out (%d)\n", err);
1143 dev_err(adap, "Hardware error %d while creating the filter.\n",
1148 if (fs->cap) { /* to destroy the filter */
1149 flow->fidx = ctx.tid;
1150 flow->f = lookup_tid(t, ctx.tid);
1153 flow->f = &adap->tids.ftid_tab[fidx];
1159 static struct rte_flow *
1160 cxgbe_flow_create(struct rte_eth_dev *dev,
1161 const struct rte_flow_attr *attr,
1162 const struct rte_flow_item item[],
1163 const struct rte_flow_action action[],
1164 struct rte_flow_error *e)
1166 struct adapter *adap = ethdev2adap(dev);
1167 struct rte_flow *flow;
1170 flow = t4_os_alloc(sizeof(struct rte_flow));
1172 rte_flow_error_set(e, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1173 NULL, "Unable to allocate memory for"
1178 flow->item_parser = parseitem;
1180 flow->fs.private = (void *)flow;
1182 if (cxgbe_flow_parse(flow, attr, item, action, e)) {
1187 t4_os_lock(&adap->flow_lock);
1188 /* go, interact with cxgbe_filter */
1189 ret = __cxgbe_flow_create(dev, flow);
1190 t4_os_unlock(&adap->flow_lock);
1192 rte_flow_error_set(e, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
1193 NULL, "Unable to create flow rule");
1198 flow->f->private = flow; /* Will be used during flush */
1203 static int __cxgbe_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
1205 struct adapter *adap = ethdev2adap(dev);
1206 struct filter_entry *f = flow->f;
1207 struct ch_filter_specification *fs;
1208 struct filter_ctx ctx;
1212 if (cxgbe_verify_fidx(flow, flow->fidx, 1))
1215 t4_init_completion(&ctx.completion);
1216 err = cxgbe_del_filter(dev, flow->fidx, fs, &ctx);
1218 dev_err(adap, "Error %d while deleting filter.\n", err);
1222 /* Poll the FW for reply */
1223 err = cxgbe_poll_for_completion(&adap->sge.fw_evtq,
1225 CXGBE_FLOW_POLL_CNT,
1228 dev_err(adap, "Filter delete operation timed out (%d)\n", err);
1232 dev_err(adap, "Hardware error %d while deleting the filter.\n",
1241 cxgbe_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
1242 struct rte_flow_error *e)
1244 struct adapter *adap = ethdev2adap(dev);
1247 t4_os_lock(&adap->flow_lock);
1248 ret = __cxgbe_flow_destroy(dev, flow);
1249 t4_os_unlock(&adap->flow_lock);
1251 return rte_flow_error_set(e, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
1252 flow, "error destroying filter.");
1257 static int __cxgbe_flow_query(struct rte_flow *flow, u64 *count,
1260 struct adapter *adap = ethdev2adap(flow->dev);
1261 struct ch_filter_specification fs = flow->f->fs;
1262 unsigned int fidx = flow->fidx;
1265 ret = cxgbe_get_filter_count(adap, fidx, count, fs.cap, 0);
1268 return cxgbe_get_filter_count(adap, fidx, byte_count, fs.cap, 1);
1272 cxgbe_flow_query(struct rte_eth_dev *dev, struct rte_flow *flow,
1273 const struct rte_flow_action *action, void *data,
1274 struct rte_flow_error *e)
1276 struct adapter *adap = ethdev2adap(flow->dev);
1277 struct ch_filter_specification fs;
1278 struct rte_flow_query_count *c;
1279 struct filter_entry *f;
1287 if (action->type != RTE_FLOW_ACTION_TYPE_COUNT)
1288 return rte_flow_error_set(e, ENOTSUP,
1289 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1290 "only count supported for query");
1293 * This is a valid operation, Since we are allowed to do chelsio
1294 * specific operations in rte side of our code but not vise-versa
1296 * So, fs can be queried/modified here BUT rte_flow_query_count
1297 * cannot be worked on by the lower layer since we want to maintain
1298 * it as rte_flow agnostic.
1301 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
1302 &fs, "filter hit counters were not"
1303 " enabled during filter creation");
1305 c = (struct rte_flow_query_count *)data;
1307 t4_os_lock(&adap->flow_lock);
1308 ret = __cxgbe_flow_query(flow, &c->hits, &c->bytes);
1310 rte_flow_error_set(e, -ret, RTE_FLOW_ERROR_TYPE_ACTION,
1311 f, "cxgbe pmd failed to perform query");
1315 /* Query was successful */
1319 cxgbe_clear_filter_count(adap, flow->fidx, f->fs.cap, true);
1322 t4_os_unlock(&adap->flow_lock);
1327 cxgbe_flow_validate(struct rte_eth_dev *dev,
1328 const struct rte_flow_attr *attr,
1329 const struct rte_flow_item item[],
1330 const struct rte_flow_action action[],
1331 struct rte_flow_error *e)
1333 struct adapter *adap = ethdev2adap(dev);
1334 struct rte_flow *flow;
1338 flow = t4_os_alloc(sizeof(struct rte_flow));
1340 return rte_flow_error_set(e, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1342 "Unable to allocate memory for filter_entry");
1344 flow->item_parser = parseitem;
1346 flow->fs.private = (void *)flow;
1348 ret = cxgbe_flow_parse(flow, attr, item, action, e);
1354 if (cxgbe_validate_filter(adap, &flow->fs)) {
1356 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE,
1358 "validation failed. Check f/w config file.");
1361 t4_os_lock(&adap->flow_lock);
1362 if (cxgbe_get_fidx(flow, &fidx)) {
1363 ret = rte_flow_error_set(e, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1364 NULL, "no memory in tcam.");
1368 if (cxgbe_verify_fidx(flow, fidx, 0)) {
1369 ret = rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE,
1370 NULL, "validation failed");
1375 t4_os_unlock(&adap->flow_lock);
1381 * @ret : > 0 filter destroyed succsesfully
1382 * < 0 error destroying filter
1383 * == 1 filter not active / not found
1386 cxgbe_check_n_destroy(struct filter_entry *f, struct rte_eth_dev *dev)
1388 if (f && (f->valid || f->pending) &&
1389 f->dev == dev && /* Only if user has asked for this port */
1390 f->private) /* We (rte_flow) created this filter */
1391 return __cxgbe_flow_destroy(dev, (struct rte_flow *)f->private);
1395 static int cxgbe_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *e)
1397 struct adapter *adap = ethdev2adap(dev);
1401 t4_os_lock(&adap->flow_lock);
1402 if (adap->tids.ftid_tab) {
1403 struct filter_entry *f = &adap->tids.ftid_tab[0];
1405 for (i = 0; i < adap->tids.nftids; i++, f++) {
1406 ret = cxgbe_check_n_destroy(f, dev);
1408 rte_flow_error_set(e, ret,
1409 RTE_FLOW_ERROR_TYPE_HANDLE,
1411 "error destroying TCAM "
1418 if (is_hashfilter(adap) && adap->tids.tid_tab) {
1419 struct filter_entry *f;
1421 for (i = adap->tids.hash_base; i <= adap->tids.ntids; i++) {
1422 f = (struct filter_entry *)adap->tids.tid_tab[i];
1424 ret = cxgbe_check_n_destroy(f, dev);
1426 rte_flow_error_set(e, ret,
1427 RTE_FLOW_ERROR_TYPE_HANDLE,
1429 "error destroying HASH "
1437 t4_os_unlock(&adap->flow_lock);
1438 return ret >= 0 ? 0 : ret;
1441 static const struct rte_flow_ops cxgbe_flow_ops = {
1442 .validate = cxgbe_flow_validate,
1443 .create = cxgbe_flow_create,
1444 .destroy = cxgbe_flow_destroy,
1445 .flush = cxgbe_flow_flush,
1446 .query = cxgbe_flow_query,
1451 cxgbe_dev_flow_ops_get(struct rte_eth_dev *dev __rte_unused,
1452 const struct rte_flow_ops **ops)
1454 *ops = &cxgbe_flow_ops;