1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * MAC Services Module - misc utilities 28 */ 29 30 #include <sys/types.h> 31 #include <sys/mac.h> 32 #include <sys/mac_impl.h> 33 #include <sys/mac_client_priv.h> 34 #include <sys/mac_client_impl.h> 35 #include <sys/mac_soft_ring.h> 36 #include <sys/strsubr.h> 37 #include <sys/strsun.h> 38 #include <sys/vlan.h> 39 #include <sys/pattr.h> 40 #include <sys/pci_tools.h> 41 #include <inet/ip.h> 42 #include <inet/ip_impl.h> 43 #include <inet/ip6.h> 44 #include <sys/vtrace.h> 45 #include <sys/dlpi.h> 46 #include <sys/sunndi.h> 47 #include <inet/ipsec_impl.h> 48 #include <inet/sadb.h> 49 #include <inet/ipsecesp.h> 50 #include <inet/ipsecah.h> 51 52 /* 53 * Copy an mblk, preserving its hardware checksum flags. 54 */ 55 static mblk_t * 56 mac_copymsg_cksum(mblk_t *mp) 57 { 58 mblk_t *mp1; 59 uint32_t start, stuff, end, value, flags; 60 61 mp1 = copymsg(mp); 62 if (mp1 == NULL) 63 return (NULL); 64 65 hcksum_retrieve(mp, NULL, NULL, &start, &stuff, &end, &value, &flags); 66 (void) hcksum_assoc(mp1, NULL, NULL, start, stuff, end, value, 67 flags, KM_NOSLEEP); 68 69 return (mp1); 70 } 71 72 /* 73 * Copy an mblk chain, presenting the hardware checksum flags of the 74 * individual mblks. 75 */ 76 mblk_t * 77 mac_copymsgchain_cksum(mblk_t *mp) 78 { 79 mblk_t *nmp = NULL; 80 mblk_t **nmpp = &nmp; 81 82 for (; mp != NULL; mp = mp->b_next) { 83 if ((*nmpp = mac_copymsg_cksum(mp)) == NULL) { 84 freemsgchain(nmp); 85 return (NULL); 86 } 87 88 nmpp = &((*nmpp)->b_next); 89 } 90 91 return (nmp); 92 } 93 94 /* 95 * Process the specified mblk chain for proper handling of hardware 96 * checksum offload. This routine is invoked for loopback traffic 97 * between MAC clients. 98 * The function handles a NULL mblk chain passed as argument. 99 */ 100 mblk_t * 101 mac_fix_cksum(mblk_t *mp_chain) 102 { 103 mblk_t *mp, *prev = NULL, *new_chain = mp_chain, *mp1; 104 uint32_t flags, start, stuff, end, value; 105 106 for (mp = mp_chain; mp != NULL; prev = mp, mp = mp->b_next) { 107 uint16_t len; 108 uint32_t offset; 109 struct ether_header *ehp; 110 uint16_t sap; 111 112 hcksum_retrieve(mp, NULL, NULL, &start, &stuff, &end, &value, 113 &flags); 114 if (flags == 0) 115 continue; 116 117 /* 118 * Since the processing of checksum offload for loopback 119 * traffic requires modification of the packet contents, 120 * ensure sure that we are always modifying our own copy. 121 */ 122 if (DB_REF(mp) > 1) { 123 mp1 = copymsg(mp); 124 if (mp1 == NULL) 125 continue; 126 mp1->b_next = mp->b_next; 127 mp->b_next = NULL; 128 freemsg(mp); 129 if (prev != NULL) 130 prev->b_next = mp1; 131 else 132 new_chain = mp1; 133 mp = mp1; 134 } 135 136 /* 137 * Ethernet, and optionally VLAN header. 138 */ 139 /* LINTED: improper alignment cast */ 140 ehp = (struct ether_header *)mp->b_rptr; 141 if (ntohs(ehp->ether_type) == VLAN_TPID) { 142 struct ether_vlan_header *evhp; 143 144 ASSERT(MBLKL(mp) >= sizeof (struct ether_vlan_header)); 145 /* LINTED: improper alignment cast */ 146 evhp = (struct ether_vlan_header *)mp->b_rptr; 147 sap = ntohs(evhp->ether_type); 148 offset = sizeof (struct ether_vlan_header); 149 } else { 150 sap = ntohs(ehp->ether_type); 151 offset = sizeof (struct ether_header); 152 } 153 154 if (MBLKL(mp) <= offset) { 155 offset -= MBLKL(mp); 156 if (mp->b_cont == NULL) { 157 /* corrupted packet, skip it */ 158 if (prev != NULL) 159 prev->b_next = mp->b_next; 160 else 161 new_chain = mp->b_next; 162 mp1 = mp->b_next; 163 mp->b_next = NULL; 164 freemsg(mp); 165 mp = mp1; 166 continue; 167 } 168 mp = mp->b_cont; 169 } 170 171 if (flags & (HCK_FULLCKSUM | HCK_IPV4_HDRCKSUM)) { 172 ipha_t *ipha = NULL; 173 174 /* 175 * In order to compute the full and header 176 * checksums, we need to find and parse 177 * the IP and/or ULP headers. 178 */ 179 180 sap = (sap < ETHERTYPE_802_MIN) ? 0 : sap; 181 182 /* 183 * IP header. 184 */ 185 if (sap != ETHERTYPE_IP) 186 continue; 187 188 ASSERT(MBLKL(mp) >= offset + sizeof (ipha_t)); 189 /* LINTED: improper alignment cast */ 190 ipha = (ipha_t *)(mp->b_rptr + offset); 191 192 if (flags & HCK_FULLCKSUM) { 193 ipaddr_t src, dst; 194 uint32_t cksum; 195 uint16_t *up; 196 uint8_t proto; 197 198 /* 199 * Pointer to checksum field in ULP header. 200 */ 201 proto = ipha->ipha_protocol; 202 ASSERT(ipha->ipha_version_and_hdr_length == 203 IP_SIMPLE_HDR_VERSION); 204 if (proto == IPPROTO_TCP) { 205 /* LINTED: improper alignment cast */ 206 up = IPH_TCPH_CHECKSUMP(ipha, 207 IP_SIMPLE_HDR_LENGTH); 208 } else { 209 ASSERT(proto == IPPROTO_UDP); 210 /* LINTED: improper alignment cast */ 211 up = IPH_UDPH_CHECKSUMP(ipha, 212 IP_SIMPLE_HDR_LENGTH); 213 } 214 215 /* 216 * Pseudo-header checksum. 217 */ 218 src = ipha->ipha_src; 219 dst = ipha->ipha_dst; 220 len = ntohs(ipha->ipha_length) - 221 IP_SIMPLE_HDR_LENGTH; 222 223 cksum = (dst >> 16) + (dst & 0xFFFF) + 224 (src >> 16) + (src & 0xFFFF); 225 cksum += htons(len); 226 227 /* 228 * The checksum value stored in the packet needs 229 * to be correct. Compute it here. 230 */ 231 *up = 0; 232 cksum += (((proto) == IPPROTO_UDP) ? 233 IP_UDP_CSUM_COMP : IP_TCP_CSUM_COMP); 234 cksum = IP_CSUM(mp, IP_SIMPLE_HDR_LENGTH + 235 offset, cksum); 236 *(up) = (uint16_t)(cksum ? cksum : ~cksum); 237 238 flags |= HCK_FULLCKSUM_OK; 239 value = 0xffff; 240 } 241 242 if (flags & HCK_IPV4_HDRCKSUM) { 243 ASSERT(ipha != NULL); 244 ipha->ipha_hdr_checksum = 245 (uint16_t)ip_csum_hdr(ipha); 246 } 247 } 248 249 if (flags & HCK_PARTIALCKSUM) { 250 uint16_t *up, partial, cksum; 251 uchar_t *ipp; /* ptr to beginning of IP header */ 252 253 if (mp->b_cont != NULL) { 254 mblk_t *mp1; 255 256 mp1 = msgpullup(mp, offset + end); 257 if (mp1 == NULL) 258 continue; 259 mp1->b_next = mp->b_next; 260 mp->b_next = NULL; 261 freemsg(mp); 262 if (prev != NULL) 263 prev->b_next = mp1; 264 else 265 new_chain = mp1; 266 mp = mp1; 267 } 268 269 ipp = mp->b_rptr + offset; 270 /* LINTED: cast may result in improper alignment */ 271 up = (uint16_t *)((uchar_t *)ipp + stuff); 272 partial = *up; 273 *up = 0; 274 275 cksum = IP_BCSUM_PARTIAL(mp->b_rptr + offset + start, 276 end - start, partial); 277 cksum = ~cksum; 278 *up = cksum ? cksum : ~cksum; 279 280 /* 281 * Since we already computed the whole checksum, 282 * indicate to the stack that it has already 283 * been verified by the hardware. 284 */ 285 flags &= ~HCK_PARTIALCKSUM; 286 flags |= (HCK_FULLCKSUM | HCK_FULLCKSUM_OK); 287 value = 0xffff; 288 } 289 290 (void) hcksum_assoc(mp, NULL, NULL, start, stuff, end, 291 value, flags, KM_NOSLEEP); 292 } 293 294 return (new_chain); 295 } 296 297 /* 298 * Add VLAN tag to the specified mblk. 299 */ 300 mblk_t * 301 mac_add_vlan_tag(mblk_t *mp, uint_t pri, uint16_t vid) 302 { 303 mblk_t *hmp; 304 struct ether_vlan_header *evhp; 305 struct ether_header *ehp; 306 uint32_t start, stuff, end, value, flags; 307 308 ASSERT(pri != 0 || vid != 0); 309 310 /* 311 * Allocate an mblk for the new tagged ethernet header, 312 * and copy the MAC addresses and ethertype from the 313 * original header. 314 */ 315 316 hmp = allocb(sizeof (struct ether_vlan_header), BPRI_MED); 317 if (hmp == NULL) { 318 freemsg(mp); 319 return (NULL); 320 } 321 322 evhp = (struct ether_vlan_header *)hmp->b_rptr; 323 ehp = (struct ether_header *)mp->b_rptr; 324 325 bcopy(ehp, evhp, (ETHERADDRL * 2)); 326 evhp->ether_type = ehp->ether_type; 327 evhp->ether_tpid = htons(ETHERTYPE_VLAN); 328 329 hmp->b_wptr += sizeof (struct ether_vlan_header); 330 mp->b_rptr += sizeof (struct ether_header); 331 332 /* 333 * Free the original message if it's now empty. Link the 334 * rest of messages to the header message. 335 */ 336 hcksum_retrieve(mp, NULL, NULL, &start, &stuff, &end, &value, &flags); 337 (void) hcksum_assoc(hmp, NULL, NULL, start, stuff, end, value, flags, 338 KM_NOSLEEP); 339 if (MBLKL(mp) == 0) { 340 hmp->b_cont = mp->b_cont; 341 freeb(mp); 342 } else { 343 hmp->b_cont = mp; 344 } 345 ASSERT(MBLKL(hmp) >= sizeof (struct ether_vlan_header)); 346 347 /* 348 * Initialize the new TCI (Tag Control Information). 349 */ 350 evhp->ether_tci = htons(VLAN_TCI(pri, 0, vid)); 351 352 return (hmp); 353 } 354 355 /* 356 * Adds a VLAN tag with the specified VID and priority to each mblk of 357 * the specified chain. 358 */ 359 mblk_t * 360 mac_add_vlan_tag_chain(mblk_t *mp_chain, uint_t pri, uint16_t vid) 361 { 362 mblk_t *next_mp, **prev, *mp; 363 364 mp = mp_chain; 365 prev = &mp_chain; 366 367 while (mp != NULL) { 368 next_mp = mp->b_next; 369 mp->b_next = NULL; 370 if ((mp = mac_add_vlan_tag(mp, pri, vid)) == NULL) { 371 freemsgchain(next_mp); 372 break; 373 } 374 *prev = mp; 375 prev = &mp->b_next; 376 mp = mp->b_next = next_mp; 377 } 378 379 return (mp_chain); 380 } 381 382 /* 383 * Strip VLAN tag 384 */ 385 mblk_t * 386 mac_strip_vlan_tag(mblk_t *mp) 387 { 388 mblk_t *newmp; 389 struct ether_vlan_header *evhp; 390 391 evhp = (struct ether_vlan_header *)mp->b_rptr; 392 if (ntohs(evhp->ether_tpid) == ETHERTYPE_VLAN) { 393 ASSERT(MBLKL(mp) >= sizeof (struct ether_vlan_header)); 394 395 if (DB_REF(mp) > 1) { 396 newmp = copymsg(mp); 397 if (newmp == NULL) 398 return (NULL); 399 freemsg(mp); 400 mp = newmp; 401 } 402 403 evhp = (struct ether_vlan_header *)mp->b_rptr; 404 405 ovbcopy(mp->b_rptr, mp->b_rptr + VLAN_TAGSZ, 2 * ETHERADDRL); 406 mp->b_rptr += VLAN_TAGSZ; 407 } 408 return (mp); 409 } 410 411 /* 412 * Strip VLAN tag from each mblk of the chain. 413 */ 414 mblk_t * 415 mac_strip_vlan_tag_chain(mblk_t *mp_chain) 416 { 417 mblk_t *mp, *next_mp, **prev; 418 419 mp = mp_chain; 420 prev = &mp_chain; 421 422 while (mp != NULL) { 423 next_mp = mp->b_next; 424 mp->b_next = NULL; 425 if ((mp = mac_strip_vlan_tag(mp)) == NULL) { 426 freemsgchain(next_mp); 427 break; 428 } 429 *prev = mp; 430 prev = &mp->b_next; 431 mp = mp->b_next = next_mp; 432 } 433 434 return (mp_chain); 435 } 436 437 /* 438 * Default callback function. Used when the datapath is not yet initialized. 439 */ 440 /* ARGSUSED */ 441 void 442 mac_pkt_drop(void *arg, mac_resource_handle_t resource, mblk_t *mp, 443 boolean_t loopback) 444 { 445 mblk_t *mp1 = mp; 446 447 while (mp1 != NULL) { 448 mp1->b_prev = NULL; 449 mp1->b_queue = NULL; 450 mp1 = mp1->b_next; 451 } 452 freemsgchain(mp); 453 } 454 455 /* 456 * Determines the IPv6 header length accounting for all the optional IPv6 457 * headers (hop-by-hop, destination, routing and fragment). The header length 458 * and next header value (a transport header) is captured. 459 * 460 * Returns B_FALSE if all the IP headers are not in the same mblk otherwise 461 * returns B_TRUE. 462 */ 463 boolean_t 464 mac_ip_hdr_length_v6(mblk_t *mp, ip6_t *ip6h, uint16_t *hdr_length, 465 uint8_t *next_hdr) 466 { 467 uint16_t length; 468 uint_t ehdrlen; 469 uint8_t *whereptr; 470 uint8_t *endptr; 471 uint8_t *nexthdrp; 472 ip6_dest_t *desthdr; 473 ip6_rthdr_t *rthdr; 474 ip6_frag_t *fraghdr; 475 476 endptr = mp->b_wptr; 477 if (((uchar_t *)ip6h + IPV6_HDR_LEN) > endptr) 478 return (B_FALSE); 479 ASSERT((IPH_HDR_VERSION(ip6h) & ~IP_FORWARD_PROG_BIT) == IPV6_VERSION); 480 length = IPV6_HDR_LEN; 481 whereptr = ((uint8_t *)&ip6h[1]); /* point to next hdr */ 482 483 nexthdrp = &ip6h->ip6_nxt; 484 while (whereptr < endptr) { 485 /* Is there enough left for len + nexthdr? */ 486 if (whereptr + MIN_EHDR_LEN > endptr) 487 break; 488 489 switch (*nexthdrp) { 490 case IPPROTO_HOPOPTS: 491 case IPPROTO_DSTOPTS: 492 /* Assumes the headers are identical for hbh and dst */ 493 desthdr = (ip6_dest_t *)whereptr; 494 ehdrlen = 8 * (desthdr->ip6d_len + 1); 495 if ((uchar_t *)desthdr + ehdrlen > endptr) 496 return (B_FALSE); 497 nexthdrp = &desthdr->ip6d_nxt; 498 break; 499 case IPPROTO_ROUTING: 500 rthdr = (ip6_rthdr_t *)whereptr; 501 ehdrlen = 8 * (rthdr->ip6r_len + 1); 502 if ((uchar_t *)rthdr + ehdrlen > endptr) 503 return (B_FALSE); 504 nexthdrp = &rthdr->ip6r_nxt; 505 break; 506 case IPPROTO_FRAGMENT: 507 fraghdr = (ip6_frag_t *)whereptr; 508 ehdrlen = sizeof (ip6_frag_t); 509 if ((uchar_t *)&fraghdr[1] > endptr) 510 return (B_FALSE); 511 nexthdrp = &fraghdr->ip6f_nxt; 512 break; 513 case IPPROTO_NONE: 514 /* No next header means we're finished */ 515 default: 516 *hdr_length = length; 517 *next_hdr = *nexthdrp; 518 return (B_TRUE); 519 } 520 length += ehdrlen; 521 whereptr += ehdrlen; 522 *hdr_length = length; 523 *next_hdr = *nexthdrp; 524 } 525 switch (*nexthdrp) { 526 case IPPROTO_HOPOPTS: 527 case IPPROTO_DSTOPTS: 528 case IPPROTO_ROUTING: 529 case IPPROTO_FRAGMENT: 530 /* 531 * If any know extension headers are still to be processed, 532 * the packet's malformed (or at least all the IP header(s) are 533 * not in the same mblk - and that should never happen. 534 */ 535 return (B_FALSE); 536 537 default: 538 /* 539 * If we get here, we know that all of the IP headers were in 540 * the same mblk, even if the ULP header is in the next mblk. 541 */ 542 *hdr_length = length; 543 *next_hdr = *nexthdrp; 544 return (B_TRUE); 545 } 546 } 547 548 typedef struct mac_dladm_intr { 549 int ino; 550 int cpu_id; 551 char driver_path[MAXPATHLEN]; 552 char nexus_path[MAXPATHLEN]; 553 } mac_dladm_intr_t; 554 555 /* Bind the interrupt to cpu_num */ 556 static int 557 mac_set_intr(ldi_handle_t lh, processorid_t cpu_num, int ino) 558 { 559 pcitool_intr_set_t iset; 560 int err; 561 562 iset.ino = ino; 563 iset.cpu_id = cpu_num; 564 iset.user_version = PCITOOL_VERSION; 565 err = ldi_ioctl(lh, PCITOOL_DEVICE_SET_INTR, (intptr_t)&iset, FKIOCTL, 566 kcred, NULL); 567 568 return (err); 569 } 570 571 /* 572 * Search interrupt information. iget is filled in with the info to search 573 */ 574 static boolean_t 575 mac_search_intrinfo(pcitool_intr_get_t *iget_p, mac_dladm_intr_t *dln) 576 { 577 int i; 578 char driver_path[2 * MAXPATHLEN]; 579 580 for (i = 0; i < iget_p->num_devs; i++) { 581 (void) strlcpy(driver_path, iget_p->dev[i].path, MAXPATHLEN); 582 (void) snprintf(&driver_path[strlen(driver_path)], MAXPATHLEN, 583 ":%s%d", iget_p->dev[i].driver_name, 584 iget_p->dev[i].dev_inst); 585 /* Match the device path for the device path */ 586 if (strcmp(driver_path, dln->driver_path) == 0) { 587 dln->ino = iget_p->ino; 588 dln->cpu_id = iget_p->cpu_id; 589 return (B_TRUE); 590 } 591 } 592 return (B_FALSE); 593 } 594 595 /* 596 * Get information about ino, i.e. if this is the interrupt for our 597 * device and where it is bound etc. 598 */ 599 static boolean_t 600 mac_get_single_intr(ldi_handle_t lh, int ino, mac_dladm_intr_t *dln) 601 { 602 pcitool_intr_get_t *iget_p; 603 int ipsz; 604 int nipsz; 605 int err; 606 uint8_t inum; 607 608 /* 609 * Check if SLEEP is OK, i.e if could come here in response to 610 * changing the fanout due to some callback from the driver, say 611 * link speed changes. 612 */ 613 ipsz = PCITOOL_IGET_SIZE(0); 614 iget_p = kmem_zalloc(ipsz, KM_SLEEP); 615 616 iget_p->num_devs_ret = 0; 617 iget_p->user_version = PCITOOL_VERSION; 618 iget_p->ino = ino; 619 620 err = ldi_ioctl(lh, PCITOOL_DEVICE_GET_INTR, (intptr_t)iget_p, 621 FKIOCTL, kcred, NULL); 622 if (err != 0) { 623 kmem_free(iget_p, ipsz); 624 return (B_FALSE); 625 } 626 if (iget_p->num_devs == 0) { 627 kmem_free(iget_p, ipsz); 628 return (B_FALSE); 629 } 630 inum = iget_p->num_devs; 631 if (iget_p->num_devs_ret < iget_p->num_devs) { 632 /* Reallocate */ 633 nipsz = PCITOOL_IGET_SIZE(iget_p->num_devs); 634 635 kmem_free(iget_p, ipsz); 636 ipsz = nipsz; 637 iget_p = kmem_zalloc(ipsz, KM_SLEEP); 638 639 iget_p->num_devs_ret = inum; 640 iget_p->ino = ino; 641 iget_p->user_version = PCITOOL_VERSION; 642 err = ldi_ioctl(lh, PCITOOL_DEVICE_GET_INTR, (intptr_t)iget_p, 643 FKIOCTL, kcred, NULL); 644 if (err != 0) { 645 kmem_free(iget_p, ipsz); 646 return (B_FALSE); 647 } 648 /* defensive */ 649 if (iget_p->num_devs != iget_p->num_devs_ret) { 650 kmem_free(iget_p, ipsz); 651 return (B_FALSE); 652 } 653 } 654 655 if (mac_search_intrinfo(iget_p, dln)) { 656 kmem_free(iget_p, ipsz); 657 return (B_TRUE); 658 } 659 kmem_free(iget_p, ipsz); 660 return (B_FALSE); 661 } 662 663 /* 664 * Get the interrupts and check each one to see if it is for our device. 665 */ 666 static int 667 mac_validate_intr(ldi_handle_t lh, mac_dladm_intr_t *dln, processorid_t cpuid) 668 { 669 pcitool_intr_info_t intr_info; 670 int err; 671 int ino; 672 673 err = ldi_ioctl(lh, PCITOOL_SYSTEM_INTR_INFO, (intptr_t)&intr_info, 674 FKIOCTL, kcred, NULL); 675 if (err != 0) 676 return (-1); 677 678 for (ino = 0; ino < intr_info.num_intr; ino++) { 679 if (mac_get_single_intr(lh, ino, dln)) { 680 if (dln->cpu_id == cpuid) 681 return (0); 682 return (1); 683 } 684 } 685 return (-1); 686 } 687 688 /* 689 * Obtain the nexus parent node info. for mdip. 690 */ 691 static dev_info_t * 692 mac_get_nexus_node(dev_info_t *mdip, mac_dladm_intr_t *dln) 693 { 694 struct dev_info *tdip = (struct dev_info *)mdip; 695 struct ddi_minor_data *minordata; 696 int circ; 697 dev_info_t *pdip; 698 char pathname[MAXPATHLEN]; 699 700 while (tdip != NULL) { 701 ndi_devi_enter((dev_info_t *)tdip, &circ); 702 for (minordata = tdip->devi_minor; minordata != NULL; 703 minordata = minordata->next) { 704 if (strncmp(minordata->ddm_node_type, DDI_NT_INTRCTL, 705 strlen(DDI_NT_INTRCTL)) == 0) { 706 pdip = minordata->dip; 707 (void) ddi_pathname(pdip, pathname); 708 (void) snprintf(dln->nexus_path, MAXPATHLEN, 709 "/devices%s:intr", pathname); 710 (void) ddi_pathname_minor(minordata, pathname); 711 ndi_devi_exit((dev_info_t *)tdip, circ); 712 return (pdip); 713 } 714 } 715 ndi_devi_exit((dev_info_t *)tdip, circ); 716 tdip = tdip->devi_parent; 717 } 718 return (NULL); 719 } 720 721 /* 722 * For a primary MAC client, if the user has set a list or CPUs or 723 * we have obtained it implicitly, we try to retarget the interrupt 724 * for that device on one of the CPUs in the list. 725 * We assign the interrupt to the same CPU as the poll thread. 726 */ 727 static boolean_t 728 mac_check_interrupt_binding(dev_info_t *mdip, int32_t cpuid) 729 { 730 ldi_handle_t lh = NULL; 731 ldi_ident_t li = NULL; 732 int err; 733 int ret; 734 mac_dladm_intr_t dln; 735 dev_info_t *dip; 736 struct ddi_minor_data *minordata; 737 738 dln.nexus_path[0] = '\0'; 739 dln.driver_path[0] = '\0'; 740 741 minordata = ((struct dev_info *)mdip)->devi_minor; 742 while (minordata != NULL) { 743 if (minordata->type == DDM_MINOR) 744 break; 745 minordata = minordata->next; 746 } 747 if (minordata == NULL) 748 return (B_FALSE); 749 750 (void) ddi_pathname_minor(minordata, dln.driver_path); 751 752 dip = mac_get_nexus_node(mdip, &dln); 753 /* defensive */ 754 if (dip == NULL) 755 return (B_FALSE); 756 757 err = ldi_ident_from_major(ddi_driver_major(dip), &li); 758 if (err != 0) 759 return (B_FALSE); 760 761 err = ldi_open_by_name(dln.nexus_path, FREAD|FWRITE, kcred, &lh, li); 762 if (err != 0) 763 return (B_FALSE); 764 765 ret = mac_validate_intr(lh, &dln, cpuid); 766 if (ret < 0) { 767 (void) ldi_close(lh, FREAD|FWRITE, kcred); 768 return (B_FALSE); 769 } 770 /* cmn_note? */ 771 if (ret != 0) 772 if ((err = (mac_set_intr(lh, cpuid, dln.ino))) != 0) { 773 (void) ldi_close(lh, FREAD|FWRITE, kcred); 774 return (B_FALSE); 775 } 776 (void) ldi_close(lh, FREAD|FWRITE, kcred); 777 return (B_TRUE); 778 } 779 780 void 781 mac_client_set_intr_cpu(void *arg, mac_client_handle_t mch, int32_t cpuid) 782 { 783 dev_info_t *mdip = (dev_info_t *)arg; 784 mac_client_impl_t *mcip = (mac_client_impl_t *)mch; 785 mac_resource_props_t *mrp; 786 mac_perim_handle_t mph; 787 788 if (cpuid == -1 || !mac_check_interrupt_binding(mdip, cpuid)) 789 return; 790 791 mac_perim_enter_by_mh((mac_handle_t)mcip->mci_mip, &mph); 792 mrp = MCIP_RESOURCE_PROPS(mcip); 793 mrp->mrp_intr_cpu = cpuid; 794 mac_perim_exit(mph); 795 } 796 797 int32_t 798 mac_client_intr_cpu(mac_client_handle_t mch) 799 { 800 mac_client_impl_t *mcip = (mac_client_impl_t *)mch; 801 mac_cpus_t *srs_cpu; 802 mac_soft_ring_set_t *rx_srs; 803 flow_entry_t *flent = mcip->mci_flent; 804 mac_resource_props_t *mrp = MCIP_RESOURCE_PROPS(mcip); 805 806 /* 807 * Check if we need to retarget the interrupt. We do this only 808 * for the primary MAC client. We do this if we have the only 809 * exclusive ring in the group. 810 */ 811 if (mac_is_primary_client(mcip) && flent->fe_rx_srs_cnt == 2) { 812 rx_srs = flent->fe_rx_srs[1]; 813 srs_cpu = &rx_srs->srs_cpu; 814 if (mrp->mrp_intr_cpu == srs_cpu->mc_pollid) 815 return (-1); 816 return (srs_cpu->mc_pollid); 817 } 818 return (-1); 819 } 820 821 void * 822 mac_get_devinfo(mac_handle_t mh) 823 { 824 mac_impl_t *mip = (mac_impl_t *)mh; 825 826 return ((void *)mip->mi_dip); 827 } 828 829 #define PKT_HASH_4BYTES(x) ((x)[0] ^ (x)[1] ^ (x)[2] ^ (x)[3]) 830 #define PKT_HASH_MAC(x) ((x)[0] ^ (x)[1] ^ (x)[2] ^ (x)[3] ^ (x)[4] ^ (x)[5]) 831 832 uint64_t 833 mac_pkt_hash(uint_t media, mblk_t *mp, uint8_t policy, boolean_t is_outbound) 834 { 835 struct ether_header *ehp; 836 uint64_t hash = 0; 837 uint16_t sap; 838 uint_t skip_len; 839 uint8_t proto; 840 841 /* 842 * We may want to have one of these per MAC type plugin in the 843 * future. For now supports only ethernet. 844 */ 845 if (media != DL_ETHER) 846 return (0L); 847 848 /* for now we support only outbound packets */ 849 ASSERT(is_outbound); 850 ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t))); 851 ASSERT(MBLKL(mp) >= sizeof (struct ether_header)); 852 853 /* compute L2 hash */ 854 855 ehp = (struct ether_header *)mp->b_rptr; 856 857 if ((policy & MAC_PKT_HASH_L2) != 0) { 858 uchar_t *mac_src = ehp->ether_shost.ether_addr_octet; 859 uchar_t *mac_dst = ehp->ether_dhost.ether_addr_octet; 860 hash = PKT_HASH_MAC(mac_src) ^ PKT_HASH_MAC(mac_dst); 861 policy &= ~MAC_PKT_HASH_L2; 862 } 863 864 if (policy == 0) 865 goto done; 866 867 /* skip ethernet header */ 868 869 sap = ntohs(ehp->ether_type); 870 if (sap == ETHERTYPE_VLAN) { 871 struct ether_vlan_header *evhp; 872 mblk_t *newmp = NULL; 873 874 skip_len = sizeof (struct ether_vlan_header); 875 if (MBLKL(mp) < skip_len) { 876 /* the vlan tag is the payload, pull up first */ 877 newmp = msgpullup(mp, -1); 878 if ((newmp == NULL) || (MBLKL(newmp) < skip_len)) { 879 goto done; 880 } 881 evhp = (struct ether_vlan_header *)newmp->b_rptr; 882 } else { 883 evhp = (struct ether_vlan_header *)mp->b_rptr; 884 } 885 886 sap = ntohs(evhp->ether_type); 887 freemsg(newmp); 888 } else { 889 skip_len = sizeof (struct ether_header); 890 } 891 892 /* if ethernet header is in its own mblk, skip it */ 893 if (MBLKL(mp) <= skip_len) { 894 skip_len -= MBLKL(mp); 895 mp = mp->b_cont; 896 if (mp == NULL) 897 goto done; 898 } 899 900 sap = (sap < ETHERTYPE_802_MIN) ? 0 : sap; 901 902 /* compute IP src/dst addresses hash and skip IPv{4,6} header */ 903 904 switch (sap) { 905 case ETHERTYPE_IP: { 906 ipha_t *iphp; 907 908 /* 909 * If the header is not aligned or the header doesn't fit 910 * in the mblk, bail now. Note that this may cause packets 911 * reordering. 912 */ 913 iphp = (ipha_t *)(mp->b_rptr + skip_len); 914 if (((unsigned char *)iphp + sizeof (ipha_t) > mp->b_wptr) || 915 !OK_32PTR((char *)iphp)) 916 goto done; 917 918 proto = iphp->ipha_protocol; 919 skip_len += IPH_HDR_LENGTH(iphp); 920 921 if ((policy & MAC_PKT_HASH_L3) != 0) { 922 uint8_t *ip_src = (uint8_t *)&(iphp->ipha_src); 923 uint8_t *ip_dst = (uint8_t *)&(iphp->ipha_dst); 924 925 hash ^= (PKT_HASH_4BYTES(ip_src) ^ 926 PKT_HASH_4BYTES(ip_dst)); 927 policy &= ~MAC_PKT_HASH_L3; 928 } 929 break; 930 } 931 case ETHERTYPE_IPV6: { 932 ip6_t *ip6hp; 933 uint16_t hdr_length; 934 935 /* 936 * If the header is not aligned or the header doesn't fit 937 * in the mblk, bail now. Note that this may cause packets 938 * reordering. 939 */ 940 941 ip6hp = (ip6_t *)(mp->b_rptr + skip_len); 942 if (((unsigned char *)ip6hp + IPV6_HDR_LEN > mp->b_wptr) || 943 !OK_32PTR((char *)ip6hp)) 944 goto done; 945 946 if (!mac_ip_hdr_length_v6(mp, ip6hp, &hdr_length, &proto)) 947 goto done; 948 skip_len += hdr_length; 949 950 if ((policy & MAC_PKT_HASH_L3) != 0) { 951 uint8_t *ip_src = &(ip6hp->ip6_src.s6_addr8[12]); 952 uint8_t *ip_dst = &(ip6hp->ip6_dst.s6_addr8[12]); 953 954 hash ^= (PKT_HASH_4BYTES(ip_src) ^ 955 PKT_HASH_4BYTES(ip_dst)); 956 policy &= ~MAC_PKT_HASH_L3; 957 } 958 break; 959 } 960 default: 961 goto done; 962 } 963 964 if (policy == 0) 965 goto done; 966 967 /* if ip header is in its own mblk, skip it */ 968 if (MBLKL(mp) <= skip_len) { 969 skip_len -= MBLKL(mp); 970 mp = mp->b_cont; 971 if (mp == NULL) 972 goto done; 973 } 974 975 /* parse ULP header */ 976 again: 977 switch (proto) { 978 case IPPROTO_TCP: 979 case IPPROTO_UDP: 980 case IPPROTO_ESP: 981 case IPPROTO_SCTP: 982 /* 983 * These Internet Protocols are intentionally designed 984 * for hashing from the git-go. Port numbers are in the first 985 * word for transports, SPI is first for ESP. 986 */ 987 if (mp->b_rptr + skip_len + 4 > mp->b_wptr) 988 goto done; 989 hash ^= PKT_HASH_4BYTES((mp->b_rptr + skip_len)); 990 break; 991 992 case IPPROTO_AH: { 993 ah_t *ah = (ah_t *)(mp->b_rptr + skip_len); 994 uint_t ah_length = AH_TOTAL_LEN(ah); 995 996 if ((unsigned char *)ah + sizeof (ah_t) > mp->b_wptr) 997 goto done; 998 999 proto = ah->ah_nexthdr; 1000 skip_len += ah_length; 1001 1002 /* if AH header is in its own mblk, skip it */ 1003 if (MBLKL(mp) <= skip_len) { 1004 skip_len -= MBLKL(mp); 1005 mp = mp->b_cont; 1006 if (mp == NULL) 1007 goto done; 1008 } 1009 1010 goto again; 1011 } 1012 } 1013 1014 done: 1015 return (hash); 1016 } 1017