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