1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2014-2021 Broadcom 3 * All rights reserved. 4 */ 5 6 #include <sys/queue.h> 7 8 #include <rte_log.h> 9 #include <rte_malloc.h> 10 #include <rte_flow.h> 11 #include <rte_flow_driver.h> 12 #include <rte_tailq.h> 13 #include <rte_alarm.h> 14 #include <rte_cycles.h> 15 16 #include "bnxt.h" 17 #include "bnxt_filter.h" 18 #include "bnxt_hwrm.h" 19 #include "bnxt_ring.h" 20 #include "bnxt_rxq.h" 21 #include "bnxt_rxr.h" 22 #include "bnxt_vnic.h" 23 #include "hsi_struct_def_dpdk.h" 24 25 static int 26 bnxt_flow_args_validate(const struct rte_flow_attr *attr, 27 const struct rte_flow_item pattern[], 28 const struct rte_flow_action actions[], 29 struct rte_flow_error *error) 30 { 31 if (!pattern) { 32 rte_flow_error_set(error, 33 EINVAL, 34 RTE_FLOW_ERROR_TYPE_ITEM_NUM, 35 NULL, 36 "NULL pattern."); 37 return -rte_errno; 38 } 39 40 if (!actions) { 41 rte_flow_error_set(error, 42 EINVAL, 43 RTE_FLOW_ERROR_TYPE_ACTION_NUM, 44 NULL, 45 "NULL action."); 46 return -rte_errno; 47 } 48 49 if (!attr) { 50 rte_flow_error_set(error, 51 EINVAL, 52 RTE_FLOW_ERROR_TYPE_ATTR, 53 NULL, 54 "NULL attribute."); 55 return -rte_errno; 56 } 57 58 return 0; 59 } 60 61 static const struct rte_flow_item * 62 bnxt_flow_non_void_item(const struct rte_flow_item *cur) 63 { 64 while (1) { 65 if (cur->type != RTE_FLOW_ITEM_TYPE_VOID) 66 return cur; 67 cur++; 68 } 69 } 70 71 static const struct rte_flow_action * 72 bnxt_flow_non_void_action(const struct rte_flow_action *cur) 73 { 74 while (1) { 75 if (cur->type != RTE_FLOW_ACTION_TYPE_VOID) 76 return cur; 77 cur++; 78 } 79 } 80 81 static int 82 bnxt_filter_type_check(const struct rte_flow_item pattern[], 83 struct rte_flow_error *error) 84 { 85 const struct rte_flow_item *item = 86 bnxt_flow_non_void_item(pattern); 87 int use_ntuple = 1; 88 bool has_vlan = 0; 89 90 while (item->type != RTE_FLOW_ITEM_TYPE_END) { 91 switch (item->type) { 92 case RTE_FLOW_ITEM_TYPE_ANY: 93 case RTE_FLOW_ITEM_TYPE_ETH: 94 use_ntuple = 0; 95 break; 96 case RTE_FLOW_ITEM_TYPE_VLAN: 97 use_ntuple = 0; 98 has_vlan = 1; 99 break; 100 case RTE_FLOW_ITEM_TYPE_IPV4: 101 case RTE_FLOW_ITEM_TYPE_IPV6: 102 case RTE_FLOW_ITEM_TYPE_TCP: 103 case RTE_FLOW_ITEM_TYPE_UDP: 104 /* FALLTHROUGH */ 105 /* need ntuple match, reset exact match */ 106 use_ntuple |= 1; 107 break; 108 default: 109 PMD_DRV_LOG(DEBUG, "Unknown Flow type\n"); 110 use_ntuple |= 0; 111 } 112 item++; 113 } 114 115 if (has_vlan && use_ntuple) { 116 PMD_DRV_LOG(ERR, 117 "VLAN flow cannot use NTUPLE filter\n"); 118 rte_flow_error_set(error, EINVAL, 119 RTE_FLOW_ERROR_TYPE_ITEM, 120 item, 121 "Cannot use VLAN with NTUPLE"); 122 return -rte_errno; 123 } 124 125 return use_ntuple; 126 } 127 128 static int 129 bnxt_validate_and_parse_flow_type(struct bnxt *bp, 130 const struct rte_flow_attr *attr, 131 const struct rte_flow_item pattern[], 132 struct rte_flow_error *error, 133 struct bnxt_filter_info *filter) 134 { 135 const struct rte_flow_item *item = bnxt_flow_non_void_item(pattern); 136 const struct rte_flow_item_vlan *vlan_spec, *vlan_mask; 137 const struct rte_flow_item_ipv4 *ipv4_spec, *ipv4_mask; 138 const struct rte_flow_item_ipv6 *ipv6_spec, *ipv6_mask; 139 const struct rte_flow_item_tcp *tcp_spec, *tcp_mask; 140 const struct rte_flow_item_udp *udp_spec, *udp_mask; 141 const struct rte_flow_item_eth *eth_spec, *eth_mask; 142 const struct rte_ether_addr *dst, *src; 143 const struct rte_flow_item_nvgre *nvgre_spec; 144 const struct rte_flow_item_nvgre *nvgre_mask; 145 const struct rte_flow_item_gre *gre_spec; 146 const struct rte_flow_item_gre *gre_mask; 147 const struct rte_flow_item_vxlan *vxlan_spec; 148 const struct rte_flow_item_vxlan *vxlan_mask; 149 uint8_t vni_mask[] = {0xFF, 0xFF, 0xFF}; 150 uint8_t tni_mask[] = {0xFF, 0xFF, 0xFF}; 151 const struct rte_flow_item_vf *vf_spec; 152 uint32_t tenant_id_be = 0, valid_flags = 0; 153 bool vni_masked = 0; 154 bool tni_masked = 0; 155 uint32_t en_ethertype; 156 uint8_t inner = 0; 157 uint32_t vf = 0; 158 uint32_t en = 0; 159 int use_ntuple; 160 int dflt_vnic; 161 162 use_ntuple = bnxt_filter_type_check(pattern, error); 163 if (use_ntuple < 0) 164 return use_ntuple; 165 PMD_DRV_LOG(DEBUG, "Use NTUPLE %d\n", use_ntuple); 166 167 filter->filter_type = use_ntuple ? 168 HWRM_CFA_NTUPLE_FILTER : HWRM_CFA_L2_FILTER; 169 en_ethertype = use_ntuple ? 170 NTUPLE_FLTR_ALLOC_INPUT_EN_ETHERTYPE : 171 EM_FLOW_ALLOC_INPUT_EN_ETHERTYPE; 172 173 while (item->type != RTE_FLOW_ITEM_TYPE_END) { 174 if (item->last) { 175 /* last or range is NOT supported as match criteria */ 176 rte_flow_error_set(error, EINVAL, 177 RTE_FLOW_ERROR_TYPE_ITEM, 178 item, 179 "No support for range"); 180 return -rte_errno; 181 } 182 183 switch (item->type) { 184 case RTE_FLOW_ITEM_TYPE_ANY: 185 inner = 186 ((const struct rte_flow_item_any *)item->spec)->num > 3; 187 if (inner) 188 PMD_DRV_LOG(DEBUG, "Parse inner header\n"); 189 break; 190 case RTE_FLOW_ITEM_TYPE_ETH: 191 if (!item->spec) 192 break; 193 194 eth_spec = item->spec; 195 196 if (item->mask) 197 eth_mask = item->mask; 198 else 199 eth_mask = &rte_flow_item_eth_mask; 200 201 /* Source MAC address mask cannot be partially set. 202 * Should be All 0's or all 1's. 203 * Destination MAC address mask must not be partially 204 * set. Should be all 1's or all 0's. 205 */ 206 if ((!rte_is_zero_ether_addr(ð_mask->src) && 207 !rte_is_broadcast_ether_addr(ð_mask->src)) || 208 (!rte_is_zero_ether_addr(ð_mask->dst) && 209 !rte_is_broadcast_ether_addr(ð_mask->dst))) { 210 rte_flow_error_set(error, 211 EINVAL, 212 RTE_FLOW_ERROR_TYPE_ITEM, 213 item, 214 "MAC_addr mask not valid"); 215 return -rte_errno; 216 } 217 218 /* Mask is not allowed. Only exact matches are */ 219 if (eth_mask->type && 220 eth_mask->type != RTE_BE16(0xffff)) { 221 rte_flow_error_set(error, EINVAL, 222 RTE_FLOW_ERROR_TYPE_ITEM, 223 item, 224 "ethertype mask not valid"); 225 return -rte_errno; 226 } 227 228 if (rte_is_broadcast_ether_addr(ð_mask->dst)) { 229 dst = ð_spec->dst; 230 if (!rte_is_valid_assigned_ether_addr(dst)) { 231 rte_flow_error_set(error, 232 EINVAL, 233 RTE_FLOW_ERROR_TYPE_ITEM, 234 item, 235 "DMAC is invalid"); 236 PMD_DRV_LOG(ERR, 237 "DMAC is invalid!\n"); 238 return -rte_errno; 239 } 240 rte_memcpy(filter->dst_macaddr, 241 ð_spec->dst, RTE_ETHER_ADDR_LEN); 242 en |= use_ntuple ? 243 NTUPLE_FLTR_ALLOC_INPUT_EN_DST_MACADDR : 244 EM_FLOW_ALLOC_INPUT_EN_DST_MACADDR; 245 valid_flags |= inner ? 246 BNXT_FLOW_L2_INNER_DST_VALID_FLAG : 247 BNXT_FLOW_L2_DST_VALID_FLAG; 248 filter->priority = attr->priority; 249 PMD_DRV_LOG(DEBUG, 250 "Creating a priority flow\n"); 251 } 252 if (rte_is_broadcast_ether_addr(ð_mask->src)) { 253 src = ð_spec->src; 254 if (!rte_is_valid_assigned_ether_addr(src)) { 255 rte_flow_error_set(error, 256 EINVAL, 257 RTE_FLOW_ERROR_TYPE_ITEM, 258 item, 259 "SMAC is invalid"); 260 PMD_DRV_LOG(ERR, 261 "SMAC is invalid!\n"); 262 return -rte_errno; 263 } 264 rte_memcpy(filter->src_macaddr, 265 ð_spec->src, RTE_ETHER_ADDR_LEN); 266 en |= use_ntuple ? 267 NTUPLE_FLTR_ALLOC_INPUT_EN_SRC_MACADDR : 268 EM_FLOW_ALLOC_INPUT_EN_SRC_MACADDR; 269 valid_flags |= inner ? 270 BNXT_FLOW_L2_INNER_SRC_VALID_FLAG : 271 BNXT_FLOW_L2_SRC_VALID_FLAG; 272 } /* 273 * else { 274 * PMD_DRV_LOG(ERR, "Handle this condition\n"); 275 * } 276 */ 277 if (eth_mask->type) { 278 filter->ethertype = 279 rte_be_to_cpu_16(eth_spec->type); 280 en |= en_ethertype; 281 } 282 if (inner) 283 valid_flags |= BNXT_FLOW_PARSE_INNER_FLAG; 284 285 break; 286 case RTE_FLOW_ITEM_TYPE_VLAN: 287 vlan_spec = item->spec; 288 289 if (item->mask) 290 vlan_mask = item->mask; 291 else 292 vlan_mask = &rte_flow_item_vlan_mask; 293 294 if (en & en_ethertype) { 295 rte_flow_error_set(error, EINVAL, 296 RTE_FLOW_ERROR_TYPE_ITEM, 297 item, 298 "VLAN TPID matching is not" 299 " supported"); 300 return -rte_errno; 301 } 302 if (vlan_mask->tci && 303 vlan_mask->tci == RTE_BE16(0x0fff)) { 304 /* Only the VLAN ID can be matched. */ 305 filter->l2_ovlan = 306 rte_be_to_cpu_16(vlan_spec->tci & 307 RTE_BE16(0x0fff)); 308 en |= EM_FLOW_ALLOC_INPUT_EN_OVLAN_VID; 309 } else { 310 rte_flow_error_set(error, 311 EINVAL, 312 RTE_FLOW_ERROR_TYPE_ITEM, 313 item, 314 "VLAN mask is invalid"); 315 return -rte_errno; 316 } 317 if (vlan_mask->inner_type && 318 vlan_mask->inner_type != RTE_BE16(0xffff)) { 319 rte_flow_error_set(error, EINVAL, 320 RTE_FLOW_ERROR_TYPE_ITEM, 321 item, 322 "inner ethertype mask not" 323 " valid"); 324 return -rte_errno; 325 } 326 if (vlan_mask->inner_type) { 327 filter->ethertype = 328 rte_be_to_cpu_16(vlan_spec->inner_type); 329 en |= en_ethertype; 330 } 331 332 break; 333 case RTE_FLOW_ITEM_TYPE_IPV4: 334 /* If mask is not involved, we could use EM filters. */ 335 ipv4_spec = item->spec; 336 337 if (!item->spec) 338 break; 339 340 if (item->mask) 341 ipv4_mask = item->mask; 342 else 343 ipv4_mask = &rte_flow_item_ipv4_mask; 344 345 /* Only IP DST and SRC fields are maskable. */ 346 if (ipv4_mask->hdr.version_ihl || 347 ipv4_mask->hdr.type_of_service || 348 ipv4_mask->hdr.total_length || 349 ipv4_mask->hdr.packet_id || 350 ipv4_mask->hdr.fragment_offset || 351 ipv4_mask->hdr.time_to_live || 352 ipv4_mask->hdr.next_proto_id || 353 ipv4_mask->hdr.hdr_checksum) { 354 rte_flow_error_set(error, 355 EINVAL, 356 RTE_FLOW_ERROR_TYPE_ITEM, 357 item, 358 "Invalid IPv4 mask."); 359 return -rte_errno; 360 } 361 362 filter->dst_ipaddr[0] = ipv4_spec->hdr.dst_addr; 363 filter->src_ipaddr[0] = ipv4_spec->hdr.src_addr; 364 365 if (use_ntuple) 366 en |= NTUPLE_FLTR_ALLOC_INPUT_EN_SRC_IPADDR | 367 NTUPLE_FLTR_ALLOC_INPUT_EN_DST_IPADDR; 368 else 369 en |= EM_FLOW_ALLOC_INPUT_EN_SRC_IPADDR | 370 EM_FLOW_ALLOC_INPUT_EN_DST_IPADDR; 371 372 if (ipv4_mask->hdr.src_addr) { 373 filter->src_ipaddr_mask[0] = 374 ipv4_mask->hdr.src_addr; 375 en |= !use_ntuple ? 0 : 376 NTUPLE_FLTR_ALLOC_INPUT_EN_SRC_IPADDR_MASK; 377 } 378 379 if (ipv4_mask->hdr.dst_addr) { 380 filter->dst_ipaddr_mask[0] = 381 ipv4_mask->hdr.dst_addr; 382 en |= !use_ntuple ? 0 : 383 NTUPLE_FLTR_ALLOC_INPUT_EN_DST_IPADDR_MASK; 384 } 385 386 filter->ip_addr_type = use_ntuple ? 387 HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_IP_ADDR_TYPE_IPV4 : 388 HWRM_CFA_EM_FLOW_ALLOC_INPUT_IP_ADDR_TYPE_IPV4; 389 390 if (ipv4_spec->hdr.next_proto_id) { 391 filter->ip_protocol = 392 ipv4_spec->hdr.next_proto_id; 393 if (use_ntuple) 394 en |= NTUPLE_FLTR_ALLOC_IN_EN_IP_PROTO; 395 else 396 en |= EM_FLOW_ALLOC_INPUT_EN_IP_PROTO; 397 } 398 break; 399 case RTE_FLOW_ITEM_TYPE_IPV6: 400 ipv6_spec = item->spec; 401 402 if (!item->spec) 403 break; 404 405 if (item->mask) 406 ipv6_mask = item->mask; 407 else 408 ipv6_mask = &rte_flow_item_ipv6_mask; 409 410 /* Only IP DST and SRC fields are maskable. */ 411 if (ipv6_mask->hdr.vtc_flow || 412 ipv6_mask->hdr.payload_len || 413 ipv6_mask->hdr.proto || 414 ipv6_mask->hdr.hop_limits) { 415 rte_flow_error_set(error, 416 EINVAL, 417 RTE_FLOW_ERROR_TYPE_ITEM, 418 item, 419 "Invalid IPv6 mask."); 420 return -rte_errno; 421 } 422 423 if (use_ntuple) 424 en |= NTUPLE_FLTR_ALLOC_INPUT_EN_SRC_IPADDR | 425 NTUPLE_FLTR_ALLOC_INPUT_EN_DST_IPADDR; 426 else 427 en |= EM_FLOW_ALLOC_INPUT_EN_SRC_IPADDR | 428 EM_FLOW_ALLOC_INPUT_EN_DST_IPADDR; 429 430 rte_memcpy(filter->src_ipaddr, 431 ipv6_spec->hdr.src_addr, 16); 432 rte_memcpy(filter->dst_ipaddr, 433 ipv6_spec->hdr.dst_addr, 16); 434 435 if (!bnxt_check_zero_bytes(ipv6_mask->hdr.src_addr, 436 16)) { 437 rte_memcpy(filter->src_ipaddr_mask, 438 ipv6_mask->hdr.src_addr, 16); 439 en |= !use_ntuple ? 0 : 440 NTUPLE_FLTR_ALLOC_INPUT_EN_SRC_IPADDR_MASK; 441 } 442 443 if (!bnxt_check_zero_bytes(ipv6_mask->hdr.dst_addr, 444 16)) { 445 rte_memcpy(filter->dst_ipaddr_mask, 446 ipv6_mask->hdr.dst_addr, 16); 447 en |= !use_ntuple ? 0 : 448 NTUPLE_FLTR_ALLOC_INPUT_EN_DST_IPADDR_MASK; 449 } 450 451 filter->ip_addr_type = use_ntuple ? 452 NTUPLE_FLTR_ALLOC_INPUT_IP_ADDR_TYPE_IPV6 : 453 EM_FLOW_ALLOC_INPUT_IP_ADDR_TYPE_IPV6; 454 break; 455 case RTE_FLOW_ITEM_TYPE_TCP: 456 tcp_spec = item->spec; 457 458 if (!item->spec) 459 break; 460 461 if (item->mask) 462 tcp_mask = item->mask; 463 else 464 tcp_mask = &rte_flow_item_tcp_mask; 465 466 /* Check TCP mask. Only DST & SRC ports are maskable */ 467 if (tcp_mask->hdr.sent_seq || 468 tcp_mask->hdr.recv_ack || 469 tcp_mask->hdr.data_off || 470 tcp_mask->hdr.tcp_flags || 471 tcp_mask->hdr.rx_win || 472 tcp_mask->hdr.cksum || 473 tcp_mask->hdr.tcp_urp) { 474 rte_flow_error_set(error, 475 EINVAL, 476 RTE_FLOW_ERROR_TYPE_ITEM, 477 item, 478 "Invalid TCP mask"); 479 return -rte_errno; 480 } 481 482 filter->src_port = tcp_spec->hdr.src_port; 483 filter->dst_port = tcp_spec->hdr.dst_port; 484 485 if (use_ntuple) 486 en |= NTUPLE_FLTR_ALLOC_INPUT_EN_SRC_PORT | 487 NTUPLE_FLTR_ALLOC_INPUT_EN_DST_PORT; 488 else 489 en |= EM_FLOW_ALLOC_INPUT_EN_SRC_PORT | 490 EM_FLOW_ALLOC_INPUT_EN_DST_PORT; 491 492 if (tcp_mask->hdr.dst_port) { 493 filter->dst_port_mask = tcp_mask->hdr.dst_port; 494 en |= !use_ntuple ? 0 : 495 NTUPLE_FLTR_ALLOC_INPUT_EN_DST_PORT_MASK; 496 } 497 498 if (tcp_mask->hdr.src_port) { 499 filter->src_port_mask = tcp_mask->hdr.src_port; 500 en |= !use_ntuple ? 0 : 501 NTUPLE_FLTR_ALLOC_INPUT_EN_SRC_PORT_MASK; 502 } 503 break; 504 case RTE_FLOW_ITEM_TYPE_UDP: 505 udp_spec = item->spec; 506 507 if (!item->spec) 508 break; 509 510 if (item->mask) 511 udp_mask = item->mask; 512 else 513 udp_mask = &rte_flow_item_udp_mask; 514 515 if (udp_mask->hdr.dgram_len || 516 udp_mask->hdr.dgram_cksum) { 517 rte_flow_error_set(error, 518 EINVAL, 519 RTE_FLOW_ERROR_TYPE_ITEM, 520 item, 521 "Invalid UDP mask"); 522 return -rte_errno; 523 } 524 525 filter->src_port = udp_spec->hdr.src_port; 526 filter->dst_port = udp_spec->hdr.dst_port; 527 528 if (use_ntuple) 529 en |= NTUPLE_FLTR_ALLOC_INPUT_EN_SRC_PORT | 530 NTUPLE_FLTR_ALLOC_INPUT_EN_DST_PORT; 531 else 532 en |= EM_FLOW_ALLOC_INPUT_EN_SRC_PORT | 533 EM_FLOW_ALLOC_INPUT_EN_DST_PORT; 534 535 if (udp_mask->hdr.dst_port) { 536 filter->dst_port_mask = udp_mask->hdr.dst_port; 537 en |= !use_ntuple ? 0 : 538 NTUPLE_FLTR_ALLOC_INPUT_EN_DST_PORT_MASK; 539 } 540 541 if (udp_mask->hdr.src_port) { 542 filter->src_port_mask = udp_mask->hdr.src_port; 543 en |= !use_ntuple ? 0 : 544 NTUPLE_FLTR_ALLOC_INPUT_EN_SRC_PORT_MASK; 545 } 546 break; 547 case RTE_FLOW_ITEM_TYPE_VXLAN: 548 vxlan_spec = item->spec; 549 vxlan_mask = item->mask; 550 /* Check if VXLAN item is used to describe protocol. 551 * If yes, both spec and mask should be NULL. 552 * If no, both spec and mask shouldn't be NULL. 553 */ 554 if ((!vxlan_spec && vxlan_mask) || 555 (vxlan_spec && !vxlan_mask)) { 556 rte_flow_error_set(error, 557 EINVAL, 558 RTE_FLOW_ERROR_TYPE_ITEM, 559 item, 560 "Invalid VXLAN item"); 561 return -rte_errno; 562 } 563 564 if (!vxlan_spec && !vxlan_mask) { 565 filter->tunnel_type = 566 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_VXLAN; 567 break; 568 } 569 570 if (vxlan_spec->rsvd1 || vxlan_spec->rsvd0[0] || 571 vxlan_spec->rsvd0[1] || vxlan_spec->rsvd0[2] || 572 vxlan_spec->flags != 0x8) { 573 rte_flow_error_set(error, 574 EINVAL, 575 RTE_FLOW_ERROR_TYPE_ITEM, 576 item, 577 "Invalid VXLAN item"); 578 return -rte_errno; 579 } 580 581 /* Check if VNI is masked. */ 582 if (vxlan_mask != NULL) { 583 vni_masked = 584 !!memcmp(vxlan_mask->vni, vni_mask, 585 RTE_DIM(vni_mask)); 586 if (vni_masked) { 587 rte_flow_error_set 588 (error, 589 EINVAL, 590 RTE_FLOW_ERROR_TYPE_ITEM, 591 item, 592 "Invalid VNI mask"); 593 return -rte_errno; 594 } 595 596 rte_memcpy(((uint8_t *)&tenant_id_be + 1), 597 vxlan_spec->vni, 3); 598 filter->vni = 599 rte_be_to_cpu_32(tenant_id_be); 600 filter->tunnel_type = 601 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_VXLAN; 602 } 603 break; 604 case RTE_FLOW_ITEM_TYPE_NVGRE: 605 nvgre_spec = item->spec; 606 nvgre_mask = item->mask; 607 /* Check if NVGRE item is used to describe protocol. 608 * If yes, both spec and mask should be NULL. 609 * If no, both spec and mask shouldn't be NULL. 610 */ 611 if ((!nvgre_spec && nvgre_mask) || 612 (nvgre_spec && !nvgre_mask)) { 613 rte_flow_error_set(error, 614 EINVAL, 615 RTE_FLOW_ERROR_TYPE_ITEM, 616 item, 617 "Invalid NVGRE item"); 618 return -rte_errno; 619 } 620 621 if (!nvgre_spec && !nvgre_mask) { 622 filter->tunnel_type = 623 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_NVGRE; 624 break; 625 } 626 627 if (nvgre_spec->c_k_s_rsvd0_ver != 0x2000 || 628 nvgre_spec->protocol != 0x6558) { 629 rte_flow_error_set(error, 630 EINVAL, 631 RTE_FLOW_ERROR_TYPE_ITEM, 632 item, 633 "Invalid NVGRE item"); 634 return -rte_errno; 635 } 636 637 if (nvgre_spec && nvgre_mask) { 638 tni_masked = 639 !!memcmp(nvgre_mask->tni, tni_mask, 640 RTE_DIM(tni_mask)); 641 if (tni_masked) { 642 rte_flow_error_set 643 (error, 644 EINVAL, 645 RTE_FLOW_ERROR_TYPE_ITEM, 646 item, 647 "Invalid TNI mask"); 648 return -rte_errno; 649 } 650 rte_memcpy(((uint8_t *)&tenant_id_be + 1), 651 nvgre_spec->tni, 3); 652 filter->vni = 653 rte_be_to_cpu_32(tenant_id_be); 654 filter->tunnel_type = 655 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_NVGRE; 656 } 657 break; 658 659 case RTE_FLOW_ITEM_TYPE_GRE: 660 gre_spec = (const struct rte_flow_item_gre *)item->spec; 661 gre_mask = (const struct rte_flow_item_gre *)item->mask; 662 663 /* 664 *Check if GRE item is used to describe protocol. 665 * If yes, both spec and mask should be NULL. 666 * If no, both spec and mask shouldn't be NULL. 667 */ 668 if (!!gre_spec ^ !!gre_mask) { 669 rte_flow_error_set(error, EINVAL, 670 RTE_FLOW_ERROR_TYPE_ITEM, 671 item, 672 "Invalid GRE item"); 673 return -rte_errno; 674 } 675 676 if (!gre_spec && !gre_mask) { 677 filter->tunnel_type = 678 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_IPGRE; 679 break; 680 } 681 break; 682 683 case RTE_FLOW_ITEM_TYPE_VF: 684 vf_spec = item->spec; 685 vf = vf_spec->id; 686 if (!BNXT_PF(bp)) { 687 rte_flow_error_set(error, 688 EINVAL, 689 RTE_FLOW_ERROR_TYPE_ITEM, 690 item, 691 "Configuring on a VF!"); 692 return -rte_errno; 693 } 694 695 if (vf >= bp->pdev->max_vfs) { 696 rte_flow_error_set(error, 697 EINVAL, 698 RTE_FLOW_ERROR_TYPE_ITEM, 699 item, 700 "Incorrect VF id!"); 701 return -rte_errno; 702 } 703 704 if (!attr->transfer) { 705 rte_flow_error_set(error, 706 ENOTSUP, 707 RTE_FLOW_ERROR_TYPE_ITEM, 708 item, 709 "Matching VF traffic without" 710 " affecting it (transfer attribute)" 711 " is unsupported"); 712 return -rte_errno; 713 } 714 715 filter->mirror_vnic_id = 716 dflt_vnic = bnxt_hwrm_func_qcfg_vf_dflt_vnic_id(bp, vf); 717 if (dflt_vnic < 0) { 718 /* This simply indicates there's no driver 719 * loaded. This is not an error. 720 */ 721 rte_flow_error_set 722 (error, 723 EINVAL, 724 RTE_FLOW_ERROR_TYPE_ITEM, 725 item, 726 "Unable to get default VNIC for VF"); 727 return -rte_errno; 728 } 729 730 filter->mirror_vnic_id = dflt_vnic; 731 en |= NTUPLE_FLTR_ALLOC_INPUT_EN_MIRROR_VNIC_ID; 732 break; 733 default: 734 break; 735 } 736 item++; 737 } 738 filter->enables = en; 739 filter->valid_flags = valid_flags; 740 741 return 0; 742 } 743 744 /* Parse attributes */ 745 static int 746 bnxt_flow_parse_attr(const struct rte_flow_attr *attr, 747 struct rte_flow_error *error) 748 { 749 /* Must be input direction */ 750 if (!attr->ingress) { 751 rte_flow_error_set(error, 752 EINVAL, 753 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS, 754 attr, 755 "Only support ingress."); 756 return -rte_errno; 757 } 758 759 /* Not supported */ 760 if (attr->egress) { 761 rte_flow_error_set(error, 762 EINVAL, 763 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, 764 attr, 765 "No support for egress."); 766 return -rte_errno; 767 } 768 769 return 0; 770 } 771 772 static struct bnxt_filter_info * 773 bnxt_find_matching_l2_filter(struct bnxt *bp, struct bnxt_filter_info *nf) 774 { 775 struct bnxt_filter_info *mf, *f0; 776 struct bnxt_vnic_info *vnic0; 777 int i; 778 779 vnic0 = BNXT_GET_DEFAULT_VNIC(bp); 780 f0 = STAILQ_FIRST(&vnic0->filter); 781 782 /* This flow has same DST MAC as the port/l2 filter. */ 783 if (memcmp(f0->l2_addr, nf->dst_macaddr, RTE_ETHER_ADDR_LEN) == 0) 784 return f0; 785 786 for (i = bp->max_vnics - 1; i >= 0; i--) { 787 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 788 789 if (vnic->fw_vnic_id == INVALID_VNIC_ID) 790 continue; 791 792 STAILQ_FOREACH(mf, &vnic->filter, next) { 793 794 if (mf->matching_l2_fltr_ptr) 795 continue; 796 797 if (mf->ethertype == nf->ethertype && 798 mf->l2_ovlan == nf->l2_ovlan && 799 mf->l2_ovlan_mask == nf->l2_ovlan_mask && 800 mf->l2_ivlan == nf->l2_ivlan && 801 mf->l2_ivlan_mask == nf->l2_ivlan_mask && 802 !memcmp(mf->src_macaddr, nf->src_macaddr, 803 RTE_ETHER_ADDR_LEN) && 804 !memcmp(mf->dst_macaddr, nf->dst_macaddr, 805 RTE_ETHER_ADDR_LEN)) 806 return mf; 807 } 808 } 809 return NULL; 810 } 811 812 static struct bnxt_filter_info * 813 bnxt_create_l2_filter(struct bnxt *bp, struct bnxt_filter_info *nf, 814 struct bnxt_vnic_info *vnic) 815 { 816 struct bnxt_filter_info *filter1; 817 int rc; 818 819 /* Alloc new L2 filter. 820 * This flow needs MAC filter which does not match any existing 821 * L2 filters. 822 */ 823 filter1 = bnxt_get_unused_filter(bp); 824 if (filter1 == NULL) 825 return NULL; 826 827 memcpy(filter1, nf, sizeof(*filter1)); 828 829 filter1->flags = HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_XDP_DISABLE; 830 filter1->flags |= HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_PATH_RX; 831 if (nf->valid_flags & BNXT_FLOW_L2_SRC_VALID_FLAG || 832 nf->valid_flags & BNXT_FLOW_L2_DST_VALID_FLAG) { 833 filter1->flags |= 834 HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_OUTERMOST; 835 PMD_DRV_LOG(DEBUG, "Create Outer filter\n"); 836 } 837 838 if (nf->filter_type == HWRM_CFA_L2_FILTER && 839 (nf->valid_flags & BNXT_FLOW_L2_SRC_VALID_FLAG || 840 nf->valid_flags & BNXT_FLOW_L2_INNER_SRC_VALID_FLAG)) { 841 PMD_DRV_LOG(DEBUG, "Create L2 filter for SRC MAC\n"); 842 filter1->flags |= 843 HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_SOURCE_VALID; 844 memcpy(filter1->l2_addr, nf->src_macaddr, RTE_ETHER_ADDR_LEN); 845 } else { 846 PMD_DRV_LOG(DEBUG, "Create L2 filter for DST MAC\n"); 847 memcpy(filter1->l2_addr, nf->dst_macaddr, RTE_ETHER_ADDR_LEN); 848 } 849 850 if (nf->priority && 851 (nf->valid_flags & BNXT_FLOW_L2_DST_VALID_FLAG || 852 nf->valid_flags & BNXT_FLOW_L2_INNER_DST_VALID_FLAG)) { 853 /* Tell the FW where to place the filter in the table. */ 854 if (nf->priority > 65535) { 855 filter1->pri_hint = 856 HWRM_CFA_L2_FILTER_ALLOC_INPUT_PRI_HINT_BELOW_FILTER; 857 /* This will place the filter in TCAM */ 858 filter1->l2_filter_id_hint = (uint64_t)-1; 859 } 860 } 861 862 if (nf->valid_flags & (BNXT_FLOW_L2_DST_VALID_FLAG | 863 BNXT_FLOW_L2_SRC_VALID_FLAG | 864 BNXT_FLOW_L2_INNER_SRC_VALID_FLAG | 865 BNXT_FLOW_L2_INNER_DST_VALID_FLAG)) { 866 filter1->enables = 867 HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR | 868 L2_FILTER_ALLOC_INPUT_EN_L2_ADDR_MASK; 869 memset(filter1->l2_addr_mask, 0xff, RTE_ETHER_ADDR_LEN); 870 } 871 872 if (nf->valid_flags & BNXT_FLOW_L2_DROP_FLAG) { 873 filter1->flags |= 874 HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_DROP; 875 if (nf->ethertype == RTE_ETHER_TYPE_IPV4) { 876 /* Num VLANs for drop filter will/should be 0. 877 * If the req is memset to 0, then the count will 878 * be automatically set to 0. 879 */ 880 if (nf->valid_flags & BNXT_FLOW_PARSE_INNER_FLAG) { 881 filter1->enables |= 882 L2_FILTER_ALLOC_INPUT_EN_T_NUM_VLANS; 883 } else { 884 filter1->enables |= 885 L2_FILTER_ALLOC_INPUT_EN_NUM_VLANS; 886 filter1->flags |= 887 HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_OUTERMOST; 888 } 889 } 890 } 891 892 rc = bnxt_hwrm_set_l2_filter(bp, vnic->fw_vnic_id, 893 filter1); 894 if (rc) { 895 bnxt_free_filter(bp, filter1); 896 return NULL; 897 } 898 return filter1; 899 } 900 901 struct bnxt_filter_info * 902 bnxt_get_l2_filter(struct bnxt *bp, struct bnxt_filter_info *nf, 903 struct bnxt_vnic_info *vnic) 904 { 905 struct bnxt_filter_info *l2_filter = NULL; 906 907 l2_filter = bnxt_find_matching_l2_filter(bp, nf); 908 if (l2_filter) { 909 l2_filter->l2_ref_cnt++; 910 } else { 911 l2_filter = bnxt_create_l2_filter(bp, nf, vnic); 912 if (l2_filter) { 913 STAILQ_INSERT_TAIL(&vnic->filter, l2_filter, next); 914 l2_filter->vnic = vnic; 915 } 916 } 917 nf->matching_l2_fltr_ptr = l2_filter; 918 919 return l2_filter; 920 } 921 922 static int bnxt_vnic_prep(struct bnxt *bp, struct bnxt_vnic_info *vnic) 923 { 924 struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf; 925 uint64_t rx_offloads = dev_conf->rxmode.offloads; 926 int rc; 927 928 rc = bnxt_vnic_grp_alloc(bp, vnic); 929 if (rc) 930 goto ret; 931 932 rc = bnxt_hwrm_vnic_alloc(bp, vnic); 933 if (rc) { 934 PMD_DRV_LOG(ERR, "HWRM vnic alloc failure rc: %x\n", rc); 935 goto ret; 936 } 937 bp->nr_vnics++; 938 939 /* RSS context is required only when there is more than one RSS ring */ 940 if (vnic->rx_queue_cnt > 1) { 941 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0 /* ctx_idx 0 */); 942 if (rc) { 943 PMD_DRV_LOG(ERR, 944 "HWRM vnic ctx alloc failure: %x\n", rc); 945 goto ret; 946 } 947 } else { 948 PMD_DRV_LOG(DEBUG, "No RSS context required\n"); 949 } 950 951 if (rx_offloads & DEV_RX_OFFLOAD_VLAN_STRIP) 952 vnic->vlan_strip = true; 953 else 954 vnic->vlan_strip = false; 955 956 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 957 if (rc) 958 goto ret; 959 960 bnxt_hwrm_vnic_plcmode_cfg(bp, vnic); 961 962 ret: 963 return rc; 964 } 965 966 static int match_vnic_rss_cfg(struct bnxt *bp, 967 struct bnxt_vnic_info *vnic, 968 const struct rte_flow_action_rss *rss) 969 { 970 unsigned int match = 0, i; 971 972 if (vnic->rx_queue_cnt != rss->queue_num) 973 return -EINVAL; 974 975 for (i = 0; i < rss->queue_num; i++) { 976 if (!bp->rx_queues[rss->queue[i]]->vnic->rx_queue_cnt && 977 !bp->rx_queues[rss->queue[i]]->rx_started) 978 return -EINVAL; 979 } 980 981 for (i = 0; i < vnic->rx_queue_cnt; i++) { 982 int j; 983 984 for (j = 0; j < vnic->rx_queue_cnt; j++) { 985 if (bp->grp_info[rss->queue[i]].fw_grp_id == 986 vnic->fw_grp_ids[j]) 987 match++; 988 } 989 } 990 991 if (match != vnic->rx_queue_cnt) { 992 PMD_DRV_LOG(ERR, 993 "VNIC queue count %d vs queues matched %d\n", 994 match, vnic->rx_queue_cnt); 995 return -EINVAL; 996 } 997 998 return 0; 999 } 1000 1001 static void 1002 bnxt_update_filter_flags_en(struct bnxt_filter_info *filter, 1003 struct bnxt_filter_info *filter1, 1004 int use_ntuple) 1005 { 1006 if (!use_ntuple && 1007 !(filter->valid_flags & 1008 ~(BNXT_FLOW_L2_DST_VALID_FLAG | 1009 BNXT_FLOW_L2_SRC_VALID_FLAG | 1010 BNXT_FLOW_L2_INNER_SRC_VALID_FLAG | 1011 BNXT_FLOW_L2_INNER_DST_VALID_FLAG | 1012 BNXT_FLOW_L2_DROP_FLAG | 1013 BNXT_FLOW_PARSE_INNER_FLAG))) { 1014 filter->flags = filter1->flags; 1015 filter->enables = filter1->enables; 1016 filter->filter_type = HWRM_CFA_L2_FILTER; 1017 memcpy(filter->l2_addr, filter1->l2_addr, RTE_ETHER_ADDR_LEN); 1018 memset(filter->l2_addr_mask, 0xff, RTE_ETHER_ADDR_LEN); 1019 filter->pri_hint = filter1->pri_hint; 1020 filter->l2_filter_id_hint = filter1->l2_filter_id_hint; 1021 } 1022 filter->fw_l2_filter_id = filter1->fw_l2_filter_id; 1023 filter->l2_ref_cnt = filter1->l2_ref_cnt; 1024 filter->flow_id = filter1->flow_id; 1025 PMD_DRV_LOG(DEBUG, 1026 "l2_filter: %p fw_l2_filter_id %" PRIx64 " l2_ref_cnt %u\n", 1027 filter1, filter->fw_l2_filter_id, filter->l2_ref_cnt); 1028 } 1029 1030 static int 1031 bnxt_validate_and_parse_flow(struct rte_eth_dev *dev, 1032 const struct rte_flow_item pattern[], 1033 const struct rte_flow_action actions[], 1034 const struct rte_flow_attr *attr, 1035 struct rte_flow_error *error, 1036 struct bnxt_filter_info *filter) 1037 { 1038 const struct rte_flow_action *act = 1039 bnxt_flow_non_void_action(actions); 1040 struct bnxt *bp = dev->data->dev_private; 1041 struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf; 1042 struct bnxt_vnic_info *vnic = NULL, *vnic0 = NULL; 1043 const struct rte_flow_action_queue *act_q; 1044 const struct rte_flow_action_vf *act_vf; 1045 struct bnxt_filter_info *filter1 = NULL; 1046 const struct rte_flow_action_rss *rss; 1047 struct bnxt_rx_queue *rxq = NULL; 1048 int dflt_vnic, vnic_id; 1049 unsigned int rss_idx; 1050 uint32_t vf = 0, i; 1051 int rc, use_ntuple; 1052 1053 rc = 1054 bnxt_validate_and_parse_flow_type(bp, attr, pattern, error, filter); 1055 if (rc != 0) 1056 goto ret; 1057 1058 rc = bnxt_flow_parse_attr(attr, error); 1059 if (rc != 0) 1060 goto ret; 1061 1062 /* Since we support ingress attribute only - right now. */ 1063 if (filter->filter_type == HWRM_CFA_EM_FILTER) 1064 filter->flags = HWRM_CFA_EM_FLOW_ALLOC_INPUT_FLAGS_PATH_RX; 1065 1066 use_ntuple = bnxt_filter_type_check(pattern, error); 1067 1068 start: 1069 switch (act->type) { 1070 case RTE_FLOW_ACTION_TYPE_QUEUE: 1071 /* Allow this flow. Redirect to a VNIC. */ 1072 act_q = (const struct rte_flow_action_queue *)act->conf; 1073 if (!act_q->index || act_q->index >= bp->rx_nr_rings) { 1074 rte_flow_error_set(error, 1075 EINVAL, 1076 RTE_FLOW_ERROR_TYPE_ACTION, 1077 act, 1078 "Invalid queue ID."); 1079 rc = -rte_errno; 1080 goto ret; 1081 } 1082 PMD_DRV_LOG(DEBUG, "Queue index %d\n", act_q->index); 1083 1084 if (use_ntuple && !BNXT_RFS_NEEDS_VNIC(bp)) { 1085 filter->flags = 1086 HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_FLAGS_DEST_RFS_RING_IDX; 1087 filter->dst_id = act_q->index; 1088 goto skip_vnic_alloc; 1089 } 1090 1091 vnic_id = attr->group; 1092 if (!vnic_id) { 1093 PMD_DRV_LOG(DEBUG, "Group id is 0\n"); 1094 vnic_id = act_q->index; 1095 } 1096 1097 BNXT_VALID_VNIC_OR_RET(bp, vnic_id); 1098 1099 vnic = &bp->vnic_info[vnic_id]; 1100 if (vnic->rx_queue_cnt) { 1101 if (vnic->start_grp_id != act_q->index) { 1102 PMD_DRV_LOG(ERR, 1103 "VNIC already in use\n"); 1104 rte_flow_error_set(error, 1105 EINVAL, 1106 RTE_FLOW_ERROR_TYPE_ACTION, 1107 act, 1108 "VNIC already in use"); 1109 rc = -rte_errno; 1110 goto ret; 1111 } 1112 goto use_vnic; 1113 } 1114 1115 rxq = bp->rx_queues[act_q->index]; 1116 1117 if (!(dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS) && rxq && 1118 vnic->fw_vnic_id != INVALID_HW_RING_ID) 1119 goto use_vnic; 1120 1121 if (!rxq) { 1122 PMD_DRV_LOG(ERR, 1123 "Queue invalid or used with other VNIC\n"); 1124 rte_flow_error_set(error, 1125 EINVAL, 1126 RTE_FLOW_ERROR_TYPE_ACTION, 1127 act, 1128 "Queue invalid queue or in use"); 1129 rc = -rte_errno; 1130 goto ret; 1131 } 1132 1133 rxq->vnic = vnic; 1134 rxq->rx_started = 1; 1135 vnic->rx_queue_cnt++; 1136 vnic->start_grp_id = act_q->index; 1137 vnic->end_grp_id = act_q->index; 1138 vnic->func_default = 0; //This is not a default VNIC. 1139 1140 PMD_DRV_LOG(DEBUG, "VNIC found\n"); 1141 1142 rc = bnxt_vnic_prep(bp, vnic); 1143 if (rc) { 1144 rte_flow_error_set(error, 1145 EINVAL, 1146 RTE_FLOW_ERROR_TYPE_ACTION, 1147 act, 1148 "VNIC prep fail"); 1149 rc = -rte_errno; 1150 goto ret; 1151 } 1152 1153 PMD_DRV_LOG(DEBUG, 1154 "vnic[%d] = %p vnic->fw_grp_ids = %p\n", 1155 act_q->index, vnic, vnic->fw_grp_ids); 1156 1157 use_vnic: 1158 vnic->ff_pool_idx = vnic_id; 1159 PMD_DRV_LOG(DEBUG, 1160 "Setting vnic ff_idx %d\n", vnic->ff_pool_idx); 1161 filter->dst_id = vnic->fw_vnic_id; 1162 skip_vnic_alloc: 1163 /* For ntuple filter, create the L2 filter with default VNIC. 1164 * The user specified redirect queue will be set while creating 1165 * the ntuple filter in hardware. 1166 */ 1167 vnic0 = BNXT_GET_DEFAULT_VNIC(bp); 1168 if (use_ntuple) 1169 filter1 = bnxt_get_l2_filter(bp, filter, vnic0); 1170 else 1171 filter1 = bnxt_get_l2_filter(bp, filter, vnic); 1172 if (filter1 == NULL) { 1173 rte_flow_error_set(error, 1174 ENOSPC, 1175 RTE_FLOW_ERROR_TYPE_ACTION, 1176 act, 1177 "Filter not available"); 1178 rc = -rte_errno; 1179 goto ret; 1180 } 1181 1182 PMD_DRV_LOG(DEBUG, "new fltr: %p l2fltr: %p l2_ref_cnt: %d\n", 1183 filter, filter1, filter1->l2_ref_cnt); 1184 bnxt_update_filter_flags_en(filter, filter1, use_ntuple); 1185 break; 1186 case RTE_FLOW_ACTION_TYPE_DROP: 1187 vnic0 = &bp->vnic_info[0]; 1188 filter->dst_id = vnic0->fw_vnic_id; 1189 filter->valid_flags |= BNXT_FLOW_L2_DROP_FLAG; 1190 filter1 = bnxt_get_l2_filter(bp, filter, vnic0); 1191 if (filter1 == NULL) { 1192 rte_flow_error_set(error, 1193 ENOSPC, 1194 RTE_FLOW_ERROR_TYPE_ACTION, 1195 act, 1196 "Filter not available"); 1197 rc = -rte_errno; 1198 goto ret; 1199 } 1200 1201 if (filter->filter_type == HWRM_CFA_EM_FILTER) 1202 filter->flags = 1203 HWRM_CFA_EM_FLOW_ALLOC_INPUT_FLAGS_DROP; 1204 else if (filter->filter_type == HWRM_CFA_NTUPLE_FILTER) 1205 filter->flags = 1206 HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_FLAGS_DROP; 1207 1208 bnxt_update_filter_flags_en(filter, filter1, use_ntuple); 1209 break; 1210 case RTE_FLOW_ACTION_TYPE_COUNT: 1211 vnic0 = &bp->vnic_info[0]; 1212 filter1 = bnxt_get_l2_filter(bp, filter, vnic0); 1213 if (filter1 == NULL) { 1214 rte_flow_error_set(error, 1215 ENOSPC, 1216 RTE_FLOW_ERROR_TYPE_ACTION, 1217 act, 1218 "New filter not available"); 1219 rc = -rte_errno; 1220 goto ret; 1221 } 1222 1223 filter->fw_l2_filter_id = filter1->fw_l2_filter_id; 1224 filter->flow_id = filter1->flow_id; 1225 filter->flags = HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_FLAGS_METER; 1226 break; 1227 case RTE_FLOW_ACTION_TYPE_VF: 1228 act_vf = (const struct rte_flow_action_vf *)act->conf; 1229 vf = act_vf->id; 1230 1231 if (filter->tunnel_type == 1232 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_VXLAN || 1233 filter->tunnel_type == 1234 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_IPGRE) { 1235 /* If issued on a VF, ensure id is 0 and is trusted */ 1236 if (BNXT_VF(bp)) { 1237 if (!BNXT_VF_IS_TRUSTED(bp) || vf) { 1238 rte_flow_error_set(error, EINVAL, 1239 RTE_FLOW_ERROR_TYPE_ACTION, 1240 act, 1241 "Incorrect VF"); 1242 rc = -rte_errno; 1243 goto ret; 1244 } 1245 } 1246 1247 filter->enables |= filter->tunnel_type; 1248 filter->filter_type = HWRM_CFA_TUNNEL_REDIRECT_FILTER; 1249 goto done; 1250 } 1251 1252 if (vf >= bp->pdev->max_vfs) { 1253 rte_flow_error_set(error, 1254 EINVAL, 1255 RTE_FLOW_ERROR_TYPE_ACTION, 1256 act, 1257 "Incorrect VF id!"); 1258 rc = -rte_errno; 1259 goto ret; 1260 } 1261 1262 filter->mirror_vnic_id = 1263 dflt_vnic = bnxt_hwrm_func_qcfg_vf_dflt_vnic_id(bp, vf); 1264 if (dflt_vnic < 0) { 1265 /* This simply indicates there's no driver loaded. 1266 * This is not an error. 1267 */ 1268 rte_flow_error_set(error, 1269 EINVAL, 1270 RTE_FLOW_ERROR_TYPE_ACTION, 1271 act, 1272 "Unable to get default VNIC for VF"); 1273 rc = -rte_errno; 1274 goto ret; 1275 } 1276 1277 filter->mirror_vnic_id = dflt_vnic; 1278 filter->enables |= NTUPLE_FLTR_ALLOC_INPUT_EN_MIRROR_VNIC_ID; 1279 1280 vnic0 = &bp->vnic_info[0]; 1281 filter1 = bnxt_get_l2_filter(bp, filter, vnic0); 1282 if (filter1 == NULL) { 1283 rte_flow_error_set(error, 1284 ENOSPC, 1285 RTE_FLOW_ERROR_TYPE_ACTION, 1286 act, 1287 "New filter not available"); 1288 rc = -rte_errno; 1289 goto ret; 1290 } 1291 1292 filter->fw_l2_filter_id = filter1->fw_l2_filter_id; 1293 filter->flow_id = filter1->flow_id; 1294 break; 1295 case RTE_FLOW_ACTION_TYPE_RSS: 1296 rss = (const struct rte_flow_action_rss *)act->conf; 1297 1298 vnic_id = attr->group; 1299 1300 BNXT_VALID_VNIC_OR_RET(bp, vnic_id); 1301 vnic = &bp->vnic_info[vnic_id]; 1302 1303 /* Check if requested RSS config matches RSS config of VNIC 1304 * only if it is not a fresh VNIC configuration. 1305 * Otherwise the existing VNIC configuration can be used. 1306 */ 1307 if (vnic->rx_queue_cnt) { 1308 rc = match_vnic_rss_cfg(bp, vnic, rss); 1309 if (rc) { 1310 PMD_DRV_LOG(ERR, 1311 "VNIC and RSS config mismatch\n"); 1312 rte_flow_error_set(error, 1313 EINVAL, 1314 RTE_FLOW_ERROR_TYPE_ACTION, 1315 act, 1316 "VNIC and RSS cfg mismatch"); 1317 rc = -rte_errno; 1318 goto ret; 1319 } 1320 goto vnic_found; 1321 } 1322 1323 for (i = 0; i < rss->queue_num; i++) { 1324 PMD_DRV_LOG(DEBUG, "RSS action Queue %d\n", 1325 rss->queue[i]); 1326 1327 if (!rss->queue[i] || 1328 rss->queue[i] >= bp->rx_nr_rings || 1329 !bp->rx_queues[rss->queue[i]]) { 1330 rte_flow_error_set(error, 1331 EINVAL, 1332 RTE_FLOW_ERROR_TYPE_ACTION, 1333 act, 1334 "Invalid queue ID for RSS"); 1335 rc = -rte_errno; 1336 goto ret; 1337 } 1338 rxq = bp->rx_queues[rss->queue[i]]; 1339 1340 if (bp->vnic_info[0].fw_grp_ids[rss->queue[i]] != 1341 INVALID_HW_RING_ID) { 1342 PMD_DRV_LOG(ERR, 1343 "queue active with other VNIC\n"); 1344 rte_flow_error_set(error, 1345 EINVAL, 1346 RTE_FLOW_ERROR_TYPE_ACTION, 1347 act, 1348 "Invalid queue ID for RSS"); 1349 rc = -rte_errno; 1350 goto ret; 1351 } 1352 1353 rxq->vnic = vnic; 1354 rxq->rx_started = 1; 1355 vnic->rx_queue_cnt++; 1356 } 1357 1358 vnic->start_grp_id = rss->queue[0]; 1359 vnic->end_grp_id = rss->queue[rss->queue_num - 1]; 1360 vnic->func_default = 0; //This is not a default VNIC. 1361 1362 rc = bnxt_vnic_prep(bp, vnic); 1363 if (rc) { 1364 rte_flow_error_set(error, 1365 EINVAL, 1366 RTE_FLOW_ERROR_TYPE_ACTION, 1367 act, 1368 "VNIC prep fail"); 1369 rc = -rte_errno; 1370 goto ret; 1371 } 1372 1373 PMD_DRV_LOG(DEBUG, 1374 "vnic[%d] = %p vnic->fw_grp_ids = %p\n", 1375 vnic_id, vnic, vnic->fw_grp_ids); 1376 1377 vnic->ff_pool_idx = vnic_id; 1378 PMD_DRV_LOG(DEBUG, 1379 "Setting vnic ff_pool_idx %d\n", vnic->ff_pool_idx); 1380 1381 /* This can be done only after vnic_grp_alloc is done. */ 1382 for (i = 0; i < vnic->rx_queue_cnt; i++) { 1383 vnic->fw_grp_ids[i] = 1384 bp->grp_info[rss->queue[i]].fw_grp_id; 1385 /* Make sure vnic0 does not use these rings. */ 1386 bp->vnic_info[0].fw_grp_ids[rss->queue[i]] = 1387 INVALID_HW_RING_ID; 1388 } 1389 1390 for (rss_idx = 0; rss_idx < HW_HASH_INDEX_SIZE; ) { 1391 for (i = 0; i < vnic->rx_queue_cnt; i++) 1392 vnic->rss_table[rss_idx++] = 1393 vnic->fw_grp_ids[i]; 1394 } 1395 1396 /* Configure RSS only if the queue count is > 1 */ 1397 if (vnic->rx_queue_cnt > 1) { 1398 vnic->hash_type = 1399 bnxt_rte_to_hwrm_hash_types(rss->types); 1400 vnic->hash_mode = 1401 bnxt_rte_to_hwrm_hash_level(bp, rss->types, rss->level); 1402 1403 if (!rss->key_len) { 1404 /* If hash key has not been specified, 1405 * use random hash key. 1406 */ 1407 prandom_bytes(vnic->rss_hash_key, 1408 HW_HASH_KEY_SIZE); 1409 } else { 1410 if (rss->key_len > HW_HASH_KEY_SIZE) 1411 memcpy(vnic->rss_hash_key, 1412 rss->key, 1413 HW_HASH_KEY_SIZE); 1414 else 1415 memcpy(vnic->rss_hash_key, 1416 rss->key, 1417 rss->key_len); 1418 } 1419 bnxt_hwrm_vnic_rss_cfg(bp, vnic); 1420 } else { 1421 PMD_DRV_LOG(DEBUG, "No RSS config required\n"); 1422 } 1423 1424 vnic_found: 1425 filter->dst_id = vnic->fw_vnic_id; 1426 filter1 = bnxt_get_l2_filter(bp, filter, vnic); 1427 if (filter1 == NULL) { 1428 rte_flow_error_set(error, 1429 ENOSPC, 1430 RTE_FLOW_ERROR_TYPE_ACTION, 1431 act, 1432 "New filter not available"); 1433 rc = -rte_errno; 1434 goto ret; 1435 } 1436 1437 PMD_DRV_LOG(DEBUG, "L2 filter created\n"); 1438 bnxt_update_filter_flags_en(filter, filter1, use_ntuple); 1439 break; 1440 case RTE_FLOW_ACTION_TYPE_MARK: 1441 if (bp->mark_table == NULL) { 1442 rte_flow_error_set(error, 1443 ENOMEM, 1444 RTE_FLOW_ERROR_TYPE_ACTION, 1445 act, 1446 "Mark table not allocated."); 1447 rc = -rte_errno; 1448 goto ret; 1449 } 1450 1451 if (bp->flags & BNXT_FLAG_RX_VECTOR_PKT_MODE) { 1452 PMD_DRV_LOG(DEBUG, 1453 "Disabling vector processing for mark\n"); 1454 bp->eth_dev->rx_pkt_burst = bnxt_recv_pkts; 1455 bp->flags &= ~BNXT_FLAG_RX_VECTOR_PKT_MODE; 1456 } 1457 1458 filter->valid_flags |= BNXT_FLOW_MARK_FLAG; 1459 filter->mark = ((const struct rte_flow_action_mark *) 1460 act->conf)->id; 1461 PMD_DRV_LOG(DEBUG, "Mark the flow %d\n", filter->mark); 1462 break; 1463 default: 1464 rte_flow_error_set(error, 1465 EINVAL, 1466 RTE_FLOW_ERROR_TYPE_ACTION, 1467 act, 1468 "Invalid action."); 1469 rc = -rte_errno; 1470 goto ret; 1471 } 1472 1473 done: 1474 act = bnxt_flow_non_void_action(++act); 1475 while (act->type != RTE_FLOW_ACTION_TYPE_END) 1476 goto start; 1477 1478 return rc; 1479 ret: 1480 1481 if (filter1) { 1482 bnxt_hwrm_clear_l2_filter(bp, filter1); 1483 bnxt_free_filter(bp, filter1); 1484 } 1485 1486 if (rte_errno) { 1487 if (vnic && STAILQ_EMPTY(&vnic->filter)) 1488 vnic->rx_queue_cnt = 0; 1489 1490 if (rxq && !vnic->rx_queue_cnt) 1491 rxq->vnic = &bp->vnic_info[0]; 1492 } 1493 return -rte_errno; 1494 } 1495 1496 static 1497 struct bnxt_vnic_info *find_matching_vnic(struct bnxt *bp, 1498 struct bnxt_filter_info *filter) 1499 { 1500 struct bnxt_vnic_info *vnic = NULL; 1501 unsigned int i; 1502 1503 for (i = 0; i < bp->max_vnics; i++) { 1504 vnic = &bp->vnic_info[i]; 1505 if (vnic->fw_vnic_id != INVALID_VNIC_ID && 1506 filter->dst_id == vnic->fw_vnic_id) { 1507 PMD_DRV_LOG(DEBUG, "Found matching VNIC Id %d\n", 1508 vnic->ff_pool_idx); 1509 return vnic; 1510 } 1511 } 1512 return NULL; 1513 } 1514 1515 static int 1516 bnxt_flow_validate(struct rte_eth_dev *dev, 1517 const struct rte_flow_attr *attr, 1518 const struct rte_flow_item pattern[], 1519 const struct rte_flow_action actions[], 1520 struct rte_flow_error *error) 1521 { 1522 struct bnxt *bp = dev->data->dev_private; 1523 struct bnxt_vnic_info *vnic = NULL; 1524 struct bnxt_filter_info *filter; 1525 int ret = 0; 1526 1527 bnxt_acquire_flow_lock(bp); 1528 ret = bnxt_flow_args_validate(attr, pattern, actions, error); 1529 if (ret != 0) { 1530 bnxt_release_flow_lock(bp); 1531 return ret; 1532 } 1533 1534 filter = bnxt_get_unused_filter(bp); 1535 if (filter == NULL) { 1536 PMD_DRV_LOG(ERR, "Not enough resources for a new flow.\n"); 1537 bnxt_release_flow_lock(bp); 1538 return -ENOMEM; 1539 } 1540 1541 ret = bnxt_validate_and_parse_flow(dev, pattern, actions, attr, 1542 error, filter); 1543 if (ret) 1544 goto exit; 1545 1546 vnic = find_matching_vnic(bp, filter); 1547 if (vnic) { 1548 if (STAILQ_EMPTY(&vnic->filter)) { 1549 rte_free(vnic->fw_grp_ids); 1550 bnxt_hwrm_vnic_ctx_free(bp, vnic); 1551 bnxt_hwrm_vnic_free(bp, vnic); 1552 vnic->rx_queue_cnt = 0; 1553 PMD_DRV_LOG(DEBUG, "Free VNIC\n"); 1554 } 1555 } 1556 1557 if (filter->filter_type == HWRM_CFA_EM_FILTER) 1558 bnxt_hwrm_clear_em_filter(bp, filter); 1559 else if (filter->filter_type == HWRM_CFA_NTUPLE_FILTER) 1560 bnxt_hwrm_clear_ntuple_filter(bp, filter); 1561 else 1562 bnxt_hwrm_clear_l2_filter(bp, filter); 1563 1564 exit: 1565 /* No need to hold on to this filter if we are just validating flow */ 1566 bnxt_free_filter(bp, filter); 1567 bnxt_release_flow_lock(bp); 1568 1569 return ret; 1570 } 1571 1572 static void 1573 bnxt_update_filter(struct bnxt *bp, struct bnxt_filter_info *old_filter, 1574 struct bnxt_filter_info *new_filter) 1575 { 1576 /* Clear the new L2 filter that was created in the previous step in 1577 * bnxt_validate_and_parse_flow. For L2 filters, we will use the new 1578 * filter which points to the new destination queue and so we clear 1579 * the previous L2 filter. For ntuple filters, we are going to reuse 1580 * the old L2 filter and create new NTUPLE filter with this new 1581 * destination queue subsequently during bnxt_flow_create. So we 1582 * decrement the ref cnt of the L2 filter that would've been bumped 1583 * up previously in bnxt_validate_and_parse_flow as the old n-tuple 1584 * filter that was referencing it will be deleted now. 1585 */ 1586 bnxt_hwrm_clear_l2_filter(bp, old_filter); 1587 if (new_filter->filter_type == HWRM_CFA_L2_FILTER) { 1588 bnxt_hwrm_set_l2_filter(bp, new_filter->dst_id, new_filter); 1589 } else { 1590 if (new_filter->filter_type == HWRM_CFA_EM_FILTER) 1591 bnxt_hwrm_clear_em_filter(bp, old_filter); 1592 if (new_filter->filter_type == HWRM_CFA_NTUPLE_FILTER) 1593 bnxt_hwrm_clear_ntuple_filter(bp, old_filter); 1594 } 1595 } 1596 1597 static int 1598 bnxt_match_filter(struct bnxt *bp, struct bnxt_filter_info *nf) 1599 { 1600 struct bnxt_filter_info *mf; 1601 struct rte_flow *flow; 1602 int i; 1603 1604 for (i = bp->max_vnics - 1; i >= 0; i--) { 1605 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 1606 1607 if (vnic->fw_vnic_id == INVALID_VNIC_ID) 1608 continue; 1609 1610 STAILQ_FOREACH(flow, &vnic->flow_list, next) { 1611 mf = flow->filter; 1612 1613 if (mf->filter_type == nf->filter_type && 1614 mf->flags == nf->flags && 1615 mf->src_port == nf->src_port && 1616 mf->src_port_mask == nf->src_port_mask && 1617 mf->dst_port == nf->dst_port && 1618 mf->dst_port_mask == nf->dst_port_mask && 1619 mf->ip_protocol == nf->ip_protocol && 1620 mf->ip_addr_type == nf->ip_addr_type && 1621 mf->ethertype == nf->ethertype && 1622 mf->vni == nf->vni && 1623 mf->tunnel_type == nf->tunnel_type && 1624 mf->l2_ovlan == nf->l2_ovlan && 1625 mf->l2_ovlan_mask == nf->l2_ovlan_mask && 1626 mf->l2_ivlan == nf->l2_ivlan && 1627 mf->l2_ivlan_mask == nf->l2_ivlan_mask && 1628 !memcmp(mf->l2_addr, nf->l2_addr, 1629 RTE_ETHER_ADDR_LEN) && 1630 !memcmp(mf->l2_addr_mask, nf->l2_addr_mask, 1631 RTE_ETHER_ADDR_LEN) && 1632 !memcmp(mf->src_macaddr, nf->src_macaddr, 1633 RTE_ETHER_ADDR_LEN) && 1634 !memcmp(mf->dst_macaddr, nf->dst_macaddr, 1635 RTE_ETHER_ADDR_LEN) && 1636 !memcmp(mf->src_ipaddr, nf->src_ipaddr, 1637 sizeof(nf->src_ipaddr)) && 1638 !memcmp(mf->src_ipaddr_mask, nf->src_ipaddr_mask, 1639 sizeof(nf->src_ipaddr_mask)) && 1640 !memcmp(mf->dst_ipaddr, nf->dst_ipaddr, 1641 sizeof(nf->dst_ipaddr)) && 1642 !memcmp(mf->dst_ipaddr_mask, nf->dst_ipaddr_mask, 1643 sizeof(nf->dst_ipaddr_mask))) { 1644 if (mf->dst_id == nf->dst_id) 1645 return -EEXIST; 1646 /* Free the old filter, update flow 1647 * with new filter 1648 */ 1649 bnxt_update_filter(bp, mf, nf); 1650 STAILQ_REMOVE(&vnic->filter, mf, 1651 bnxt_filter_info, next); 1652 STAILQ_INSERT_TAIL(&vnic->filter, nf, next); 1653 bnxt_free_filter(bp, mf); 1654 flow->filter = nf; 1655 return -EXDEV; 1656 } 1657 } 1658 } 1659 return 0; 1660 } 1661 1662 static void 1663 bnxt_setup_flow_counter(struct bnxt *bp) 1664 { 1665 if (bp->fw_cap & BNXT_FW_CAP_ADV_FLOW_COUNTERS && 1666 !(bp->flags & BNXT_FLAG_FC_THREAD) && BNXT_FLOW_XSTATS_EN(bp)) { 1667 rte_eal_alarm_set(US_PER_S * BNXT_FC_TIMER, 1668 bnxt_flow_cnt_alarm_cb, 1669 (void *)bp); 1670 bp->flags |= BNXT_FLAG_FC_THREAD; 1671 } 1672 } 1673 1674 void bnxt_flow_cnt_alarm_cb(void *arg) 1675 { 1676 int rc = 0; 1677 struct bnxt *bp = arg; 1678 1679 if (!bp->flow_stat->rx_fc_out_tbl.va) { 1680 PMD_DRV_LOG(ERR, "bp->flow_stat->rx_fc_out_tbl.va is NULL?\n"); 1681 bnxt_cancel_fc_thread(bp); 1682 return; 1683 } 1684 1685 if (!bp->flow_stat->flow_count) { 1686 bnxt_cancel_fc_thread(bp); 1687 return; 1688 } 1689 1690 if (!bp->eth_dev->data->dev_started) { 1691 bnxt_cancel_fc_thread(bp); 1692 return; 1693 } 1694 1695 rc = bnxt_flow_stats_req(bp); 1696 if (rc) { 1697 PMD_DRV_LOG(ERR, "Flow stat alarm not rescheduled.\n"); 1698 return; 1699 } 1700 1701 rte_eal_alarm_set(US_PER_S * BNXT_FC_TIMER, 1702 bnxt_flow_cnt_alarm_cb, 1703 (void *)bp); 1704 } 1705 1706 1707 static struct rte_flow * 1708 bnxt_flow_create(struct rte_eth_dev *dev, 1709 const struct rte_flow_attr *attr, 1710 const struct rte_flow_item pattern[], 1711 const struct rte_flow_action actions[], 1712 struct rte_flow_error *error) 1713 { 1714 struct bnxt *bp = dev->data->dev_private; 1715 struct bnxt_vnic_info *vnic = NULL; 1716 struct bnxt_filter_info *filter; 1717 bool update_flow = false; 1718 struct rte_flow *flow; 1719 int ret = 0; 1720 uint32_t tun_type, flow_id; 1721 1722 if (BNXT_VF(bp) && !BNXT_VF_IS_TRUSTED(bp)) { 1723 rte_flow_error_set(error, EINVAL, 1724 RTE_FLOW_ERROR_TYPE_HANDLE, NULL, 1725 "Failed to create flow, Not a Trusted VF!"); 1726 return NULL; 1727 } 1728 1729 if (!dev->data->dev_started) { 1730 rte_flow_error_set(error, 1731 EINVAL, 1732 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, 1733 NULL, 1734 "Device must be started"); 1735 return NULL; 1736 } 1737 1738 flow = rte_zmalloc("bnxt_flow", sizeof(struct rte_flow), 0); 1739 if (!flow) { 1740 rte_flow_error_set(error, ENOMEM, 1741 RTE_FLOW_ERROR_TYPE_HANDLE, NULL, 1742 "Failed to allocate memory"); 1743 return flow; 1744 } 1745 1746 bnxt_acquire_flow_lock(bp); 1747 ret = bnxt_flow_args_validate(attr, pattern, actions, error); 1748 if (ret != 0) { 1749 PMD_DRV_LOG(ERR, "Not a validate flow.\n"); 1750 goto free_flow; 1751 } 1752 1753 filter = bnxt_get_unused_filter(bp); 1754 if (filter == NULL) { 1755 rte_flow_error_set(error, ENOSPC, 1756 RTE_FLOW_ERROR_TYPE_HANDLE, NULL, 1757 "Not enough resources for a new flow"); 1758 goto free_flow; 1759 } 1760 1761 ret = bnxt_validate_and_parse_flow(dev, pattern, actions, attr, 1762 error, filter); 1763 if (ret != 0) 1764 goto free_filter; 1765 1766 ret = bnxt_match_filter(bp, filter); 1767 if (ret == -EEXIST) { 1768 PMD_DRV_LOG(DEBUG, "Flow already exists.\n"); 1769 /* Clear the filter that was created as part of 1770 * validate_and_parse_flow() above 1771 */ 1772 bnxt_hwrm_clear_l2_filter(bp, filter); 1773 goto free_filter; 1774 } else if (ret == -EXDEV) { 1775 PMD_DRV_LOG(DEBUG, "Flow with same pattern exists\n"); 1776 PMD_DRV_LOG(DEBUG, "Updating with different destination\n"); 1777 update_flow = true; 1778 } 1779 1780 /* If tunnel redirection to a VF/PF is specified then only tunnel_type 1781 * is set and enable is set to the tunnel type. Issue hwrm cmd directly 1782 * in such a case. 1783 */ 1784 if (filter->filter_type == HWRM_CFA_TUNNEL_REDIRECT_FILTER && 1785 filter->enables == filter->tunnel_type) { 1786 ret = bnxt_hwrm_tunnel_redirect_query(bp, &tun_type); 1787 if (ret) { 1788 rte_flow_error_set(error, -ret, 1789 RTE_FLOW_ERROR_TYPE_HANDLE, NULL, 1790 "Unable to query tunnel to VF"); 1791 goto free_filter; 1792 } 1793 if (tun_type == (1U << filter->tunnel_type)) { 1794 ret = 1795 bnxt_hwrm_tunnel_redirect_free(bp, 1796 filter->tunnel_type); 1797 if (ret) { 1798 PMD_DRV_LOG(ERR, 1799 "Unable to free existing tunnel\n"); 1800 rte_flow_error_set(error, -ret, 1801 RTE_FLOW_ERROR_TYPE_HANDLE, 1802 NULL, 1803 "Unable to free preexisting " 1804 "tunnel on VF"); 1805 goto free_filter; 1806 } 1807 } 1808 ret = bnxt_hwrm_tunnel_redirect(bp, filter->tunnel_type); 1809 if (ret) { 1810 rte_flow_error_set(error, -ret, 1811 RTE_FLOW_ERROR_TYPE_HANDLE, NULL, 1812 "Unable to redirect tunnel to VF"); 1813 goto free_filter; 1814 } 1815 vnic = &bp->vnic_info[0]; 1816 goto done; 1817 } 1818 1819 if (filter->filter_type == HWRM_CFA_EM_FILTER) { 1820 filter->enables |= 1821 HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_L2_FILTER_ID; 1822 ret = bnxt_hwrm_set_em_filter(bp, filter->dst_id, filter); 1823 if (ret != 0) { 1824 rte_flow_error_set(error, -ret, 1825 RTE_FLOW_ERROR_TYPE_HANDLE, NULL, 1826 "Failed to create EM filter"); 1827 goto free_filter; 1828 } 1829 } 1830 1831 if (filter->filter_type == HWRM_CFA_NTUPLE_FILTER) { 1832 filter->enables |= 1833 HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_L2_FILTER_ID; 1834 ret = bnxt_hwrm_set_ntuple_filter(bp, filter->dst_id, filter); 1835 if (ret != 0) { 1836 rte_flow_error_set(error, -ret, 1837 RTE_FLOW_ERROR_TYPE_HANDLE, NULL, 1838 "Failed to create ntuple filter"); 1839 goto free_filter; 1840 } 1841 } 1842 1843 if (BNXT_RFS_NEEDS_VNIC(bp)) 1844 vnic = find_matching_vnic(bp, filter); 1845 else 1846 vnic = BNXT_GET_DEFAULT_VNIC(bp); 1847 done: 1848 if (!ret || update_flow) { 1849 flow->filter = filter; 1850 flow->vnic = vnic; 1851 if (update_flow) { 1852 ret = -EXDEV; 1853 goto free_flow; 1854 } 1855 1856 if (filter->valid_flags & BNXT_FLOW_MARK_FLAG) { 1857 PMD_DRV_LOG(DEBUG, 1858 "Mark action: mark id 0x%x, flow id 0x%x\n", 1859 filter->mark, filter->flow_id); 1860 1861 /* TCAM and EM should be 16-bit only. 1862 * Other modes not supported. 1863 */ 1864 flow_id = filter->flow_id & BNXT_FLOW_ID_MASK; 1865 if (bp->mark_table[flow_id].valid) { 1866 rte_flow_error_set(error, EEXIST, 1867 RTE_FLOW_ERROR_TYPE_HANDLE, 1868 NULL, 1869 "Flow with mark id exists"); 1870 bnxt_clear_one_vnic_filter(bp, filter); 1871 goto free_filter; 1872 } 1873 bp->mark_table[flow_id].valid = true; 1874 bp->mark_table[flow_id].mark_id = filter->mark; 1875 } 1876 1877 STAILQ_INSERT_TAIL(&vnic->filter, filter, next); 1878 STAILQ_INSERT_TAIL(&vnic->flow_list, flow, next); 1879 1880 if (BNXT_FLOW_XSTATS_EN(bp)) 1881 bp->flow_stat->flow_count++; 1882 bnxt_release_flow_lock(bp); 1883 bnxt_setup_flow_counter(bp); 1884 PMD_DRV_LOG(DEBUG, "Successfully created flow.\n"); 1885 return flow; 1886 } 1887 1888 free_filter: 1889 bnxt_free_filter(bp, filter); 1890 free_flow: 1891 if (ret == -EEXIST) 1892 rte_flow_error_set(error, ret, 1893 RTE_FLOW_ERROR_TYPE_HANDLE, NULL, 1894 "Matching Flow exists."); 1895 else if (ret == -EXDEV) 1896 rte_flow_error_set(error, 0, 1897 RTE_FLOW_ERROR_TYPE_NONE, NULL, 1898 "Flow with pattern exists, updating destination queue"); 1899 else if (!rte_errno) 1900 rte_flow_error_set(error, -ret, 1901 RTE_FLOW_ERROR_TYPE_HANDLE, NULL, 1902 "Failed to create flow."); 1903 rte_free(flow); 1904 flow = NULL; 1905 bnxt_release_flow_lock(bp); 1906 return flow; 1907 } 1908 1909 static int bnxt_handle_tunnel_redirect_destroy(struct bnxt *bp, 1910 struct bnxt_filter_info *filter, 1911 struct rte_flow_error *error) 1912 { 1913 uint16_t tun_dst_fid; 1914 uint32_t tun_type; 1915 int ret = 0; 1916 1917 ret = bnxt_hwrm_tunnel_redirect_query(bp, &tun_type); 1918 if (ret) { 1919 rte_flow_error_set(error, -ret, 1920 RTE_FLOW_ERROR_TYPE_HANDLE, NULL, 1921 "Unable to query tunnel to VF"); 1922 return ret; 1923 } 1924 if (tun_type == (1U << filter->tunnel_type)) { 1925 ret = bnxt_hwrm_tunnel_redirect_info(bp, filter->tunnel_type, 1926 &tun_dst_fid); 1927 if (ret) { 1928 rte_flow_error_set(error, -ret, 1929 RTE_FLOW_ERROR_TYPE_HANDLE, 1930 NULL, 1931 "tunnel_redirect info cmd fail"); 1932 return ret; 1933 } 1934 PMD_DRV_LOG(INFO, "Pre-existing tunnel fid = %x vf->fid = %x\n", 1935 tun_dst_fid + bp->first_vf_id, bp->fw_fid); 1936 1937 /* Tunnel doesn't belong to this VF, so don't send HWRM 1938 * cmd, just delete the flow from driver 1939 */ 1940 if (bp->fw_fid != (tun_dst_fid + bp->first_vf_id)) 1941 PMD_DRV_LOG(ERR, 1942 "Tunnel does not belong to this VF, skip hwrm_tunnel_redirect_free\n"); 1943 else 1944 ret = bnxt_hwrm_tunnel_redirect_free(bp, 1945 filter->tunnel_type); 1946 } 1947 return ret; 1948 } 1949 1950 static int 1951 _bnxt_flow_destroy(struct bnxt *bp, 1952 struct rte_flow *flow, 1953 struct rte_flow_error *error) 1954 { 1955 struct bnxt_filter_info *filter; 1956 struct bnxt_vnic_info *vnic; 1957 int ret = 0; 1958 uint32_t flow_id; 1959 1960 filter = flow->filter; 1961 vnic = flow->vnic; 1962 1963 if (filter->filter_type == HWRM_CFA_TUNNEL_REDIRECT_FILTER && 1964 filter->enables == filter->tunnel_type) { 1965 ret = bnxt_handle_tunnel_redirect_destroy(bp, filter, error); 1966 if (!ret) 1967 goto done; 1968 else 1969 return ret; 1970 } 1971 1972 ret = bnxt_match_filter(bp, filter); 1973 if (ret == 0) 1974 PMD_DRV_LOG(ERR, "Could not find matching flow\n"); 1975 1976 if (filter->valid_flags & BNXT_FLOW_MARK_FLAG) { 1977 flow_id = filter->flow_id & BNXT_FLOW_ID_MASK; 1978 memset(&bp->mark_table[flow_id], 0, 1979 sizeof(bp->mark_table[flow_id])); 1980 filter->flow_id = 0; 1981 } 1982 1983 ret = bnxt_clear_one_vnic_filter(bp, filter); 1984 1985 done: 1986 if (!ret) { 1987 /* If it is a L2 drop filter, when the filter is created, 1988 * the FW updates the BC/MC records. 1989 * Once this filter is removed, issue the set_rx_mask command 1990 * to reset the BC/MC records in the HW to the settings 1991 * before the drop counter is created. 1992 */ 1993 if (filter->valid_flags & BNXT_FLOW_L2_DROP_FLAG) 1994 bnxt_set_rx_mask_no_vlan(bp, &bp->vnic_info[0]); 1995 1996 STAILQ_REMOVE(&vnic->filter, filter, bnxt_filter_info, next); 1997 bnxt_free_filter(bp, filter); 1998 STAILQ_REMOVE(&vnic->flow_list, flow, rte_flow, next); 1999 rte_free(flow); 2000 if (BNXT_FLOW_XSTATS_EN(bp)) 2001 bp->flow_stat->flow_count--; 2002 2003 /* If this was the last flow associated with this vnic, 2004 * switch the queue back to RSS pool. 2005 */ 2006 if (vnic && !vnic->func_default && 2007 STAILQ_EMPTY(&vnic->flow_list)) { 2008 rte_free(vnic->fw_grp_ids); 2009 if (vnic->rx_queue_cnt > 1) 2010 bnxt_hwrm_vnic_ctx_free(bp, vnic); 2011 2012 bnxt_hwrm_vnic_free(bp, vnic); 2013 vnic->rx_queue_cnt = 0; 2014 } 2015 } else { 2016 rte_flow_error_set(error, -ret, 2017 RTE_FLOW_ERROR_TYPE_HANDLE, NULL, 2018 "Failed to destroy flow."); 2019 } 2020 2021 return ret; 2022 } 2023 2024 static int 2025 bnxt_flow_destroy(struct rte_eth_dev *dev, 2026 struct rte_flow *flow, 2027 struct rte_flow_error *error) 2028 { 2029 struct bnxt *bp = dev->data->dev_private; 2030 int ret = 0; 2031 2032 bnxt_acquire_flow_lock(bp); 2033 if (!flow) { 2034 rte_flow_error_set(error, EINVAL, 2035 RTE_FLOW_ERROR_TYPE_HANDLE, NULL, 2036 "Invalid flow: failed to destroy flow."); 2037 bnxt_release_flow_lock(bp); 2038 return -EINVAL; 2039 } 2040 2041 if (!flow->filter) { 2042 rte_flow_error_set(error, EINVAL, 2043 RTE_FLOW_ERROR_TYPE_HANDLE, NULL, 2044 "Invalid flow: failed to destroy flow."); 2045 bnxt_release_flow_lock(bp); 2046 return -EINVAL; 2047 } 2048 ret = _bnxt_flow_destroy(bp, flow, error); 2049 bnxt_release_flow_lock(bp); 2050 2051 return ret; 2052 } 2053 2054 void bnxt_cancel_fc_thread(struct bnxt *bp) 2055 { 2056 bp->flags &= ~BNXT_FLAG_FC_THREAD; 2057 rte_eal_alarm_cancel(bnxt_flow_cnt_alarm_cb, (void *)bp); 2058 } 2059 2060 static int 2061 bnxt_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error) 2062 { 2063 struct bnxt *bp = dev->data->dev_private; 2064 struct bnxt_vnic_info *vnic; 2065 struct rte_flow *flow; 2066 unsigned int i; 2067 int ret = 0; 2068 2069 bnxt_acquire_flow_lock(bp); 2070 for (i = 0; i < bp->max_vnics; i++) { 2071 vnic = &bp->vnic_info[i]; 2072 if (vnic && vnic->fw_vnic_id == INVALID_VNIC_ID) 2073 continue; 2074 2075 while (!STAILQ_EMPTY(&vnic->flow_list)) { 2076 flow = STAILQ_FIRST(&vnic->flow_list); 2077 2078 if (!flow->filter) 2079 continue; 2080 2081 ret = _bnxt_flow_destroy(bp, flow, error); 2082 if (ret) 2083 break; 2084 } 2085 } 2086 2087 bnxt_cancel_fc_thread(bp); 2088 bnxt_release_flow_lock(bp); 2089 2090 return ret; 2091 } 2092 2093 const struct rte_flow_ops bnxt_flow_ops = { 2094 .validate = bnxt_flow_validate, 2095 .create = bnxt_flow_create, 2096 .destroy = bnxt_flow_destroy, 2097 .flush = bnxt_flow_flush, 2098 }; 2099