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