Home | History | Annotate | Line # | Download | only in libshmif
if_shmem.c revision 1.43
      1 /*	$NetBSD: if_shmem.c,v 1.43 2011/09/02 22:25:08 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.43 2011/09/02 22:25:08 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 	vmem_addr_t t;
    288 	int unit, error;
    289 	int memfd = -1; /* XXXgcc */
    290 
    291 	if (path) {
    292 		memfd = rumpuser_open(path, O_RDWR | O_CREAT, &error);
    293 		if (memfd == -1)
    294 			return error;
    295 	}
    296 
    297 	error = vmem_xalloc(shmif_units, 1, 0, 0, 0,
    298 	    VMEM_ADDR_MIN, VMEM_ADDR_MAX, VM_INSTANTFIT | VM_SLEEP, &t);
    299 
    300 	if (error != 0) {
    301 		if (path)
    302 			rumpuser_close(memfd, NULL);
    303 		return error;
    304 	}
    305 
    306 	unit = t - 1;
    307 
    308 	if ((error = allocif(unit, &sc)) != 0) {
    309 		if (path)
    310 			rumpuser_close(memfd, NULL);
    311 		return error;
    312 	}
    313 
    314 	if (!path)
    315 		goto out;
    316 
    317 	error = initbackend(sc, memfd);
    318 	if (error) {
    319 		shmif_unclone(&sc->sc_ec.ec_if);
    320 		return error;
    321 	}
    322 
    323 	sc->sc_backfilelen = strlen(path)+1;
    324 	sc->sc_backfile = kmem_alloc(sc->sc_backfilelen, KM_SLEEP);
    325 	strcpy(sc->sc_backfile, path);
    326 
    327  out:
    328 	if (ifnum)
    329 		*ifnum = unit;
    330 
    331 	return 0;
    332 }
    333 
    334 static int
    335 shmif_clone(struct if_clone *ifc, int unit)
    336 {
    337 	int rc;
    338 	vmem_addr_t unit2;
    339 
    340 	/*
    341 	 * Ok, we know the unit number, but we must still reserve it.
    342 	 * Otherwise the wildcard-side of things might get the same one.
    343 	 * This is slightly offset-happy due to vmem.  First, we offset
    344 	 * the range of unit numbers by +1 since vmem cannot deal with
    345 	 * ranges starting from 0.  Talk about uuuh.
    346 	 */
    347 	rc = vmem_xalloc(shmif_units, 1, 0, 0, 0, unit+1, unit+1,
    348 	    VM_SLEEP | VM_INSTANTFIT, &unit2);
    349 	KASSERT(rc == 0 && unit2-1 == unit);
    350 
    351 	return allocif(unit, NULL);
    352 }
    353 
    354 static int
    355 shmif_unclone(struct ifnet *ifp)
    356 {
    357 	struct shmif_sc *sc = ifp->if_softc;
    358 
    359 	shmif_stop(ifp, 1);
    360 	if_down(ifp);
    361 	finibackend(sc);
    362 
    363 	mutex_enter(&sc->sc_mtx);
    364 	sc->sc_dying = true;
    365 	cv_broadcast(&sc->sc_cv);
    366 	mutex_exit(&sc->sc_mtx);
    367 
    368 	if (sc->sc_rcvl)
    369 		kthread_join(sc->sc_rcvl);
    370 	sc->sc_rcvl = NULL;
    371 
    372 	vmem_xfree(shmif_units, sc->sc_unit+1, 1);
    373 
    374 	ether_ifdetach(ifp);
    375 	if_detach(ifp);
    376 
    377 	cv_destroy(&sc->sc_cv);
    378 	mutex_destroy(&sc->sc_mtx);
    379 
    380 	kmem_free(sc, sizeof(*sc));
    381 
    382 	return 0;
    383 }
    384 
    385 static int
    386 shmif_init(struct ifnet *ifp)
    387 {
    388 	struct shmif_sc *sc = ifp->if_softc;
    389 	int error = 0;
    390 
    391 	if (sc->sc_memfd == -1)
    392 		return ENXIO;
    393 	KASSERT(sc->sc_busmem);
    394 
    395 	ifp->if_flags |= IFF_RUNNING;
    396 
    397 	mutex_enter(&sc->sc_mtx);
    398 	sc->sc_nextpacket = sc->sc_busmem->shm_last;
    399 	sc->sc_devgen = sc->sc_busmem->shm_gen;
    400 
    401 	cv_broadcast(&sc->sc_cv);
    402 	mutex_exit(&sc->sc_mtx);
    403 
    404 	return error;
    405 }
    406 
    407 static int
    408 shmif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    409 {
    410 	struct shmif_sc *sc = ifp->if_softc;
    411 	struct ifdrv *ifd;
    412 	char *path;
    413 	int s, rv, memfd;
    414 
    415 	s = splnet();
    416 	switch (cmd) {
    417 	case SIOCGLINKSTR:
    418 		ifd = data;
    419 
    420 		if (sc->sc_backfilelen == 0) {
    421 			rv = ENOENT;
    422 			break;
    423 		}
    424 
    425 		ifd->ifd_len = sc->sc_backfilelen;
    426 		if (ifd->ifd_cmd == IFLINKSTR_QUERYLEN) {
    427 			rv = 0;
    428 			break;
    429 		}
    430 
    431 		if (ifd->ifd_cmd != 0) {
    432 			rv = EINVAL;
    433 			break;
    434 		}
    435 
    436 		rv = copyoutstr(sc->sc_backfile, ifd->ifd_data,
    437 		    MIN(sc->sc_backfilelen, ifd->ifd_len), NULL);
    438 		break;
    439 	case SIOCSLINKSTR:
    440 		if (ifp->if_flags & IFF_UP) {
    441 			rv = EBUSY;
    442 			break;
    443 		}
    444 
    445 		ifd = data;
    446 		if (ifd->ifd_cmd == IFLINKSTR_UNSET) {
    447 			finibackend(sc);
    448 			rv = 0;
    449 			break;
    450 		} else if (ifd->ifd_cmd != 0) {
    451 			rv = EINVAL;
    452 			break;
    453 		} else if (sc->sc_backfile) {
    454 			rv = EBUSY;
    455 			break;
    456 		}
    457 
    458 		if (ifd->ifd_len > MAXPATHLEN) {
    459 			rv = E2BIG;
    460 			break;
    461 		} else if (ifd->ifd_len < 1) {
    462 			rv = EINVAL;
    463 			break;
    464 		}
    465 
    466 		path = kmem_alloc(ifd->ifd_len, KM_SLEEP);
    467 		rv = copyinstr(ifd->ifd_data, path, ifd->ifd_len, NULL);
    468 		if (rv) {
    469 			kmem_free(path, ifd->ifd_len);
    470 			break;
    471 		}
    472 		memfd = rumpuser_open(path, O_RDWR | O_CREAT, &rv);
    473 		if (memfd == -1) {
    474 			kmem_free(path, ifd->ifd_len);
    475 			break;
    476 		}
    477 		rv = initbackend(sc, memfd);
    478 		if (rv) {
    479 			kmem_free(path, ifd->ifd_len);
    480 			rumpuser_close(memfd, NULL);
    481 			break;
    482 		}
    483 		sc->sc_backfile = path;
    484 		sc->sc_backfilelen = ifd->ifd_len;
    485 
    486 		break;
    487 	default:
    488 		rv = ether_ioctl(ifp, cmd, data);
    489 		if (rv == ENETRESET)
    490 			rv = 0;
    491 		break;
    492 	}
    493 	splx(s);
    494 
    495 	return rv;
    496 }
    497 
    498 /* send everything in-context since it's just a matter of mem-to-mem copy */
    499 static void
    500 shmif_start(struct ifnet *ifp)
    501 {
    502 	struct shmif_sc *sc = ifp->if_softc;
    503 	struct shmif_mem *busmem = sc->sc_busmem;
    504 	struct mbuf *m, *m0;
    505 	uint32_t dataoff;
    506 	uint32_t pktsize, pktwrote;
    507 	bool wrote = false;
    508 	bool wrap;
    509 	int error;
    510 
    511 	ifp->if_flags |= IFF_OACTIVE;
    512 
    513 	for (;;) {
    514 		struct shmif_pkthdr sp;
    515 		struct timeval tv;
    516 
    517 		IF_DEQUEUE(&ifp->if_snd, m0);
    518 		if (m0 == NULL) {
    519 			break;
    520 		}
    521 
    522 		pktsize = 0;
    523 		for (m = m0; m != NULL; m = m->m_next) {
    524 			pktsize += m->m_len;
    525 		}
    526 		KASSERT(pktsize <= ETHERMTU + ETHER_HDR_LEN);
    527 
    528 		getmicrouptime(&tv);
    529 		sp.sp_len = pktsize;
    530 		sp.sp_sec = tv.tv_sec;
    531 		sp.sp_usec = tv.tv_usec;
    532 
    533 		bpf_mtap(ifp, m0);
    534 
    535 		shmif_lockbus(busmem);
    536 		KASSERT(busmem->shm_magic == SHMIF_MAGIC);
    537 		busmem->shm_last = shmif_nextpktoff(busmem, busmem->shm_last);
    538 
    539 		wrap = false;
    540 		dataoff = shmif_buswrite(busmem,
    541 		    busmem->shm_last, &sp, sizeof(sp), &wrap);
    542 		pktwrote = 0;
    543 		for (m = m0; m != NULL; m = m->m_next) {
    544 			pktwrote += m->m_len;
    545 			dataoff = shmif_buswrite(busmem, dataoff,
    546 			    mtod(m, void *), m->m_len, &wrap);
    547 		}
    548 		KASSERT(pktwrote == pktsize);
    549 		if (wrap) {
    550 			busmem->shm_gen++;
    551 			DPRINTF(("bus generation now %d\n", 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/%d\n", sc->sc_nextpacket, sc->sc_devgen));
    654 		KASSERT(m->m_flags & M_EXT);
    655 
    656 		shmif_lockbus(busmem);
    657 		KASSERT(busmem->shm_magic == SHMIF_MAGIC);
    658 		KASSERT(busmem->shm_gen >= sc->sc_devgen);
    659 
    660 		/* need more data? */
    661 		if (sc->sc_devgen == busmem->shm_gen &&
    662 		    shmif_nextpktoff(busmem, busmem->shm_last)
    663 		     == sc->sc_nextpacket) {
    664 			shmif_unlockbus(busmem);
    665 			error = 0;
    666 			rumpuser_writewatchfile_wait(sc->sc_kq, NULL, &error);
    667 			if (__predict_false(error))
    668 				printf("shmif_rcv: wait failed %d\n", error);
    669 			membar_consumer();
    670 			continue;
    671 		}
    672 
    673 		if (stillvalid_p(sc)) {
    674 			nextpkt = sc->sc_nextpacket;
    675 		} else {
    676 			KASSERT(busmem->shm_gen > 0);
    677 			nextpkt = busmem->shm_first;
    678 			if (busmem->shm_first > busmem->shm_last)
    679 				sc->sc_devgen = busmem->shm_gen - 1;
    680 			else
    681 				sc->sc_devgen = busmem->shm_gen;
    682 			DPRINTF(("dev %p overrun, new data: %d/%d\n",
    683 			    sc, nextpkt, sc->sc_devgen));
    684 		}
    685 
    686 		/*
    687 		 * If our read pointer is ahead the bus last write, our
    688 		 * generation must be one behind.
    689 		 */
    690 		KASSERT(!(nextpkt > busmem->shm_last
    691 		    && sc->sc_devgen == busmem->shm_gen));
    692 
    693 		wrap = false;
    694 		nextpkt = shmif_busread(busmem, &sp,
    695 		    nextpkt, sizeof(sp), &wrap);
    696 		KASSERT(sp.sp_len <= ETHERMTU + ETHER_HDR_LEN);
    697 		nextpkt = shmif_busread(busmem, mtod(m, void *),
    698 		    nextpkt, sp.sp_len, &wrap);
    699 
    700 		DPRINTF(("shmif_rcv: read packet of length %d at %d\n",
    701 		    sp.sp_len, nextpkt));
    702 
    703 		sc->sc_nextpacket = nextpkt;
    704 		shmif_unlockbus(sc->sc_busmem);
    705 
    706 		if (wrap) {
    707 			sc->sc_devgen++;
    708 			DPRINTF(("dev %p generation now %d\n",
    709 			    sc, sc->sc_devgen));
    710 		}
    711 
    712 		m->m_len = m->m_pkthdr.len = sp.sp_len;
    713 		m->m_pkthdr.rcvif = ifp;
    714 
    715 		/*
    716 		 * Test if we want to pass the packet upwards
    717 		 */
    718 		eth = mtod(m, struct ether_header *);
    719 		if (memcmp(eth->ether_dhost, CLLADDR(ifp->if_sadl),
    720 		    ETHER_ADDR_LEN) == 0) {
    721 			passup = true;
    722 		} else if (memcmp(eth->ether_dhost, etherbroadcastaddr,
    723 		    ETHER_ADDR_LEN) == 0) {
    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