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