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