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