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