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