Home | History | Annotate | Line # | Download | only in libshmif
if_shmem.c revision 1.51
      1 /*	$NetBSD: if_shmem.c,v 1.51 2013/04/29 13:17:32 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.51 2013/04/29 13:17:32 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 = rumpcomp_shmif_mmap(memfd, BUSMEM_SIZE, &error);
    209 	if (error)
    210 		return error;
    211 
    212 	if (sc->sc_busmem->shm_magic
    213 	    && sc->sc_busmem->shm_magic != SHMIF_MAGIC) {
    214 		printf("bus is not magical");
    215 		rumpuser_unmap(sc->sc_busmem, BUSMEM_SIZE);
    216 		return ENOEXEC;
    217 	}
    218 
    219 	/*
    220 	 * Prefault in pages to minimize runtime penalty with buslock.
    221 	 * Use 512 instead of PAGE_SIZE to make sure we catch cases where
    222 	 * rump kernel PAGE_SIZE > host page size.
    223 	 */
    224 	for (p = (uint8_t *)sc->sc_busmem;
    225 	    p < (uint8_t *)sc->sc_busmem + BUSMEM_SIZE;
    226 	    p += 512)
    227 		v = *p;
    228 
    229 	shmif_lockbus(sc->sc_busmem);
    230 	/* we're first?  initialize bus */
    231 	if (sc->sc_busmem->shm_magic == 0) {
    232 		sc->sc_busmem->shm_magic = SHMIF_MAGIC;
    233 		sc->sc_busmem->shm_first = BUSMEM_DATASIZE;
    234 	}
    235 
    236 	sc->sc_nextpacket = sc->sc_busmem->shm_last;
    237 	sc->sc_devgen = sc->sc_busmem->shm_gen;
    238 
    239 #ifdef PREFAULT_RW
    240 	for (p = (uint8_t *)sc->sc_busmem;
    241 	    p < (uint8_t *)sc->sc_busmem + BUSMEM_SIZE;
    242 	    p += PAGE_SIZE) {
    243 		v = *p;
    244 		*p = v;
    245 	}
    246 #endif
    247 	shmif_unlockbus(sc->sc_busmem);
    248 
    249 	sc->sc_kq = rumpcomp_shmif_watchsetup(-1, memfd, &error);
    250 	if (sc->sc_kq == -1) {
    251 		rumpuser_unmap(sc->sc_busmem, BUSMEM_SIZE);
    252 		return error;
    253 	}
    254 
    255 	sc->sc_memfd = memfd;
    256 
    257 	return error;
    258 }
    259 
    260 static void
    261 finibackend(struct shmif_sc *sc)
    262 {
    263 
    264 	if (sc->sc_backfile == NULL)
    265 		return;
    266 
    267 	if (sc->sc_backfile) {
    268 		kmem_free(sc->sc_backfile, sc->sc_backfilelen);
    269 		sc->sc_backfile = NULL;
    270 		sc->sc_backfilelen = 0;
    271 	}
    272 
    273 	rumpuser_unmap(sc->sc_busmem, BUSMEM_SIZE);
    274 	rumpuser_close(sc->sc_memfd, NULL);
    275 	rumpuser_close(sc->sc_kq, NULL);
    276 
    277 	sc->sc_memfd = -1;
    278 }
    279 
    280 int
    281 rump_shmif_create(const char *path, int *ifnum)
    282 {
    283 	struct shmif_sc *sc;
    284 	vmem_addr_t t;
    285 	int unit, error;
    286 	int memfd = -1; /* XXXgcc */
    287 
    288 	if (path) {
    289 		memfd = rumpuser_open(path,
    290 		    RUMPUSER_OPEN_RDWR | RUMPUSER_OPEN_CREATE, &error);
    291 		if (memfd == -1)
    292 			return error;
    293 	}
    294 
    295 	error = vmem_xalloc(shmif_units, 1, 0, 0, 0,
    296 	    VMEM_ADDR_MIN, VMEM_ADDR_MAX, VM_INSTANTFIT | VM_SLEEP, &t);
    297 
    298 	if (error != 0) {
    299 		if (path)
    300 			rumpuser_close(memfd, NULL);
    301 		return error;
    302 	}
    303 
    304 	unit = t - 1;
    305 
    306 	if ((error = allocif(unit, &sc)) != 0) {
    307 		if (path)
    308 			rumpuser_close(memfd, NULL);
    309 		return error;
    310 	}
    311 
    312 	if (!path)
    313 		goto out;
    314 
    315 	error = initbackend(sc, memfd);
    316 	if (error) {
    317 		shmif_unclone(&sc->sc_ec.ec_if);
    318 		return error;
    319 	}
    320 
    321 	sc->sc_backfilelen = strlen(path)+1;
    322 	sc->sc_backfile = kmem_alloc(sc->sc_backfilelen, KM_SLEEP);
    323 	strcpy(sc->sc_backfile, path);
    324 
    325  out:
    326 	if (ifnum)
    327 		*ifnum = unit;
    328 
    329 	return 0;
    330 }
    331 
    332 static int
    333 shmif_clone(struct if_clone *ifc, int unit)
    334 {
    335 	int rc;
    336 	vmem_addr_t unit2;
    337 
    338 	/*
    339 	 * Ok, we know the unit number, but we must still reserve it.
    340 	 * Otherwise the wildcard-side of things might get the same one.
    341 	 * This is slightly offset-happy due to vmem.  First, we offset
    342 	 * the range of unit numbers by +1 since vmem cannot deal with
    343 	 * ranges starting from 0.  Talk about uuuh.
    344 	 */
    345 	rc = vmem_xalloc(shmif_units, 1, 0, 0, 0, unit+1, unit+1,
    346 	    VM_SLEEP | VM_INSTANTFIT, &unit2);
    347 	KASSERT(rc == 0 && unit2-1 == unit);
    348 
    349 	return allocif(unit, NULL);
    350 }
    351 
    352 static int
    353 shmif_unclone(struct ifnet *ifp)
    354 {
    355 	struct shmif_sc *sc = ifp->if_softc;
    356 
    357 	shmif_stop(ifp, 1);
    358 	if_down(ifp);
    359 	finibackend(sc);
    360 
    361 	mutex_enter(&sc->sc_mtx);
    362 	sc->sc_dying = true;
    363 	cv_broadcast(&sc->sc_cv);
    364 	mutex_exit(&sc->sc_mtx);
    365 
    366 	if (sc->sc_rcvl)
    367 		kthread_join(sc->sc_rcvl);
    368 	sc->sc_rcvl = NULL;
    369 
    370 	vmem_xfree(shmif_units, sc->sc_unit+1, 1);
    371 
    372 	ether_ifdetach(ifp);
    373 	if_detach(ifp);
    374 
    375 	cv_destroy(&sc->sc_cv);
    376 	mutex_destroy(&sc->sc_mtx);
    377 
    378 	kmem_free(sc, sizeof(*sc));
    379 
    380 	return 0;
    381 }
    382 
    383 static int
    384 shmif_init(struct ifnet *ifp)
    385 {
    386 	struct shmif_sc *sc = ifp->if_softc;
    387 	int error = 0;
    388 
    389 	if (sc->sc_memfd == -1)
    390 		return ENXIO;
    391 	KASSERT(sc->sc_busmem);
    392 
    393 	ifp->if_flags |= IFF_RUNNING;
    394 
    395 	mutex_enter(&sc->sc_mtx);
    396 	sc->sc_nextpacket = sc->sc_busmem->shm_last;
    397 	sc->sc_devgen = sc->sc_busmem->shm_gen;
    398 
    399 	cv_broadcast(&sc->sc_cv);
    400 	mutex_exit(&sc->sc_mtx);
    401 
    402 	return error;
    403 }
    404 
    405 static int
    406 shmif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    407 {
    408 	struct shmif_sc *sc = ifp->if_softc;
    409 	struct ifdrv *ifd;
    410 	char *path;
    411 	int s, rv, memfd;
    412 
    413 	s = splnet();
    414 	switch (cmd) {
    415 	case SIOCGLINKSTR:
    416 		ifd = data;
    417 
    418 		if (sc->sc_backfilelen == 0) {
    419 			rv = ENOENT;
    420 			break;
    421 		}
    422 
    423 		ifd->ifd_len = sc->sc_backfilelen;
    424 		if (ifd->ifd_cmd == IFLINKSTR_QUERYLEN) {
    425 			rv = 0;
    426 			break;
    427 		}
    428 
    429 		if (ifd->ifd_cmd != 0) {
    430 			rv = EINVAL;
    431 			break;
    432 		}
    433 
    434 		rv = copyoutstr(sc->sc_backfile, ifd->ifd_data,
    435 		    MIN(sc->sc_backfilelen, ifd->ifd_len), NULL);
    436 		break;
    437 	case SIOCSLINKSTR:
    438 		if (ifp->if_flags & IFF_UP) {
    439 			rv = EBUSY;
    440 			break;
    441 		}
    442 
    443 		ifd = data;
    444 		if (ifd->ifd_cmd == IFLINKSTR_UNSET) {
    445 			finibackend(sc);
    446 			rv = 0;
    447 			break;
    448 		} else if (ifd->ifd_cmd != 0) {
    449 			rv = EINVAL;
    450 			break;
    451 		} else if (sc->sc_backfile) {
    452 			rv = EBUSY;
    453 			break;
    454 		}
    455 
    456 		if (ifd->ifd_len > MAXPATHLEN) {
    457 			rv = E2BIG;
    458 			break;
    459 		} else if (ifd->ifd_len < 1) {
    460 			rv = EINVAL;
    461 			break;
    462 		}
    463 
    464 		path = kmem_alloc(ifd->ifd_len, KM_SLEEP);
    465 		rv = copyinstr(ifd->ifd_data, path, ifd->ifd_len, NULL);
    466 		if (rv) {
    467 			kmem_free(path, ifd->ifd_len);
    468 			break;
    469 		}
    470 		memfd = rumpuser_open(path,
    471 		    RUMPUSER_OPEN_RDWR | RUMPUSER_OPEN_CREATE, &rv);
    472 		if (memfd == -1) {
    473 			kmem_free(path, ifd->ifd_len);
    474 			break;
    475 		}
    476 		rv = initbackend(sc, memfd);
    477 		if (rv) {
    478 			kmem_free(path, ifd->ifd_len);
    479 			rumpuser_close(memfd, NULL);
    480 			break;
    481 		}
    482 		sc->sc_backfile = path;
    483 		sc->sc_backfilelen = ifd->ifd_len;
    484 
    485 		break;
    486 	default:
    487 		rv = ether_ioctl(ifp, cmd, data);
    488 		if (rv == ENETRESET)
    489 			rv = 0;
    490 		break;
    491 	}
    492 	splx(s);
    493 
    494 	return rv;
    495 }
    496 
    497 /* send everything in-context since it's just a matter of mem-to-mem copy */
    498 static void
    499 shmif_start(struct ifnet *ifp)
    500 {
    501 	struct shmif_sc *sc = ifp->if_softc;
    502 	struct shmif_mem *busmem = sc->sc_busmem;
    503 	struct mbuf *m, *m0;
    504 	uint32_t dataoff;
    505 	uint32_t pktsize, pktwrote;
    506 	bool wrote = false;
    507 	bool wrap;
    508 	int error;
    509 
    510 	ifp->if_flags |= IFF_OACTIVE;
    511 
    512 	for (;;) {
    513 		struct shmif_pkthdr sp;
    514 		struct timeval tv;
    515 
    516 		IF_DEQUEUE(&ifp->if_snd, m0);
    517 		if (m0 == NULL) {
    518 			break;
    519 		}
    520 
    521 		pktsize = 0;
    522 		for (m = m0; m != NULL; m = m->m_next) {
    523 			pktsize += m->m_len;
    524 		}
    525 		KASSERT(pktsize <= ETHERMTU + ETHER_HDR_LEN);
    526 
    527 		getmicrouptime(&tv);
    528 		sp.sp_len = pktsize;
    529 		sp.sp_sec = tv.tv_sec;
    530 		sp.sp_usec = tv.tv_usec;
    531 
    532 		bpf_mtap(ifp, m0);
    533 
    534 		shmif_lockbus(busmem);
    535 		KASSERT(busmem->shm_magic == SHMIF_MAGIC);
    536 		busmem->shm_last = shmif_nextpktoff(busmem, busmem->shm_last);
    537 
    538 		wrap = false;
    539 		dataoff = shmif_buswrite(busmem,
    540 		    busmem->shm_last, &sp, sizeof(sp), &wrap);
    541 		pktwrote = 0;
    542 		for (m = m0; m != NULL; m = m->m_next) {
    543 			pktwrote += m->m_len;
    544 			dataoff = shmif_buswrite(busmem, dataoff,
    545 			    mtod(m, void *), m->m_len, &wrap);
    546 		}
    547 		KASSERT(pktwrote == pktsize);
    548 		if (wrap) {
    549 			busmem->shm_gen++;
    550 			DPRINTF(("bus generation now %" PRIu64 "\n",
    551 			    busmem->shm_gen));
    552 		}
    553 		shmif_unlockbus(busmem);
    554 
    555 		m_freem(m0);
    556 		wrote = true;
    557 
    558 		DPRINTF(("shmif_start: send %d bytes at off %d\n",
    559 		    pktsize, busmem->shm_last));
    560 	}
    561 
    562 	ifp->if_flags &= ~IFF_OACTIVE;
    563 
    564 	/* wakeup? */
    565 	if (wrote)
    566 		rumpuser_pwrite(sc->sc_memfd,
    567 		    &busversion, sizeof(busversion), IFMEM_WAKEUP, &error);
    568 }
    569 
    570 static void
    571 shmif_stop(struct ifnet *ifp, int disable)
    572 {
    573 	struct shmif_sc *sc = ifp->if_softc;
    574 
    575 	ifp->if_flags &= ~IFF_RUNNING;
    576 	membar_producer();
    577 
    578 	/*
    579 	 * wakeup thread.  this will of course wake up all bus
    580 	 * listeners, but that's life.
    581 	 */
    582 	if (sc->sc_memfd != -1)
    583 		rumpuser_pwrite(sc->sc_memfd,
    584 		    &busversion, sizeof(busversion), IFMEM_WAKEUP, NULL);
    585 }
    586 
    587 
    588 /*
    589  * Check if we have been sleeping too long.  Basically,
    590  * our in-sc nextpkt must by first <= nextpkt <= last"+1".
    591  * We use the fact that first is guaranteed to never overlap
    592  * with the last frame in the ring.
    593  */
    594 static __inline bool
    595 stillvalid_p(struct shmif_sc *sc)
    596 {
    597 	struct shmif_mem *busmem = sc->sc_busmem;
    598 	unsigned gendiff = busmem->shm_gen - sc->sc_devgen;
    599 	uint32_t lastoff, devoff;
    600 
    601 	KASSERT(busmem->shm_first != busmem->shm_last);
    602 
    603 	/* normalize onto a 2x busmem chunk */
    604 	devoff = sc->sc_nextpacket;
    605 	lastoff = shmif_nextpktoff(busmem, busmem->shm_last);
    606 
    607 	/* trivial case */
    608 	if (gendiff > 1)
    609 		return false;
    610 	KASSERT(gendiff <= 1);
    611 
    612 	/* Normalize onto 2x busmem chunk */
    613 	if (busmem->shm_first >= lastoff) {
    614 		lastoff += BUSMEM_DATASIZE;
    615 		if (gendiff == 0)
    616 			devoff += BUSMEM_DATASIZE;
    617 	} else {
    618 		if (gendiff)
    619 			return false;
    620 	}
    621 
    622 	return devoff >= busmem->shm_first && devoff <= lastoff;
    623 }
    624 
    625 static void
    626 shmif_rcv(void *arg)
    627 {
    628 	struct ifnet *ifp = arg;
    629 	struct shmif_sc *sc = ifp->if_softc;
    630 	struct shmif_mem *busmem;
    631 	struct mbuf *m = NULL;
    632 	struct ether_header *eth;
    633 	uint32_t nextpkt;
    634 	bool wrap, passup;
    635 	int error;
    636 
    637  reup:
    638 	mutex_enter(&sc->sc_mtx);
    639 	while ((ifp->if_flags & IFF_RUNNING) == 0 && !sc->sc_dying)
    640 		cv_wait(&sc->sc_cv, &sc->sc_mtx);
    641 	mutex_exit(&sc->sc_mtx);
    642 
    643 	busmem = sc->sc_busmem;
    644 
    645 	while (ifp->if_flags & IFF_RUNNING) {
    646 		struct shmif_pkthdr sp;
    647 
    648 		if (m == NULL) {
    649 			m = m_gethdr(M_WAIT, MT_DATA);
    650 			MCLGET(m, M_WAIT);
    651 		}
    652 
    653 		DPRINTF(("waiting %d/%" PRIu64 "\n",
    654 		    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 			rumpcomp_shmif_watchwait(sc->sc_kq, &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/%" PRIu64 "\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 %" PRIu64 "\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 (ETHER_IS_MULTICAST(eth->ether_dhost)) {
    724 			passup = true;
    725 		} else if (ifp->if_flags & IFF_PROMISC) {
    726 			m->m_flags |= M_PROMISC;
    727 			passup = true;
    728 		} else {
    729 			passup = false;
    730 		}
    731 
    732 		if (passup) {
    733 			KERNEL_LOCK(1, NULL);
    734 			bpf_mtap(ifp, m);
    735 			ifp->if_input(ifp, m);
    736 			KERNEL_UNLOCK_ONE(NULL);
    737 			m = NULL;
    738 		}
    739 		/* else: reuse mbuf for a future packet */
    740 	}
    741 	m_freem(m);
    742 	m = NULL;
    743 
    744 	if (!sc->sc_dying)
    745 		goto reup;
    746 
    747 	kthread_exit(0);
    748 }
    749