1 /* $NetBSD: if_shmem.c,v 1.58 2013/09/13 20:38:04 joerg Exp $ */ 2 3 /* 4 * Copyright (c) 2009, 2010 Antti Kantee. All Rights Reserved. 5 * 6 * Development of this software was supported by The Nokia Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __KERNEL_RCSID(0, "$NetBSD: if_shmem.c,v 1.58 2013/09/13 20:38:04 joerg Exp $"); 32 33 #include <sys/param.h> 34 #include <sys/atomic.h> 35 #include <sys/fcntl.h> 36 #include <sys/kmem.h> 37 #include <sys/kthread.h> 38 #include <sys/lock.h> 39 #include <sys/vmem.h> 40 #include <sys/cprng.h> 41 42 #include <net/bpf.h> 43 #include <net/if.h> 44 #include <net/if_dl.h> 45 #include <net/if_ether.h> 46 47 #include <netinet/in.h> 48 #include <netinet/in_var.h> 49 50 #include <rump/rump.h> 51 #include <rump/rumpuser.h> 52 53 #include "rump_private.h" 54 #include "rump_net_private.h" 55 #include "rumpcomp_user.h" 56 57 static int shmif_clone(struct if_clone *, int); 58 static int shmif_unclone(struct ifnet *); 59 60 struct if_clone shmif_cloner = 61 IF_CLONE_INITIALIZER("shmif", shmif_clone, shmif_unclone); 62 63 /* 64 * Do r/w prefault for backend pages when attaching the interface. 65 * At least logically thinking improves performance (although no 66 * mlocking is done, so they might go away). 67 */ 68 #define PREFAULT_RW 69 70 /* 71 * A virtual ethernet interface which uses shared memory from a 72 * memory mapped file as the bus. 73 */ 74 75 static int shmif_init(struct ifnet *); 76 static int shmif_ioctl(struct ifnet *, u_long, void *); 77 static void shmif_start(struct ifnet *); 78 static void shmif_stop(struct ifnet *, int); 79 80 #include "shmifvar.h" 81 82 struct shmif_sc { 83 struct ethercom sc_ec; 84 struct shmif_mem *sc_busmem; 85 int sc_memfd; 86 int sc_kq; 87 int sc_unit; 88 89 char *sc_backfile; 90 size_t sc_backfilelen; 91 92 uint64_t sc_devgen; 93 uint32_t sc_nextpacket; 94 95 kmutex_t sc_mtx; 96 kcondvar_t sc_cv; 97 98 struct lwp *sc_rcvl; 99 bool sc_dying; 100 }; 101 102 static void shmif_rcv(void *); 103 104 #define LOCK_UNLOCKED 0 105 #define LOCK_LOCKED 1 106 #define LOCK_COOLDOWN 1001 107 108 vmem_t *shmif_units; 109 110 static void 111 dowakeup(struct shmif_sc *sc) 112 { 113 struct rumpuser_iovec iov; 114 uint32_t ver = SHMIF_VERSION; 115 size_t n; 116 117 iov.iov_base = &ver; 118 iov.iov_len = sizeof(ver); 119 rumpuser_iovwrite(sc->sc_memfd, &iov, 1, IFMEM_WAKEUP, &n); 120 } 121 122 /* 123 * This locking needs work and will misbehave severely if: 124 * 1) the backing memory has to be paged in 125 * 2) some lockholder exits while holding the lock 126 */ 127 static void 128 shmif_lockbus(struct shmif_mem *busmem) 129 { 130 int i = 0; 131 132 while (__predict_false(atomic_cas_32(&busmem->shm_lock, 133 LOCK_UNLOCKED, LOCK_LOCKED) == LOCK_LOCKED)) { 134 if (__predict_false(++i > LOCK_COOLDOWN)) { 135 /* wait 1ms */ 136 rumpuser_clock_sleep(RUMPUSER_CLOCK_RELWALL, 137 0, 1000*1000); 138 i = 0; 139 } 140 continue; 141 } 142 membar_enter(); 143 } 144 145 static void 146 shmif_unlockbus(struct shmif_mem *busmem) 147 { 148 unsigned int old; 149 150 membar_exit(); 151 old = atomic_swap_32(&busmem->shm_lock, LOCK_UNLOCKED); 152 KASSERT(old == LOCK_LOCKED); 153 } 154 155 static int 156 allocif(int unit, struct shmif_sc **scp) 157 { 158 uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0xa0, 0x00, 0x00, 0x00, 0x00 }; 159 struct shmif_sc *sc; 160 struct ifnet *ifp; 161 uint32_t randnum; 162 int error; 163 164 randnum = cprng_fast32(); 165 memcpy(&enaddr[2], &randnum, sizeof(randnum)); 166 167 sc = kmem_zalloc(sizeof(*sc), KM_SLEEP); 168 sc->sc_memfd = -1; 169 sc->sc_unit = unit; 170 171 ifp = &sc->sc_ec.ec_if; 172 173 sprintf(ifp->if_xname, "shmif%d", unit); 174 ifp->if_softc = sc; 175 ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST; 176 ifp->if_init = shmif_init; 177 ifp->if_ioctl = shmif_ioctl; 178 ifp->if_start = shmif_start; 179 ifp->if_stop = shmif_stop; 180 ifp->if_mtu = ETHERMTU; 181 ifp->if_dlt = DLT_EN10MB; 182 183 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE); 184 cv_init(&sc->sc_cv, "shmifcv"); 185 186 if_attach(ifp); 187 ether_ifattach(ifp, enaddr); 188 189 aprint_verbose("shmif%d: Ethernet address %s\n", 190 unit, ether_sprintf(enaddr)); 191 192 if (scp) 193 *scp = sc; 194 195 error = 0; 196 if (rump_threads) { 197 error = kthread_create(PRI_NONE, 198 KTHREAD_MPSAFE | KTHREAD_MUSTJOIN, NULL, 199 shmif_rcv, ifp, &sc->sc_rcvl, "shmif"); 200 } else { 201 printf("WARNING: threads not enabled, shmif NOT working\n"); 202 } 203 204 if (error) { 205 shmif_unclone(ifp); 206 } 207 208 return error; 209 } 210 211 static int 212 initbackend(struct shmif_sc *sc, int memfd) 213 { 214 volatile uint8_t v; 215 volatile uint8_t *p; 216 void *mem; 217 int error; 218 219 error = rumpcomp_shmif_mmap(memfd, BUSMEM_SIZE, &mem); 220 if (error) 221 return error; 222 sc->sc_busmem = mem; 223 224 if (sc->sc_busmem->shm_magic 225 && sc->sc_busmem->shm_magic != SHMIF_MAGIC) { 226 printf("bus is not magical"); 227 rumpuser_unmap(sc->sc_busmem, BUSMEM_SIZE); 228 return ENOEXEC; 229 } 230 231 /* 232 * Prefault in pages to minimize runtime penalty with buslock. 233 * Use 512 instead of PAGE_SIZE to make sure we catch cases where 234 * rump kernel PAGE_SIZE > host page size. 235 */ 236 for (p = (uint8_t *)sc->sc_busmem; 237 p < (uint8_t *)sc->sc_busmem + BUSMEM_SIZE; 238 p += 512) 239 v = *p; 240 241 shmif_lockbus(sc->sc_busmem); 242 /* we're first? initialize bus */ 243 if (sc->sc_busmem->shm_magic == 0) { 244 sc->sc_busmem->shm_magic = SHMIF_MAGIC; 245 sc->sc_busmem->shm_first = BUSMEM_DATASIZE; 246 } 247 248 sc->sc_nextpacket = sc->sc_busmem->shm_last; 249 sc->sc_devgen = sc->sc_busmem->shm_gen; 250 251 #ifdef PREFAULT_RW 252 for (p = (uint8_t *)sc->sc_busmem; 253 p < (uint8_t *)sc->sc_busmem + BUSMEM_SIZE; 254 p += PAGE_SIZE) { 255 v = *p; 256 *p = v; 257 } 258 #endif 259 shmif_unlockbus(sc->sc_busmem); 260 261 sc->sc_kq = -1; 262 error = rumpcomp_shmif_watchsetup(&sc->sc_kq, memfd); 263 if (error) { 264 rumpuser_unmap(sc->sc_busmem, BUSMEM_SIZE); 265 return error; 266 } 267 268 sc->sc_memfd = memfd; 269 270 return error; 271 } 272 273 static void 274 finibackend(struct shmif_sc *sc) 275 { 276 277 if (sc->sc_backfile == NULL) 278 return; 279 280 if (sc->sc_backfile) { 281 kmem_free(sc->sc_backfile, sc->sc_backfilelen); 282 sc->sc_backfile = NULL; 283 sc->sc_backfilelen = 0; 284 } 285 286 rumpuser_unmap(sc->sc_busmem, BUSMEM_SIZE); 287 rumpuser_close(sc->sc_memfd); 288 rumpuser_close(sc->sc_kq); 289 290 sc->sc_memfd = -1; 291 } 292 293 int 294 rump_shmif_create(const char *path, int *ifnum) 295 { 296 struct shmif_sc *sc; 297 vmem_addr_t t; 298 int unit, error; 299 int memfd = -1; /* XXXgcc */ 300 301 if (path) { 302 error = rumpuser_open(path, 303 RUMPUSER_OPEN_RDWR | RUMPUSER_OPEN_CREATE, &memfd); 304 if (error) 305 return error; 306 } 307 308 error = vmem_xalloc(shmif_units, 1, 0, 0, 0, 309 VMEM_ADDR_MIN, VMEM_ADDR_MAX, VM_INSTANTFIT | VM_SLEEP, &t); 310 311 if (error != 0) { 312 if (path) 313 rumpuser_close(memfd); 314 return error; 315 } 316 317 unit = t - 1; 318 319 if ((error = allocif(unit, &sc)) != 0) { 320 if (path) 321 rumpuser_close(memfd); 322 return error; 323 } 324 325 if (!path) 326 goto out; 327 328 error = initbackend(sc, memfd); 329 if (error) { 330 shmif_unclone(&sc->sc_ec.ec_if); 331 return error; 332 } 333 334 sc->sc_backfilelen = strlen(path)+1; 335 sc->sc_backfile = kmem_alloc(sc->sc_backfilelen, KM_SLEEP); 336 strcpy(sc->sc_backfile, path); 337 338 out: 339 if (ifnum) 340 *ifnum = unit; 341 342 return 0; 343 } 344 345 static int 346 shmif_clone(struct if_clone *ifc, int unit) 347 { 348 int rc; 349 vmem_addr_t unit2; 350 351 /* 352 * Ok, we know the unit number, but we must still reserve it. 353 * Otherwise the wildcard-side of things might get the same one. 354 * This is slightly offset-happy due to vmem. First, we offset 355 * the range of unit numbers by +1 since vmem cannot deal with 356 * ranges starting from 0. Talk about uuuh. 357 */ 358 rc = vmem_xalloc(shmif_units, 1, 0, 0, 0, unit+1, unit+1, 359 VM_SLEEP | VM_INSTANTFIT, &unit2); 360 KASSERT(rc == 0 && unit2-1 == unit); 361 362 return allocif(unit, NULL); 363 } 364 365 static int 366 shmif_unclone(struct ifnet *ifp) 367 { 368 struct shmif_sc *sc = ifp->if_softc; 369 370 shmif_stop(ifp, 1); 371 if_down(ifp); 372 finibackend(sc); 373 374 mutex_enter(&sc->sc_mtx); 375 sc->sc_dying = true; 376 cv_broadcast(&sc->sc_cv); 377 mutex_exit(&sc->sc_mtx); 378 379 if (sc->sc_rcvl) 380 kthread_join(sc->sc_rcvl); 381 sc->sc_rcvl = NULL; 382 383 vmem_xfree(shmif_units, sc->sc_unit+1, 1); 384 385 ether_ifdetach(ifp); 386 if_detach(ifp); 387 388 cv_destroy(&sc->sc_cv); 389 mutex_destroy(&sc->sc_mtx); 390 391 kmem_free(sc, sizeof(*sc)); 392 393 return 0; 394 } 395 396 static int 397 shmif_init(struct ifnet *ifp) 398 { 399 struct shmif_sc *sc = ifp->if_softc; 400 int error = 0; 401 402 if (sc->sc_memfd == -1) 403 return ENXIO; 404 KASSERT(sc->sc_busmem); 405 406 ifp->if_flags |= IFF_RUNNING; 407 408 mutex_enter(&sc->sc_mtx); 409 sc->sc_nextpacket = sc->sc_busmem->shm_last; 410 sc->sc_devgen = sc->sc_busmem->shm_gen; 411 412 cv_broadcast(&sc->sc_cv); 413 mutex_exit(&sc->sc_mtx); 414 415 return error; 416 } 417 418 static int 419 shmif_ioctl(struct ifnet *ifp, u_long cmd, void *data) 420 { 421 struct shmif_sc *sc = ifp->if_softc; 422 struct ifdrv *ifd; 423 char *path; 424 int s, rv, memfd; 425 426 s = splnet(); 427 switch (cmd) { 428 case SIOCGLINKSTR: 429 ifd = data; 430 431 if (sc->sc_backfilelen == 0) { 432 rv = ENOENT; 433 break; 434 } 435 436 ifd->ifd_len = sc->sc_backfilelen; 437 if (ifd->ifd_cmd == IFLINKSTR_QUERYLEN) { 438 rv = 0; 439 break; 440 } 441 442 if (ifd->ifd_cmd != 0) { 443 rv = EINVAL; 444 break; 445 } 446 447 rv = copyoutstr(sc->sc_backfile, ifd->ifd_data, 448 MIN(sc->sc_backfilelen, ifd->ifd_len), NULL); 449 break; 450 case SIOCSLINKSTR: 451 if (ifp->if_flags & IFF_UP) { 452 rv = EBUSY; 453 break; 454 } 455 456 ifd = data; 457 if (ifd->ifd_cmd == IFLINKSTR_UNSET) { 458 finibackend(sc); 459 rv = 0; 460 break; 461 } else if (ifd->ifd_cmd != 0) { 462 rv = EINVAL; 463 break; 464 } else if (sc->sc_backfile) { 465 rv = EBUSY; 466 break; 467 } 468 469 if (ifd->ifd_len > MAXPATHLEN) { 470 rv = E2BIG; 471 break; 472 } else if (ifd->ifd_len < 1) { 473 rv = EINVAL; 474 break; 475 } 476 477 path = kmem_alloc(ifd->ifd_len, KM_SLEEP); 478 rv = copyinstr(ifd->ifd_data, path, ifd->ifd_len, NULL); 479 if (rv) { 480 kmem_free(path, ifd->ifd_len); 481 break; 482 } 483 rv = rumpuser_open(path, 484 RUMPUSER_OPEN_RDWR | RUMPUSER_OPEN_CREATE, &memfd); 485 if (rv) { 486 kmem_free(path, ifd->ifd_len); 487 break; 488 } 489 rv = initbackend(sc, memfd); 490 if (rv) { 491 kmem_free(path, ifd->ifd_len); 492 rumpuser_close(memfd); 493 break; 494 } 495 sc->sc_backfile = path; 496 sc->sc_backfilelen = ifd->ifd_len; 497 498 break; 499 default: 500 rv = ether_ioctl(ifp, cmd, data); 501 if (rv == ENETRESET) 502 rv = 0; 503 break; 504 } 505 splx(s); 506 507 return rv; 508 } 509 510 /* send everything in-context since it's just a matter of mem-to-mem copy */ 511 static void 512 shmif_start(struct ifnet *ifp) 513 { 514 struct shmif_sc *sc = ifp->if_softc; 515 struct shmif_mem *busmem = sc->sc_busmem; 516 struct mbuf *m, *m0; 517 uint32_t dataoff; 518 uint32_t pktsize, pktwrote; 519 bool wrote = false; 520 bool wrap; 521 522 ifp->if_flags |= IFF_OACTIVE; 523 524 for (;;) { 525 struct shmif_pkthdr sp; 526 struct timeval tv; 527 528 IF_DEQUEUE(&ifp->if_snd, m0); 529 if (m0 == NULL) { 530 break; 531 } 532 533 pktsize = 0; 534 for (m = m0; m != NULL; m = m->m_next) { 535 pktsize += m->m_len; 536 } 537 KASSERT(pktsize <= ETHERMTU + ETHER_HDR_LEN); 538 539 getmicrouptime(&tv); 540 sp.sp_len = pktsize; 541 sp.sp_sec = tv.tv_sec; 542 sp.sp_usec = tv.tv_usec; 543 544 bpf_mtap(ifp, m0); 545 546 shmif_lockbus(busmem); 547 KASSERT(busmem->shm_magic == SHMIF_MAGIC); 548 busmem->shm_last = shmif_nextpktoff(busmem, busmem->shm_last); 549 550 wrap = false; 551 dataoff = shmif_buswrite(busmem, 552 busmem->shm_last, &sp, sizeof(sp), &wrap); 553 pktwrote = 0; 554 for (m = m0; m != NULL; m = m->m_next) { 555 pktwrote += m->m_len; 556 dataoff = shmif_buswrite(busmem, dataoff, 557 mtod(m, void *), m->m_len, &wrap); 558 } 559 KASSERT(pktwrote == pktsize); 560 if (wrap) { 561 busmem->shm_gen++; 562 DPRINTF(("bus generation now %" PRIu64 "\n", 563 busmem->shm_gen)); 564 } 565 shmif_unlockbus(busmem); 566 567 m_freem(m0); 568 wrote = true; 569 570 DPRINTF(("shmif_start: send %d bytes at off %d\n", 571 pktsize, busmem->shm_last)); 572 } 573 574 ifp->if_flags &= ~IFF_OACTIVE; 575 576 /* wakeup? */ 577 if (wrote) { 578 dowakeup(sc); 579 } 580 } 581 582 static void 583 shmif_stop(struct ifnet *ifp, int disable) 584 { 585 struct shmif_sc *sc = ifp->if_softc; 586 587 ifp->if_flags &= ~IFF_RUNNING; 588 membar_producer(); 589 590 /* 591 * wakeup thread. this will of course wake up all bus 592 * listeners, but that's life. 593 */ 594 if (sc->sc_memfd != -1) { 595 dowakeup(sc); 596 } 597 } 598 599 600 /* 601 * Check if we have been sleeping too long. Basically, 602 * our in-sc nextpkt must by first <= nextpkt <= last"+1". 603 * We use the fact that first is guaranteed to never overlap 604 * with the last frame in the ring. 605 */ 606 static __inline bool 607 stillvalid_p(struct shmif_sc *sc) 608 { 609 struct shmif_mem *busmem = sc->sc_busmem; 610 unsigned gendiff = busmem->shm_gen - sc->sc_devgen; 611 uint32_t lastoff, devoff; 612 613 KASSERT(busmem->shm_first != busmem->shm_last); 614 615 /* normalize onto a 2x busmem chunk */ 616 devoff = sc->sc_nextpacket; 617 lastoff = shmif_nextpktoff(busmem, busmem->shm_last); 618 619 /* trivial case */ 620 if (gendiff > 1) 621 return false; 622 KASSERT(gendiff <= 1); 623 624 /* Normalize onto 2x busmem chunk */ 625 if (busmem->shm_first >= lastoff) { 626 lastoff += BUSMEM_DATASIZE; 627 if (gendiff == 0) 628 devoff += BUSMEM_DATASIZE; 629 } else { 630 if (gendiff) 631 return false; 632 } 633 634 return devoff >= busmem->shm_first && devoff <= lastoff; 635 } 636 637 static void 638 shmif_rcv(void *arg) 639 { 640 struct ifnet *ifp = arg; 641 struct shmif_sc *sc = ifp->if_softc; 642 struct shmif_mem *busmem; 643 struct mbuf *m = NULL; 644 struct ether_header *eth; 645 uint32_t nextpkt; 646 bool wrap, passup; 647 int error; 648 const int align 649 = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header); 650 651 reup: 652 mutex_enter(&sc->sc_mtx); 653 while ((ifp->if_flags & IFF_RUNNING) == 0 && !sc->sc_dying) 654 cv_wait(&sc->sc_cv, &sc->sc_mtx); 655 mutex_exit(&sc->sc_mtx); 656 657 busmem = sc->sc_busmem; 658 659 while (ifp->if_flags & IFF_RUNNING) { 660 struct shmif_pkthdr sp; 661 662 if (m == NULL) { 663 m = m_gethdr(M_WAIT, MT_DATA); 664 MCLGET(m, M_WAIT); 665 m->m_data += align; 666 } 667 668 DPRINTF(("waiting %d/%" PRIu64 "\n", 669 sc->sc_nextpacket, sc->sc_devgen)); 670 KASSERT(m->m_flags & M_EXT); 671 672 shmif_lockbus(busmem); 673 KASSERT(busmem->shm_magic == SHMIF_MAGIC); 674 KASSERT(busmem->shm_gen >= sc->sc_devgen); 675 676 /* need more data? */ 677 if (sc->sc_devgen == busmem->shm_gen && 678 shmif_nextpktoff(busmem, busmem->shm_last) 679 == sc->sc_nextpacket) { 680 shmif_unlockbus(busmem); 681 error = 0; 682 rumpcomp_shmif_watchwait(sc->sc_kq); 683 if (__predict_false(error)) 684 printf("shmif_rcv: wait failed %d\n", error); 685 membar_consumer(); 686 continue; 687 } 688 689 if (stillvalid_p(sc)) { 690 nextpkt = sc->sc_nextpacket; 691 } else { 692 KASSERT(busmem->shm_gen > 0); 693 nextpkt = busmem->shm_first; 694 if (busmem->shm_first > busmem->shm_last) 695 sc->sc_devgen = busmem->shm_gen - 1; 696 else 697 sc->sc_devgen = busmem->shm_gen; 698 DPRINTF(("dev %p overrun, new data: %d/%" PRIu64 "\n", 699 sc, nextpkt, sc->sc_devgen)); 700 } 701 702 /* 703 * If our read pointer is ahead the bus last write, our 704 * generation must be one behind. 705 */ 706 KASSERT(!(nextpkt > busmem->shm_last 707 && sc->sc_devgen == busmem->shm_gen)); 708 709 wrap = false; 710 nextpkt = shmif_busread(busmem, &sp, 711 nextpkt, sizeof(sp), &wrap); 712 KASSERT(sp.sp_len <= ETHERMTU + ETHER_HDR_LEN); 713 nextpkt = shmif_busread(busmem, mtod(m, void *), 714 nextpkt, sp.sp_len, &wrap); 715 716 DPRINTF(("shmif_rcv: read packet of length %d at %d\n", 717 sp.sp_len, nextpkt)); 718 719 sc->sc_nextpacket = nextpkt; 720 shmif_unlockbus(sc->sc_busmem); 721 722 if (wrap) { 723 sc->sc_devgen++; 724 DPRINTF(("dev %p generation now %" PRIu64 "\n", 725 sc, sc->sc_devgen)); 726 } 727 728 /* 729 * Ignore packets too short to possibly be valid. 730 * This is hit at least for the first frame on a new bus. 731 */ 732 if (__predict_false(sp.sp_len < ETHER_HDR_LEN)) { 733 DPRINTF(("shmif read packet len %d < ETHER_HDR_LEN\n", 734 sp.sp_len)); 735 continue; 736 } 737 738 m->m_len = m->m_pkthdr.len = sp.sp_len; 739 m->m_pkthdr.rcvif = ifp; 740 741 /* 742 * Test if we want to pass the packet upwards 743 */ 744 eth = mtod(m, struct ether_header *); 745 if (memcmp(eth->ether_dhost, CLLADDR(ifp->if_sadl), 746 ETHER_ADDR_LEN) == 0) { 747 passup = true; 748 } else if (ETHER_IS_MULTICAST(eth->ether_dhost)) { 749 passup = true; 750 } else if (ifp->if_flags & IFF_PROMISC) { 751 m->m_flags |= M_PROMISC; 752 passup = true; 753 } else { 754 passup = false; 755 } 756 757 if (passup) { 758 KERNEL_LOCK(1, NULL); 759 bpf_mtap(ifp, m); 760 ifp->if_input(ifp, m); 761 KERNEL_UNLOCK_ONE(NULL); 762 m = NULL; 763 } 764 /* else: reuse mbuf for a future packet */ 765 } 766 m_freem(m); 767 m = NULL; 768 769 if (!sc->sc_dying) 770 goto reup; 771 772 kthread_exit(0); 773 } 774