Home | History | Annotate | Line # | Download | only in libshmif
if_shmem.c revision 1.42
      1 /*	$NetBSD: if_shmem.c,v 1.42 2011/08/25 15:14:19 dyoung 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.42 2011/08/25 15:14:19 dyoung 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_MUSTJOIN, 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,
    297 	    VMEM_ADDR_MIN, VMEM_ADDR_MAX, 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.  Talk about uuuh.
    336 	 */
    337 	unit2 = vmem_xalloc(shmif_units, 1, 0, 0, 0, unit+1, unit+1,
    338 	    VM_SLEEP | VM_INSTANTFIT);
    339 	KASSERT(unit2-1 == unit);
    340 
    341 	return allocif(unit, NULL);
    342 }
    343 
    344 static int
    345 shmif_unclone(struct ifnet *ifp)
    346 {
    347 	struct shmif_sc *sc = ifp->if_softc;
    348 
    349 	shmif_stop(ifp, 1);
    350 	if_down(ifp);
    351 	finibackend(sc);
    352 
    353 	mutex_enter(&sc->sc_mtx);
    354 	sc->sc_dying = true;
    355 	cv_broadcast(&sc->sc_cv);
    356 	mutex_exit(&sc->sc_mtx);
    357 
    358 	if (sc->sc_rcvl)
    359 		kthread_join(sc->sc_rcvl);
    360 	sc->sc_rcvl = NULL;
    361 
    362 	vmem_xfree(shmif_units, sc->sc_unit+1, 1);
    363 
    364 	ether_ifdetach(ifp);
    365 	if_detach(ifp);
    366 
    367 	cv_destroy(&sc->sc_cv);
    368 	mutex_destroy(&sc->sc_mtx);
    369 
    370 	kmem_free(sc, sizeof(*sc));
    371 
    372 	return 0;
    373 }
    374 
    375 static int
    376 shmif_init(struct ifnet *ifp)
    377 {
    378 	struct shmif_sc *sc = ifp->if_softc;
    379 	int error = 0;
    380 
    381 	if (sc->sc_memfd == -1)
    382 		return ENXIO;
    383 	KASSERT(sc->sc_busmem);
    384 
    385 	ifp->if_flags |= IFF_RUNNING;
    386 
    387 	mutex_enter(&sc->sc_mtx);
    388 	sc->sc_nextpacket = sc->sc_busmem->shm_last;
    389 	sc->sc_devgen = sc->sc_busmem->shm_gen;
    390 
    391 	cv_broadcast(&sc->sc_cv);
    392 	mutex_exit(&sc->sc_mtx);
    393 
    394 	return error;
    395 }
    396 
    397 static int
    398 shmif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    399 {
    400 	struct shmif_sc *sc = ifp->if_softc;
    401 	struct ifdrv *ifd;
    402 	char *path;
    403 	int s, rv, memfd;
    404 
    405 	s = splnet();
    406 	switch (cmd) {
    407 	case SIOCGLINKSTR:
    408 		ifd = data;
    409 
    410 		if (sc->sc_backfilelen == 0) {
    411 			rv = ENOENT;
    412 			break;
    413 		}
    414 
    415 		ifd->ifd_len = sc->sc_backfilelen;
    416 		if (ifd->ifd_cmd == IFLINKSTR_QUERYLEN) {
    417 			rv = 0;
    418 			break;
    419 		}
    420 
    421 		if (ifd->ifd_cmd != 0) {
    422 			rv = EINVAL;
    423 			break;
    424 		}
    425 
    426 		rv = copyoutstr(sc->sc_backfile, ifd->ifd_data,
    427 		    MIN(sc->sc_backfilelen, ifd->ifd_len), NULL);
    428 		break;
    429 	case SIOCSLINKSTR:
    430 		if (ifp->if_flags & IFF_UP) {
    431 			rv = EBUSY;
    432 			break;
    433 		}
    434 
    435 		ifd = data;
    436 		if (ifd->ifd_cmd == IFLINKSTR_UNSET) {
    437 			finibackend(sc);
    438 			rv = 0;
    439 			break;
    440 		} else if (ifd->ifd_cmd != 0) {
    441 			rv = EINVAL;
    442 			break;
    443 		} else if (sc->sc_backfile) {
    444 			rv = EBUSY;
    445 			break;
    446 		}
    447 
    448 		if (ifd->ifd_len > MAXPATHLEN) {
    449 			rv = E2BIG;
    450 			break;
    451 		} else if (ifd->ifd_len < 1) {
    452 			rv = EINVAL;
    453 			break;
    454 		}
    455 
    456 		path = kmem_alloc(ifd->ifd_len, KM_SLEEP);
    457 		rv = copyinstr(ifd->ifd_data, path, ifd->ifd_len, NULL);
    458 		if (rv) {
    459 			kmem_free(path, ifd->ifd_len);
    460 			break;
    461 		}
    462 		memfd = rumpuser_open(path, O_RDWR | O_CREAT, &rv);
    463 		if (memfd == -1) {
    464 			kmem_free(path, ifd->ifd_len);
    465 			break;
    466 		}
    467 		rv = initbackend(sc, memfd);
    468 		if (rv) {
    469 			kmem_free(path, ifd->ifd_len);
    470 			rumpuser_close(memfd, NULL);
    471 			break;
    472 		}
    473 		sc->sc_backfile = path;
    474 		sc->sc_backfilelen = ifd->ifd_len;
    475 
    476 		break;
    477 	default:
    478 		rv = ether_ioctl(ifp, cmd, data);
    479 		if (rv == ENETRESET)
    480 			rv = 0;
    481 		break;
    482 	}
    483 	splx(s);
    484 
    485 	return rv;
    486 }
    487 
    488 /* send everything in-context since it's just a matter of mem-to-mem copy */
    489 static void
    490 shmif_start(struct ifnet *ifp)
    491 {
    492 	struct shmif_sc *sc = ifp->if_softc;
    493 	struct shmif_mem *busmem = sc->sc_busmem;
    494 	struct mbuf *m, *m0;
    495 	uint32_t dataoff;
    496 	uint32_t pktsize, pktwrote;
    497 	bool wrote = false;
    498 	bool wrap;
    499 	int error;
    500 
    501 	ifp->if_flags |= IFF_OACTIVE;
    502 
    503 	for (;;) {
    504 		struct shmif_pkthdr sp;
    505 		struct timeval tv;
    506 
    507 		IF_DEQUEUE(&ifp->if_snd, m0);
    508 		if (m0 == NULL) {
    509 			break;
    510 		}
    511 
    512 		pktsize = 0;
    513 		for (m = m0; m != NULL; m = m->m_next) {
    514 			pktsize += m->m_len;
    515 		}
    516 		KASSERT(pktsize <= ETHERMTU + ETHER_HDR_LEN);
    517 
    518 		getmicrouptime(&tv);
    519 		sp.sp_len = pktsize;
    520 		sp.sp_sec = tv.tv_sec;
    521 		sp.sp_usec = tv.tv_usec;
    522 
    523 		bpf_mtap(ifp, m0);
    524 
    525 		shmif_lockbus(busmem);
    526 		KASSERT(busmem->shm_magic == SHMIF_MAGIC);
    527 		busmem->shm_last = shmif_nextpktoff(busmem, busmem->shm_last);
    528 
    529 		wrap = false;
    530 		dataoff = shmif_buswrite(busmem,
    531 		    busmem->shm_last, &sp, sizeof(sp), &wrap);
    532 		pktwrote = 0;
    533 		for (m = m0; m != NULL; m = m->m_next) {
    534 			pktwrote += m->m_len;
    535 			dataoff = shmif_buswrite(busmem, dataoff,
    536 			    mtod(m, void *), m->m_len, &wrap);
    537 		}
    538 		KASSERT(pktwrote == pktsize);
    539 		if (wrap) {
    540 			busmem->shm_gen++;
    541 			DPRINTF(("bus generation now %d\n", busmem->shm_gen));
    542 		}
    543 		shmif_unlockbus(busmem);
    544 
    545 		m_freem(m0);
    546 		wrote = true;
    547 
    548 		DPRINTF(("shmif_start: send %d bytes at off %d\n",
    549 		    pktsize, busmem->shm_last));
    550 	}
    551 
    552 	ifp->if_flags &= ~IFF_OACTIVE;
    553 
    554 	/* wakeup? */
    555 	if (wrote)
    556 		rumpuser_pwrite(sc->sc_memfd,
    557 		    &busversion, sizeof(busversion), IFMEM_WAKEUP, &error);
    558 }
    559 
    560 static void
    561 shmif_stop(struct ifnet *ifp, int disable)
    562 {
    563 	struct shmif_sc *sc = ifp->if_softc;
    564 
    565 	ifp->if_flags &= ~IFF_RUNNING;
    566 	membar_producer();
    567 
    568 	/*
    569 	 * wakeup thread.  this will of course wake up all bus
    570 	 * listeners, but that's life.
    571 	 */
    572 	if (sc->sc_memfd != -1)
    573 		rumpuser_pwrite(sc->sc_memfd,
    574 		    &busversion, sizeof(busversion), IFMEM_WAKEUP, NULL);
    575 }
    576 
    577 
    578 /*
    579  * Check if we have been sleeping too long.  Basically,
    580  * our in-sc nextpkt must by first <= nextpkt <= last"+1".
    581  * We use the fact that first is guaranteed to never overlap
    582  * with the last frame in the ring.
    583  */
    584 static __inline bool
    585 stillvalid_p(struct shmif_sc *sc)
    586 {
    587 	struct shmif_mem *busmem = sc->sc_busmem;
    588 	unsigned gendiff = busmem->shm_gen - sc->sc_devgen;
    589 	uint32_t lastoff, devoff;
    590 
    591 	KASSERT(busmem->shm_first != busmem->shm_last);
    592 
    593 	/* normalize onto a 2x busmem chunk */
    594 	devoff = sc->sc_nextpacket;
    595 	lastoff = shmif_nextpktoff(busmem, busmem->shm_last);
    596 
    597 	/* trivial case */
    598 	if (gendiff > 1)
    599 		return false;
    600 	KASSERT(gendiff <= 1);
    601 
    602 	/* Normalize onto 2x busmem chunk */
    603 	if (busmem->shm_first >= lastoff) {
    604 		lastoff += BUSMEM_DATASIZE;
    605 		if (gendiff == 0)
    606 			devoff += BUSMEM_DATASIZE;
    607 	} else {
    608 		if (gendiff)
    609 			return false;
    610 	}
    611 
    612 	return devoff >= busmem->shm_first && devoff <= lastoff;
    613 }
    614 
    615 static void
    616 shmif_rcv(void *arg)
    617 {
    618 	struct ifnet *ifp = arg;
    619 	struct shmif_sc *sc = ifp->if_softc;
    620 	struct shmif_mem *busmem;
    621 	struct mbuf *m = NULL;
    622 	struct ether_header *eth;
    623 	uint32_t nextpkt;
    624 	bool wrap, passup;
    625 	int error;
    626 
    627  reup:
    628 	mutex_enter(&sc->sc_mtx);
    629 	while ((ifp->if_flags & IFF_RUNNING) == 0 && !sc->sc_dying)
    630 		cv_wait(&sc->sc_cv, &sc->sc_mtx);
    631 	mutex_exit(&sc->sc_mtx);
    632 
    633 	busmem = sc->sc_busmem;
    634 
    635 	while (ifp->if_flags & IFF_RUNNING) {
    636 		struct shmif_pkthdr sp;
    637 
    638 		if (m == NULL) {
    639 			m = m_gethdr(M_WAIT, MT_DATA);
    640 			MCLGET(m, M_WAIT);
    641 		}
    642 
    643 		DPRINTF(("waiting %d/%d\n", sc->sc_nextpacket, sc->sc_devgen));
    644 		KASSERT(m->m_flags & M_EXT);
    645 
    646 		shmif_lockbus(busmem);
    647 		KASSERT(busmem->shm_magic == SHMIF_MAGIC);
    648 		KASSERT(busmem->shm_gen >= sc->sc_devgen);
    649 
    650 		/* need more data? */
    651 		if (sc->sc_devgen == busmem->shm_gen &&
    652 		    shmif_nextpktoff(busmem, busmem->shm_last)
    653 		     == sc->sc_nextpacket) {
    654 			shmif_unlockbus(busmem);
    655 			error = 0;
    656 			rumpuser_writewatchfile_wait(sc->sc_kq, NULL, &error);
    657 			if (__predict_false(error))
    658 				printf("shmif_rcv: wait failed %d\n", error);
    659 			membar_consumer();
    660 			continue;
    661 		}
    662 
    663 		if (stillvalid_p(sc)) {
    664 			nextpkt = sc->sc_nextpacket;
    665 		} else {
    666 			KASSERT(busmem->shm_gen > 0);
    667 			nextpkt = busmem->shm_first;
    668 			if (busmem->shm_first > busmem->shm_last)
    669 				sc->sc_devgen = busmem->shm_gen - 1;
    670 			else
    671 				sc->sc_devgen = busmem->shm_gen;
    672 			DPRINTF(("dev %p overrun, new data: %d/%d\n",
    673 			    sc, nextpkt, sc->sc_devgen));
    674 		}
    675 
    676 		/*
    677 		 * If our read pointer is ahead the bus last write, our
    678 		 * generation must be one behind.
    679 		 */
    680 		KASSERT(!(nextpkt > busmem->shm_last
    681 		    && sc->sc_devgen == busmem->shm_gen));
    682 
    683 		wrap = false;
    684 		nextpkt = shmif_busread(busmem, &sp,
    685 		    nextpkt, sizeof(sp), &wrap);
    686 		KASSERT(sp.sp_len <= ETHERMTU + ETHER_HDR_LEN);
    687 		nextpkt = shmif_busread(busmem, mtod(m, void *),
    688 		    nextpkt, sp.sp_len, &wrap);
    689 
    690 		DPRINTF(("shmif_rcv: read packet of length %d at %d\n",
    691 		    sp.sp_len, nextpkt));
    692 
    693 		sc->sc_nextpacket = nextpkt;
    694 		shmif_unlockbus(sc->sc_busmem);
    695 
    696 		if (wrap) {
    697 			sc->sc_devgen++;
    698 			DPRINTF(("dev %p generation now %d\n",
    699 			    sc, sc->sc_devgen));
    700 		}
    701 
    702 		m->m_len = m->m_pkthdr.len = sp.sp_len;
    703 		m->m_pkthdr.rcvif = ifp;
    704 
    705 		/*
    706 		 * Test if we want to pass the packet upwards
    707 		 */
    708 		eth = mtod(m, struct ether_header *);
    709 		if (memcmp(eth->ether_dhost, CLLADDR(ifp->if_sadl),
    710 		    ETHER_ADDR_LEN) == 0) {
    711 			passup = true;
    712 		} else if (memcmp(eth->ether_dhost, etherbroadcastaddr,
    713 		    ETHER_ADDR_LEN) == 0) {
    714 			passup = true;
    715 		} else if (ifp->if_flags & IFF_PROMISC) {
    716 			m->m_flags |= M_PROMISC;
    717 			passup = true;
    718 		} else {
    719 			passup = false;
    720 		}
    721 
    722 		if (passup) {
    723 			KERNEL_LOCK(1, NULL);
    724 			bpf_mtap(ifp, m);
    725 			ifp->if_input(ifp, m);
    726 			KERNEL_UNLOCK_ONE(NULL);
    727 			m = NULL;
    728 		}
    729 		/* else: reuse mbuf for a future packet */
    730 	}
    731 	m_freem(m);
    732 	m = NULL;
    733 
    734 	if (!sc->sc_dying)
    735 		goto reup;
    736 
    737 	kthread_exit(0);
    738 }
    739