1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#include <net/tc_act/tc_mirred.h>
36#include <net/tc_act/tc_pedit.h>
37#include <net/tc_act/tc_gact.h>
38#include <net/tc_act/tc_vlan.h>
39
40#include "cxgb4.h"
41#include "cxgb4_filter.h"
42#include "cxgb4_tc_flower.h"
43
44#define STATS_CHECK_PERIOD (HZ / 2)
45
46static struct ch_tc_pedit_fields pedits[] = {
47 PEDIT_FIELDS(ETH_, DMAC_31_0, 4, dmac, 0),
48 PEDIT_FIELDS(ETH_, DMAC_47_32, 2, dmac, 4),
49 PEDIT_FIELDS(ETH_, SMAC_15_0, 2, smac, 0),
50 PEDIT_FIELDS(ETH_, SMAC_47_16, 4, smac, 2),
51 PEDIT_FIELDS(IP4_, SRC, 4, nat_fip, 0),
52 PEDIT_FIELDS(IP4_, DST, 4, nat_lip, 0),
53 PEDIT_FIELDS(IP6_, SRC_31_0, 4, nat_fip, 0),
54 PEDIT_FIELDS(IP6_, SRC_63_32, 4, nat_fip, 4),
55 PEDIT_FIELDS(IP6_, SRC_95_64, 4, nat_fip, 8),
56 PEDIT_FIELDS(IP6_, SRC_127_96, 4, nat_fip, 12),
57 PEDIT_FIELDS(IP6_, DST_31_0, 4, nat_lip, 0),
58 PEDIT_FIELDS(IP6_, DST_63_32, 4, nat_lip, 4),
59 PEDIT_FIELDS(IP6_, DST_95_64, 4, nat_lip, 8),
60 PEDIT_FIELDS(IP6_, DST_127_96, 4, nat_lip, 12),
61 PEDIT_FIELDS(TCP_, SPORT, 2, nat_fport, 0),
62 PEDIT_FIELDS(TCP_, DPORT, 2, nat_lport, 0),
63 PEDIT_FIELDS(UDP_, SPORT, 2, nat_fport, 0),
64 PEDIT_FIELDS(UDP_, DPORT, 2, nat_lport, 0),
65};
66
67static struct ch_tc_flower_entry *allocate_flower_entry(void)
68{
69 struct ch_tc_flower_entry *new = kzalloc(sizeof(*new), GFP_KERNEL);
70 if (new)
71 spin_lock_init(&new->lock);
72 return new;
73}
74
75
76static struct ch_tc_flower_entry *ch_flower_lookup(struct adapter *adap,
77 unsigned long flower_cookie)
78{
79 return rhashtable_lookup_fast(&adap->flower_tbl, &flower_cookie,
80 adap->flower_ht_params);
81}
82
83static void cxgb4_process_flow_match(struct net_device *dev,
84 struct flow_cls_offload *cls,
85 struct ch_filter_specification *fs)
86{
87 struct flow_rule *rule = flow_cls_offload_flow_rule(cls);
88 u16 addr_type = 0;
89
90 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
91 struct flow_match_control match;
92
93 flow_rule_match_control(rule, &match);
94 addr_type = match.key->addr_type;
95 }
96
97 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
98 struct flow_match_basic match;
99 u16 ethtype_key, ethtype_mask;
100
101 flow_rule_match_basic(rule, &match);
102 ethtype_key = ntohs(match.key->n_proto);
103 ethtype_mask = ntohs(match.mask->n_proto);
104
105 if (ethtype_key == ETH_P_ALL) {
106 ethtype_key = 0;
107 ethtype_mask = 0;
108 }
109
110 if (ethtype_key == ETH_P_IPV6)
111 fs->type = 1;
112
113 fs->val.ethtype = ethtype_key;
114 fs->mask.ethtype = ethtype_mask;
115 fs->val.proto = match.key->ip_proto;
116 fs->mask.proto = match.mask->ip_proto;
117 }
118
119 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
120 struct flow_match_ipv4_addrs match;
121
122 flow_rule_match_ipv4_addrs(rule, &match);
123 fs->type = 0;
124 memcpy(&fs->val.lip[0], &match.key->dst, sizeof(match.key->dst));
125 memcpy(&fs->val.fip[0], &match.key->src, sizeof(match.key->src));
126 memcpy(&fs->mask.lip[0], &match.mask->dst, sizeof(match.mask->dst));
127 memcpy(&fs->mask.fip[0], &match.mask->src, sizeof(match.mask->src));
128
129
130 memcpy(&fs->nat_lip[0], &match.key->dst, sizeof(match.key->dst));
131 memcpy(&fs->nat_fip[0], &match.key->src, sizeof(match.key->src));
132 }
133
134 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
135 struct flow_match_ipv6_addrs match;
136
137 flow_rule_match_ipv6_addrs(rule, &match);
138 fs->type = 1;
139 memcpy(&fs->val.lip[0], match.key->dst.s6_addr,
140 sizeof(match.key->dst));
141 memcpy(&fs->val.fip[0], match.key->src.s6_addr,
142 sizeof(match.key->src));
143 memcpy(&fs->mask.lip[0], match.mask->dst.s6_addr,
144 sizeof(match.mask->dst));
145 memcpy(&fs->mask.fip[0], match.mask->src.s6_addr,
146 sizeof(match.mask->src));
147
148
149 memcpy(&fs->nat_lip[0], match.key->dst.s6_addr,
150 sizeof(match.key->dst));
151 memcpy(&fs->nat_fip[0], match.key->src.s6_addr,
152 sizeof(match.key->src));
153 }
154
155 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
156 struct flow_match_ports match;
157
158 flow_rule_match_ports(rule, &match);
159 fs->val.lport = cpu_to_be16(match.key->dst);
160 fs->mask.lport = cpu_to_be16(match.mask->dst);
161 fs->val.fport = cpu_to_be16(match.key->src);
162 fs->mask.fport = cpu_to_be16(match.mask->src);
163
164
165 fs->nat_lport = cpu_to_be16(match.key->dst);
166 fs->nat_fport = cpu_to_be16(match.key->src);
167 }
168
169 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
170 struct flow_match_ip match;
171
172 flow_rule_match_ip(rule, &match);
173 fs->val.tos = match.key->tos;
174 fs->mask.tos = match.mask->tos;
175 }
176
177 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
178 struct flow_match_enc_keyid match;
179
180 flow_rule_match_enc_keyid(rule, &match);
181 fs->val.vni = be32_to_cpu(match.key->keyid);
182 fs->mask.vni = be32_to_cpu(match.mask->keyid);
183 if (fs->mask.vni) {
184 fs->val.encap_vld = 1;
185 fs->mask.encap_vld = 1;
186 }
187 }
188
189 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
190 struct flow_match_vlan match;
191 u16 vlan_tci, vlan_tci_mask;
192
193 flow_rule_match_vlan(rule, &match);
194 vlan_tci = match.key->vlan_id | (match.key->vlan_priority <<
195 VLAN_PRIO_SHIFT);
196 vlan_tci_mask = match.mask->vlan_id | (match.mask->vlan_priority <<
197 VLAN_PRIO_SHIFT);
198 fs->val.ivlan = vlan_tci;
199 fs->mask.ivlan = vlan_tci_mask;
200
201 fs->val.ivlan_vld = 1;
202 fs->mask.ivlan_vld = 1;
203
204
205
206
207
208
209
210
211
212
213 if (fs->val.ethtype == ETH_P_8021Q) {
214 fs->val.ethtype = 0;
215 fs->mask.ethtype = 0;
216 }
217 }
218
219
220
221
222 fs->val.iport = netdev2pinfo(dev)->port_id;
223 fs->mask.iport = ~0;
224}
225
226static int cxgb4_validate_flow_match(struct net_device *dev,
227 struct flow_cls_offload *cls)
228{
229 struct flow_rule *rule = flow_cls_offload_flow_rule(cls);
230 struct flow_dissector *dissector = rule->match.dissector;
231 u16 ethtype_mask = 0;
232 u16 ethtype_key = 0;
233
234 if (dissector->used_keys &
235 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
236 BIT(FLOW_DISSECTOR_KEY_BASIC) |
237 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
238 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
239 BIT(FLOW_DISSECTOR_KEY_PORTS) |
240 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
241 BIT(FLOW_DISSECTOR_KEY_VLAN) |
242 BIT(FLOW_DISSECTOR_KEY_IP))) {
243 netdev_warn(dev, "Unsupported key used: 0x%x\n",
244 dissector->used_keys);
245 return -EOPNOTSUPP;
246 }
247
248 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
249 struct flow_match_basic match;
250
251 flow_rule_match_basic(rule, &match);
252 ethtype_key = ntohs(match.key->n_proto);
253 ethtype_mask = ntohs(match.mask->n_proto);
254 }
255
256 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
257 u16 eth_ip_type = ethtype_key & ethtype_mask;
258 struct flow_match_ip match;
259
260 if (eth_ip_type != ETH_P_IP && eth_ip_type != ETH_P_IPV6) {
261 netdev_err(dev, "IP Key supported only with IPv4/v6");
262 return -EINVAL;
263 }
264
265 flow_rule_match_ip(rule, &match);
266 if (match.mask->ttl) {
267 netdev_warn(dev, "ttl match unsupported for offload");
268 return -EOPNOTSUPP;
269 }
270 }
271
272 return 0;
273}
274
275static void offload_pedit(struct ch_filter_specification *fs, u32 val, u32 mask,
276 u8 field)
277{
278 u32 set_val = val & ~mask;
279 u32 offset = 0;
280 u8 size = 1;
281 int i;
282
283 for (i = 0; i < ARRAY_SIZE(pedits); i++) {
284 if (pedits[i].field == field) {
285 offset = pedits[i].offset;
286 size = pedits[i].size;
287 break;
288 }
289 }
290 memcpy((u8 *)fs + offset, &set_val, size);
291}
292
293static void process_pedit_field(struct ch_filter_specification *fs, u32 val,
294 u32 mask, u32 offset, u8 htype)
295{
296 switch (htype) {
297 case FLOW_ACT_MANGLE_HDR_TYPE_ETH:
298 switch (offset) {
299 case PEDIT_ETH_DMAC_31_0:
300 fs->newdmac = 1;
301 offload_pedit(fs, val, mask, ETH_DMAC_31_0);
302 break;
303 case PEDIT_ETH_DMAC_47_32_SMAC_15_0:
304 if (~mask & PEDIT_ETH_DMAC_MASK)
305 offload_pedit(fs, val, mask, ETH_DMAC_47_32);
306 else
307 offload_pedit(fs, val >> 16, mask >> 16,
308 ETH_SMAC_15_0);
309 break;
310 case PEDIT_ETH_SMAC_47_16:
311 fs->newsmac = 1;
312 offload_pedit(fs, val, mask, ETH_SMAC_47_16);
313 }
314 break;
315 case FLOW_ACT_MANGLE_HDR_TYPE_IP4:
316 switch (offset) {
317 case PEDIT_IP4_SRC:
318 offload_pedit(fs, val, mask, IP4_SRC);
319 break;
320 case PEDIT_IP4_DST:
321 offload_pedit(fs, val, mask, IP4_DST);
322 }
323 fs->nat_mode = NAT_MODE_ALL;
324 break;
325 case FLOW_ACT_MANGLE_HDR_TYPE_IP6:
326 switch (offset) {
327 case PEDIT_IP6_SRC_31_0:
328 offload_pedit(fs, val, mask, IP6_SRC_31_0);
329 break;
330 case PEDIT_IP6_SRC_63_32:
331 offload_pedit(fs, val, mask, IP6_SRC_63_32);
332 break;
333 case PEDIT_IP6_SRC_95_64:
334 offload_pedit(fs, val, mask, IP6_SRC_95_64);
335 break;
336 case PEDIT_IP6_SRC_127_96:
337 offload_pedit(fs, val, mask, IP6_SRC_127_96);
338 break;
339 case PEDIT_IP6_DST_31_0:
340 offload_pedit(fs, val, mask, IP6_DST_31_0);
341 break;
342 case PEDIT_IP6_DST_63_32:
343 offload_pedit(fs, val, mask, IP6_DST_63_32);
344 break;
345 case PEDIT_IP6_DST_95_64:
346 offload_pedit(fs, val, mask, IP6_DST_95_64);
347 break;
348 case PEDIT_IP6_DST_127_96:
349 offload_pedit(fs, val, mask, IP6_DST_127_96);
350 }
351 fs->nat_mode = NAT_MODE_ALL;
352 break;
353 case FLOW_ACT_MANGLE_HDR_TYPE_TCP:
354 switch (offset) {
355 case PEDIT_TCP_SPORT_DPORT:
356 if (~mask & PEDIT_TCP_UDP_SPORT_MASK)
357 offload_pedit(fs, cpu_to_be32(val) >> 16,
358 cpu_to_be32(mask) >> 16,
359 TCP_SPORT);
360 else
361 offload_pedit(fs, cpu_to_be32(val),
362 cpu_to_be32(mask), TCP_DPORT);
363 }
364 fs->nat_mode = NAT_MODE_ALL;
365 break;
366 case FLOW_ACT_MANGLE_HDR_TYPE_UDP:
367 switch (offset) {
368 case PEDIT_UDP_SPORT_DPORT:
369 if (~mask & PEDIT_TCP_UDP_SPORT_MASK)
370 offload_pedit(fs, cpu_to_be32(val) >> 16,
371 cpu_to_be32(mask) >> 16,
372 UDP_SPORT);
373 else
374 offload_pedit(fs, cpu_to_be32(val),
375 cpu_to_be32(mask), UDP_DPORT);
376 }
377 fs->nat_mode = NAT_MODE_ALL;
378 }
379}
380
381void cxgb4_process_flow_actions(struct net_device *in,
382 struct flow_action *actions,
383 struct ch_filter_specification *fs)
384{
385 struct flow_action_entry *act;
386 int i;
387
388 flow_action_for_each(i, act, actions) {
389 switch (act->id) {
390 case FLOW_ACTION_ACCEPT:
391 fs->action = FILTER_PASS;
392 break;
393 case FLOW_ACTION_DROP:
394 fs->action = FILTER_DROP;
395 break;
396 case FLOW_ACTION_REDIRECT: {
397 struct net_device *out = act->dev;
398 struct port_info *pi = netdev_priv(out);
399
400 fs->action = FILTER_SWITCH;
401 fs->eport = pi->port_id;
402 }
403 break;
404 case FLOW_ACTION_VLAN_POP:
405 case FLOW_ACTION_VLAN_PUSH:
406 case FLOW_ACTION_VLAN_MANGLE: {
407 u8 prio = act->vlan.prio;
408 u16 vid = act->vlan.vid;
409 u16 vlan_tci = (prio << VLAN_PRIO_SHIFT) | vid;
410 switch (act->id) {
411 case FLOW_ACTION_VLAN_POP:
412 fs->newvlan |= VLAN_REMOVE;
413 break;
414 case FLOW_ACTION_VLAN_PUSH:
415 fs->newvlan |= VLAN_INSERT;
416 fs->vlan = vlan_tci;
417 break;
418 case FLOW_ACTION_VLAN_MANGLE:
419 fs->newvlan |= VLAN_REWRITE;
420 fs->vlan = vlan_tci;
421 break;
422 default:
423 break;
424 }
425 }
426 break;
427 case FLOW_ACTION_MANGLE: {
428 u32 mask, val, offset;
429 u8 htype;
430
431 htype = act->mangle.htype;
432 mask = act->mangle.mask;
433 val = act->mangle.val;
434 offset = act->mangle.offset;
435
436 process_pedit_field(fs, val, mask, offset, htype);
437 }
438 break;
439 default:
440 break;
441 }
442 }
443}
444
445static bool valid_l4_mask(u32 mask)
446{
447 u16 hi, lo;
448
449
450
451
452 hi = (mask >> 16) & 0xFFFF;
453 lo = mask & 0xFFFF;
454
455 return hi && lo ? false : true;
456}
457
458static bool valid_pedit_action(struct net_device *dev,
459 const struct flow_action_entry *act)
460{
461 u32 mask, offset;
462 u8 htype;
463
464 htype = act->mangle.htype;
465 mask = act->mangle.mask;
466 offset = act->mangle.offset;
467
468 switch (htype) {
469 case FLOW_ACT_MANGLE_HDR_TYPE_ETH:
470 switch (offset) {
471 case PEDIT_ETH_DMAC_31_0:
472 case PEDIT_ETH_DMAC_47_32_SMAC_15_0:
473 case PEDIT_ETH_SMAC_47_16:
474 break;
475 default:
476 netdev_err(dev, "%s: Unsupported pedit field\n",
477 __func__);
478 return false;
479 }
480 break;
481 case FLOW_ACT_MANGLE_HDR_TYPE_IP4:
482 switch (offset) {
483 case PEDIT_IP4_SRC:
484 case PEDIT_IP4_DST:
485 break;
486 default:
487 netdev_err(dev, "%s: Unsupported pedit field\n",
488 __func__);
489 return false;
490 }
491 break;
492 case FLOW_ACT_MANGLE_HDR_TYPE_IP6:
493 switch (offset) {
494 case PEDIT_IP6_SRC_31_0:
495 case PEDIT_IP6_SRC_63_32:
496 case PEDIT_IP6_SRC_95_64:
497 case PEDIT_IP6_SRC_127_96:
498 case PEDIT_IP6_DST_31_0:
499 case PEDIT_IP6_DST_63_32:
500 case PEDIT_IP6_DST_95_64:
501 case PEDIT_IP6_DST_127_96:
502 break;
503 default:
504 netdev_err(dev, "%s: Unsupported pedit field\n",
505 __func__);
506 return false;
507 }
508 break;
509 case FLOW_ACT_MANGLE_HDR_TYPE_TCP:
510 switch (offset) {
511 case PEDIT_TCP_SPORT_DPORT:
512 if (!valid_l4_mask(~mask)) {
513 netdev_err(dev, "%s: Unsupported mask for TCP L4 ports\n",
514 __func__);
515 return false;
516 }
517 break;
518 default:
519 netdev_err(dev, "%s: Unsupported pedit field\n",
520 __func__);
521 return false;
522 }
523 break;
524 case FLOW_ACT_MANGLE_HDR_TYPE_UDP:
525 switch (offset) {
526 case PEDIT_UDP_SPORT_DPORT:
527 if (!valid_l4_mask(~mask)) {
528 netdev_err(dev, "%s: Unsupported mask for UDP L4 ports\n",
529 __func__);
530 return false;
531 }
532 break;
533 default:
534 netdev_err(dev, "%s: Unsupported pedit field\n",
535 __func__);
536 return false;
537 }
538 break;
539 default:
540 netdev_err(dev, "%s: Unsupported pedit type\n", __func__);
541 return false;
542 }
543 return true;
544}
545
546int cxgb4_validate_flow_actions(struct net_device *dev,
547 struct flow_action *actions)
548{
549 struct flow_action_entry *act;
550 bool act_redir = false;
551 bool act_pedit = false;
552 bool act_vlan = false;
553 int i;
554
555 flow_action_for_each(i, act, actions) {
556 switch (act->id) {
557 case FLOW_ACTION_ACCEPT:
558 case FLOW_ACTION_DROP:
559
560 break;
561 case FLOW_ACTION_REDIRECT: {
562 struct adapter *adap = netdev2adap(dev);
563 struct net_device *n_dev, *target_dev;
564 unsigned int i;
565 bool found = false;
566
567 target_dev = act->dev;
568 for_each_port(adap, i) {
569 n_dev = adap->port[i];
570 if (target_dev == n_dev) {
571 found = true;
572 break;
573 }
574 }
575
576
577
578
579 if (!found) {
580 netdev_err(dev, "%s: Out port invalid\n",
581 __func__);
582 return -EINVAL;
583 }
584 act_redir = true;
585 }
586 break;
587 case FLOW_ACTION_VLAN_POP:
588 case FLOW_ACTION_VLAN_PUSH:
589 case FLOW_ACTION_VLAN_MANGLE: {
590 u16 proto = be16_to_cpu(act->vlan.proto);
591
592 switch (act->id) {
593 case FLOW_ACTION_VLAN_POP:
594 break;
595 case FLOW_ACTION_VLAN_PUSH:
596 case FLOW_ACTION_VLAN_MANGLE:
597 if (proto != ETH_P_8021Q) {
598 netdev_err(dev, "%s: Unsupported vlan proto\n",
599 __func__);
600 return -EOPNOTSUPP;
601 }
602 break;
603 default:
604 netdev_err(dev, "%s: Unsupported vlan action\n",
605 __func__);
606 return -EOPNOTSUPP;
607 }
608 act_vlan = true;
609 }
610 break;
611 case FLOW_ACTION_MANGLE: {
612 bool pedit_valid = valid_pedit_action(dev, act);
613
614 if (!pedit_valid)
615 return -EOPNOTSUPP;
616 act_pedit = true;
617 }
618 break;
619 default:
620 netdev_err(dev, "%s: Unsupported action\n", __func__);
621 return -EOPNOTSUPP;
622 }
623 }
624
625 if ((act_pedit || act_vlan) && !act_redir) {
626 netdev_err(dev, "%s: pedit/vlan rewrite invalid without egress redirect\n",
627 __func__);
628 return -EINVAL;
629 }
630
631 return 0;
632}
633
634static void cxgb4_tc_flower_hash_prio_add(struct adapter *adap, u32 tc_prio)
635{
636 spin_lock_bh(&adap->tids.ftid_lock);
637 if (adap->tids.tc_hash_tids_max_prio < tc_prio)
638 adap->tids.tc_hash_tids_max_prio = tc_prio;
639 spin_unlock_bh(&adap->tids.ftid_lock);
640}
641
642static void cxgb4_tc_flower_hash_prio_del(struct adapter *adap, u32 tc_prio)
643{
644 struct tid_info *t = &adap->tids;
645 struct ch_tc_flower_entry *fe;
646 struct rhashtable_iter iter;
647 u32 found = 0;
648
649 spin_lock_bh(&t->ftid_lock);
650
651
652
653 if (t->tc_hash_tids_max_prio != tc_prio)
654 goto out_unlock;
655
656
657
658
659 rhashtable_walk_enter(&adap->flower_tbl, &iter);
660 do {
661 rhashtable_walk_start(&iter);
662
663 fe = rhashtable_walk_next(&iter);
664 while (!IS_ERR_OR_NULL(fe)) {
665 if (fe->fs.hash &&
666 fe->fs.tc_prio <= t->tc_hash_tids_max_prio) {
667 t->tc_hash_tids_max_prio = fe->fs.tc_prio;
668 found++;
669
670
671
672
673
674 if (fe->fs.tc_prio == tc_prio)
675 break;
676 }
677
678 fe = rhashtable_walk_next(&iter);
679 }
680
681 rhashtable_walk_stop(&iter);
682 } while (fe == ERR_PTR(-EAGAIN));
683 rhashtable_walk_exit(&iter);
684
685 if (!found)
686 t->tc_hash_tids_max_prio = 0;
687
688out_unlock:
689 spin_unlock_bh(&t->ftid_lock);
690}
691
692int cxgb4_tc_flower_replace(struct net_device *dev,
693 struct flow_cls_offload *cls)
694{
695 struct flow_rule *rule = flow_cls_offload_flow_rule(cls);
696 struct netlink_ext_ack *extack = cls->common.extack;
697 struct adapter *adap = netdev2adap(dev);
698 struct ch_tc_flower_entry *ch_flower;
699 struct ch_filter_specification *fs;
700 struct filter_ctx ctx;
701 u8 inet_family;
702 int fidx, ret;
703
704 if (cxgb4_validate_flow_actions(dev, &rule->action))
705 return -EOPNOTSUPP;
706
707 if (cxgb4_validate_flow_match(dev, cls))
708 return -EOPNOTSUPP;
709
710 ch_flower = allocate_flower_entry();
711 if (!ch_flower) {
712 netdev_err(dev, "%s: ch_flower alloc failed.\n", __func__);
713 return -ENOMEM;
714 }
715
716 fs = &ch_flower->fs;
717 fs->hitcnts = 1;
718 cxgb4_process_flow_match(dev, cls, fs);
719 cxgb4_process_flow_actions(dev, &rule->action, fs);
720
721 fs->hash = is_filter_exact_match(adap, fs);
722 inet_family = fs->type ? PF_INET6 : PF_INET;
723
724
725
726
727
728 fidx = cxgb4_get_free_ftid(dev, inet_family, fs->hash,
729 cls->common.prio);
730 if (fidx < 0) {
731 NL_SET_ERR_MSG_MOD(extack,
732 "No free LETCAM index available");
733 ret = -ENOMEM;
734 goto free_entry;
735 }
736
737 if (fidx < adap->tids.nhpftids) {
738 fs->prio = 1;
739 fs->hash = 0;
740 }
741
742
743
744
745 if (fs->hash)
746 fidx = 0;
747
748 fs->tc_prio = cls->common.prio;
749 fs->tc_cookie = cls->cookie;
750
751 init_completion(&ctx.completion);
752 ret = __cxgb4_set_filter(dev, fidx, fs, &ctx);
753 if (ret) {
754 netdev_err(dev, "%s: filter creation err %d\n",
755 __func__, ret);
756 goto free_entry;
757 }
758
759
760 ret = wait_for_completion_timeout(&ctx.completion, 10 * HZ);
761 if (!ret) {
762 ret = -ETIMEDOUT;
763 goto free_entry;
764 }
765
766 ret = ctx.result;
767
768 if (ret)
769 goto free_entry;
770
771 ch_flower->tc_flower_cookie = cls->cookie;
772 ch_flower->filter_id = ctx.tid;
773 ret = rhashtable_insert_fast(&adap->flower_tbl, &ch_flower->node,
774 adap->flower_ht_params);
775 if (ret)
776 goto del_filter;
777
778 if (fs->hash)
779 cxgb4_tc_flower_hash_prio_add(adap, cls->common.prio);
780
781 return 0;
782
783del_filter:
784 cxgb4_del_filter(dev, ch_flower->filter_id, &ch_flower->fs);
785
786free_entry:
787 kfree(ch_flower);
788 return ret;
789}
790
791int cxgb4_tc_flower_destroy(struct net_device *dev,
792 struct flow_cls_offload *cls)
793{
794 struct adapter *adap = netdev2adap(dev);
795 struct ch_tc_flower_entry *ch_flower;
796 u32 tc_prio;
797 bool hash;
798 int ret;
799
800 ch_flower = ch_flower_lookup(adap, cls->cookie);
801 if (!ch_flower)
802 return -ENOENT;
803
804 hash = ch_flower->fs.hash;
805 tc_prio = ch_flower->fs.tc_prio;
806
807 ret = cxgb4_del_filter(dev, ch_flower->filter_id, &ch_flower->fs);
808 if (ret)
809 goto err;
810
811 ret = rhashtable_remove_fast(&adap->flower_tbl, &ch_flower->node,
812 adap->flower_ht_params);
813 if (ret) {
814 netdev_err(dev, "Flow remove from rhashtable failed");
815 goto err;
816 }
817 kfree_rcu(ch_flower, rcu);
818
819 if (hash)
820 cxgb4_tc_flower_hash_prio_del(adap, tc_prio);
821
822err:
823 return ret;
824}
825
826static void ch_flower_stats_handler(struct work_struct *work)
827{
828 struct adapter *adap = container_of(work, struct adapter,
829 flower_stats_work);
830 struct ch_tc_flower_entry *flower_entry;
831 struct ch_tc_flower_stats *ofld_stats;
832 struct rhashtable_iter iter;
833 u64 packets;
834 u64 bytes;
835 int ret;
836
837 rhashtable_walk_enter(&adap->flower_tbl, &iter);
838 do {
839 rhashtable_walk_start(&iter);
840
841 while ((flower_entry = rhashtable_walk_next(&iter)) &&
842 !IS_ERR(flower_entry)) {
843 ret = cxgb4_get_filter_counters(adap->port[0],
844 flower_entry->filter_id,
845 &packets, &bytes,
846 flower_entry->fs.hash);
847 if (!ret) {
848 spin_lock(&flower_entry->lock);
849 ofld_stats = &flower_entry->stats;
850
851 if (ofld_stats->prev_packet_count != packets) {
852 ofld_stats->prev_packet_count = packets;
853 ofld_stats->last_used = jiffies;
854 }
855 spin_unlock(&flower_entry->lock);
856 }
857 }
858
859 rhashtable_walk_stop(&iter);
860
861 } while (flower_entry == ERR_PTR(-EAGAIN));
862 rhashtable_walk_exit(&iter);
863 mod_timer(&adap->flower_stats_timer, jiffies + STATS_CHECK_PERIOD);
864}
865
866static void ch_flower_stats_cb(struct timer_list *t)
867{
868 struct adapter *adap = from_timer(adap, t, flower_stats_timer);
869
870 schedule_work(&adap->flower_stats_work);
871}
872
873int cxgb4_tc_flower_stats(struct net_device *dev,
874 struct flow_cls_offload *cls)
875{
876 struct adapter *adap = netdev2adap(dev);
877 struct ch_tc_flower_stats *ofld_stats;
878 struct ch_tc_flower_entry *ch_flower;
879 u64 packets;
880 u64 bytes;
881 int ret;
882
883 ch_flower = ch_flower_lookup(adap, cls->cookie);
884 if (!ch_flower) {
885 ret = -ENOENT;
886 goto err;
887 }
888
889 ret = cxgb4_get_filter_counters(dev, ch_flower->filter_id,
890 &packets, &bytes,
891 ch_flower->fs.hash);
892 if (ret < 0)
893 goto err;
894
895 spin_lock_bh(&ch_flower->lock);
896 ofld_stats = &ch_flower->stats;
897 if (ofld_stats->packet_count != packets) {
898 if (ofld_stats->prev_packet_count != packets)
899 ofld_stats->last_used = jiffies;
900 flow_stats_update(&cls->stats, bytes - ofld_stats->byte_count,
901 packets - ofld_stats->packet_count, 0,
902 ofld_stats->last_used,
903 FLOW_ACTION_HW_STATS_IMMEDIATE);
904
905 ofld_stats->packet_count = packets;
906 ofld_stats->byte_count = bytes;
907 ofld_stats->prev_packet_count = packets;
908 }
909 spin_unlock_bh(&ch_flower->lock);
910 return 0;
911
912err:
913 return ret;
914}
915
916static const struct rhashtable_params cxgb4_tc_flower_ht_params = {
917 .nelem_hint = 384,
918 .head_offset = offsetof(struct ch_tc_flower_entry, node),
919 .key_offset = offsetof(struct ch_tc_flower_entry, tc_flower_cookie),
920 .key_len = sizeof(((struct ch_tc_flower_entry *)0)->tc_flower_cookie),
921 .max_size = 524288,
922 .min_size = 512,
923 .automatic_shrinking = true
924};
925
926int cxgb4_init_tc_flower(struct adapter *adap)
927{
928 int ret;
929
930 if (adap->tc_flower_initialized)
931 return -EEXIST;
932
933 adap->flower_ht_params = cxgb4_tc_flower_ht_params;
934 ret = rhashtable_init(&adap->flower_tbl, &adap->flower_ht_params);
935 if (ret)
936 return ret;
937
938 INIT_WORK(&adap->flower_stats_work, ch_flower_stats_handler);
939 timer_setup(&adap->flower_stats_timer, ch_flower_stats_cb, 0);
940 mod_timer(&adap->flower_stats_timer, jiffies + STATS_CHECK_PERIOD);
941 adap->tc_flower_initialized = true;
942 return 0;
943}
944
945void cxgb4_cleanup_tc_flower(struct adapter *adap)
946{
947 if (!adap->tc_flower_initialized)
948 return;
949
950 if (adap->flower_stats_timer.function)
951 del_timer_sync(&adap->flower_stats_timer);
952 cancel_work_sync(&adap->flower_stats_work);
953 rhashtable_destroy(&adap->flower_tbl);
954 adap->tc_flower_initialized = false;
955}
956