Home | History | Annotate | Line # | Download | only in libshmif
if_shmem.c revision 1.10.2.2
      1  1.10.2.1  uebayasi /*	$NetBSD: if_shmem.c,v 1.10.2.2 2010/10/22 07:22:53 uebayasi Exp $	*/
      2       1.1     pooka 
      3       1.1     pooka /*
      4       1.1     pooka  * Copyright (c) 2009 Antti Kantee.  All Rights Reserved.
      5       1.1     pooka  *
      6       1.1     pooka  * Development of this software was supported by The Nokia Foundation.
      7       1.1     pooka  *
      8       1.1     pooka  * Redistribution and use in source and binary forms, with or without
      9       1.1     pooka  * modification, are permitted provided that the following conditions
     10       1.1     pooka  * are met:
     11       1.1     pooka  * 1. Redistributions of source code must retain the above copyright
     12       1.1     pooka  *    notice, this list of conditions and the following disclaimer.
     13       1.1     pooka  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1     pooka  *    notice, this list of conditions and the following disclaimer in the
     15       1.1     pooka  *    documentation and/or other materials provided with the distribution.
     16       1.1     pooka  *
     17       1.1     pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     18       1.1     pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19       1.1     pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20       1.1     pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21       1.1     pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22       1.1     pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     23       1.1     pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24       1.1     pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25       1.1     pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26       1.1     pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27       1.1     pooka  * SUCH DAMAGE.
     28       1.1     pooka  */
     29       1.1     pooka 
     30       1.1     pooka #include <sys/cdefs.h>
     31  1.10.2.1  uebayasi __KERNEL_RCSID(0, "$NetBSD: if_shmem.c,v 1.10.2.2 2010/10/22 07:22:53 uebayasi Exp $");
     32       1.1     pooka 
     33       1.1     pooka #include <sys/param.h>
     34  1.10.2.1  uebayasi #include <sys/atomic.h>
     35       1.1     pooka #include <sys/fcntl.h>
     36       1.1     pooka #include <sys/kmem.h>
     37       1.1     pooka #include <sys/kthread.h>
     38       1.1     pooka #include <sys/lock.h>
     39       1.3    martin #include <sys/atomic.h>
     40       1.1     pooka 
     41       1.1     pooka #include <net/if.h>
     42       1.1     pooka #include <net/if_ether.h>
     43       1.1     pooka 
     44       1.1     pooka #include <netinet/in.h>
     45       1.1     pooka #include <netinet/in_var.h>
     46       1.1     pooka 
     47       1.1     pooka #include <rump/rump.h>
     48       1.1     pooka #include <rump/rumpuser.h>
     49       1.1     pooka 
     50       1.1     pooka #include "rump_private.h"
     51       1.9     pooka #include "rump_net_private.h"
     52       1.1     pooka 
     53       1.1     pooka /*
     54  1.10.2.2  uebayasi  * Do r/w prefault for backend pages when attaching the interface.
     55  1.10.2.2  uebayasi  * This works aroud the most likely kernel/ffs/x86pmap bug described
     56  1.10.2.2  uebayasi  * in http://mail-index.netbsd.org/tech-kern/2010/08/17/msg008749.html
     57  1.10.2.2  uebayasi  *
     58  1.10.2.2  uebayasi  * NOTE: read prefaulting is not enough (that's done always)!
     59  1.10.2.2  uebayasi  */
     60  1.10.2.2  uebayasi 
     61  1.10.2.2  uebayasi #define PREFAULT_RW
     62  1.10.2.2  uebayasi 
     63  1.10.2.2  uebayasi /*
     64       1.1     pooka  * A virtual ethernet interface which uses shared memory from a
     65       1.1     pooka  * memory mapped file as the bus.
     66       1.1     pooka  */
     67       1.1     pooka 
     68       1.1     pooka static int	shmif_init(struct ifnet *);
     69       1.1     pooka static int	shmif_ioctl(struct ifnet *, u_long, void *);
     70       1.1     pooka static void	shmif_start(struct ifnet *);
     71       1.1     pooka static void	shmif_stop(struct ifnet *, int);
     72       1.1     pooka 
     73  1.10.2.1  uebayasi #include "shmifvar.h"
     74  1.10.2.1  uebayasi 
     75       1.1     pooka struct shmif_sc {
     76       1.1     pooka 	struct ethercom sc_ec;
     77       1.1     pooka 	uint8_t sc_myaddr[6];
     78  1.10.2.1  uebayasi 	struct shmif_mem *sc_busmem;
     79       1.1     pooka 	int sc_memfd;
     80       1.1     pooka 	int sc_kq;
     81       1.1     pooka 
     82  1.10.2.2  uebayasi 	uint64_t sc_devgen;
     83       1.1     pooka 	uint32_t sc_nextpacket;
     84       1.1     pooka };
     85       1.1     pooka 
     86  1.10.2.1  uebayasi static const uint32_t busversion = SHMIF_VERSION;
     87       1.1     pooka 
     88       1.1     pooka static void shmif_rcv(void *);
     89       1.1     pooka 
     90       1.1     pooka static uint32_t numif;
     91       1.1     pooka 
     92  1.10.2.1  uebayasi #define LOCK_UNLOCKED	0
     93  1.10.2.1  uebayasi #define LOCK_LOCKED	1
     94  1.10.2.1  uebayasi #define LOCK_COOLDOWN	1001
     95  1.10.2.1  uebayasi 
     96       1.2     pooka /*
     97       1.2     pooka  * This locking needs work and will misbehave severely if:
     98       1.2     pooka  * 1) the backing memory has to be paged in
     99       1.2     pooka  * 2) some lockholder exits while holding the lock
    100       1.2     pooka  */
    101       1.1     pooka static void
    102  1.10.2.1  uebayasi shmif_lockbus(struct shmif_mem *busmem)
    103       1.1     pooka {
    104  1.10.2.1  uebayasi 	int i = 0;
    105       1.1     pooka 
    106  1.10.2.1  uebayasi 	while (__predict_false(atomic_cas_32(&busmem->shm_lock,
    107  1.10.2.1  uebayasi 	    LOCK_UNLOCKED, LOCK_LOCKED) == LOCK_LOCKED)) {
    108  1.10.2.1  uebayasi 		if (__predict_false(++i > LOCK_COOLDOWN)) {
    109  1.10.2.1  uebayasi 			uint64_t sec, nsec;
    110  1.10.2.1  uebayasi 			int error;
    111  1.10.2.1  uebayasi 
    112  1.10.2.1  uebayasi 			sec = 0;
    113  1.10.2.1  uebayasi 			nsec = 1000*1000; /* 1ms */
    114  1.10.2.1  uebayasi 			rumpuser_nanosleep(&sec, &nsec, &error);
    115  1.10.2.1  uebayasi 			i = 0;
    116  1.10.2.1  uebayasi 		}
    117  1.10.2.1  uebayasi 		continue;
    118  1.10.2.1  uebayasi 	}
    119  1.10.2.1  uebayasi 	membar_enter();
    120       1.1     pooka }
    121       1.1     pooka 
    122       1.1     pooka static void
    123  1.10.2.1  uebayasi shmif_unlockbus(struct shmif_mem *busmem)
    124       1.1     pooka {
    125  1.10.2.1  uebayasi 	unsigned int old;
    126       1.1     pooka 
    127  1.10.2.1  uebayasi 	membar_exit();
    128  1.10.2.1  uebayasi 	old = atomic_swap_32(&busmem->shm_lock, LOCK_UNLOCKED);
    129  1.10.2.1  uebayasi 	KASSERT(old == LOCK_LOCKED);
    130       1.1     pooka }
    131       1.1     pooka 
    132       1.1     pooka int
    133       1.1     pooka rump_shmif_create(const char *path, int *ifnum)
    134       1.1     pooka {
    135       1.1     pooka 	struct shmif_sc *sc;
    136       1.1     pooka 	struct ifnet *ifp;
    137       1.1     pooka 	uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0xa0, 0x00, 0x00, 0x00, 0x00 };
    138       1.1     pooka 	uint32_t randnum;
    139       1.1     pooka 	unsigned mynum;
    140  1.10.2.2  uebayasi 	volatile uint8_t v;
    141  1.10.2.2  uebayasi 	volatile uint8_t *p;
    142       1.1     pooka 	int error;
    143       1.1     pooka 
    144       1.1     pooka 	randnum = arc4random();
    145  1.10.2.1  uebayasi 	memcpy(&enaddr[2], &randnum, sizeof(randnum));
    146       1.1     pooka 	mynum = atomic_inc_uint_nv(&numif)-1;
    147       1.1     pooka 
    148       1.1     pooka 	sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
    149       1.1     pooka 	ifp = &sc->sc_ec.ec_if;
    150       1.1     pooka 	memcpy(sc->sc_myaddr, enaddr, sizeof(enaddr));
    151       1.1     pooka 
    152       1.1     pooka 	sc->sc_memfd = rumpuser_open(path, O_RDWR | O_CREAT, &error);
    153       1.1     pooka 	if (sc->sc_memfd == -1)
    154       1.1     pooka 		goto fail;
    155       1.1     pooka 	sc->sc_busmem = rumpuser_filemmap(sc->sc_memfd, 0, BUSMEM_SIZE,
    156       1.5     pooka 	    RUMPUSER_FILEMMAP_TRUNCATE | RUMPUSER_FILEMMAP_SHARED
    157       1.5     pooka 	    | RUMPUSER_FILEMMAP_READ | RUMPUSER_FILEMMAP_WRITE, &error);
    158       1.1     pooka 	if (error)
    159       1.1     pooka 		goto fail;
    160       1.1     pooka 
    161  1.10.2.1  uebayasi 	if (sc->sc_busmem->shm_magic && sc->sc_busmem->shm_magic != SHMIF_MAGIC)
    162  1.10.2.1  uebayasi 		panic("bus is not magical");
    163  1.10.2.1  uebayasi 
    164  1.10.2.2  uebayasi 
    165  1.10.2.2  uebayasi 	/* Prefault in pages to minimize runtime penalty with buslock */
    166  1.10.2.2  uebayasi 	for (p = (uint8_t *)sc->sc_busmem;
    167  1.10.2.2  uebayasi 	    p < (uint8_t *)sc->sc_busmem + BUSMEM_SIZE;
    168  1.10.2.2  uebayasi 	    p += PAGE_SIZE)
    169  1.10.2.2  uebayasi 		v = *p;
    170  1.10.2.2  uebayasi 
    171  1.10.2.1  uebayasi 	shmif_lockbus(sc->sc_busmem);
    172  1.10.2.1  uebayasi 	/* we're first?  initialize bus */
    173  1.10.2.1  uebayasi 	if (sc->sc_busmem->shm_magic == 0) {
    174  1.10.2.1  uebayasi 		sc->sc_busmem->shm_magic = SHMIF_MAGIC;
    175  1.10.2.1  uebayasi 		sc->sc_busmem->shm_first = BUSMEM_DATASIZE;
    176  1.10.2.1  uebayasi 	}
    177  1.10.2.1  uebayasi 
    178  1.10.2.1  uebayasi 	sc->sc_nextpacket = sc->sc_busmem->shm_last;
    179  1.10.2.2  uebayasi 	sc->sc_devgen = sc->sc_busmem->shm_gen;
    180  1.10.2.2  uebayasi 
    181  1.10.2.2  uebayasi #ifdef PREFAULT_RW
    182  1.10.2.2  uebayasi 	for (p = (uint8_t *)sc->sc_busmem;
    183  1.10.2.2  uebayasi 	    p < (uint8_t *)sc->sc_busmem + BUSMEM_SIZE;
    184  1.10.2.2  uebayasi 	    p += PAGE_SIZE) {
    185  1.10.2.2  uebayasi 		v = *p;
    186  1.10.2.2  uebayasi 		*p = v;
    187  1.10.2.2  uebayasi 	}
    188  1.10.2.2  uebayasi #endif
    189  1.10.2.1  uebayasi 	shmif_unlockbus(sc->sc_busmem);
    190       1.1     pooka 
    191       1.1     pooka 	sc->sc_kq = rumpuser_writewatchfile_setup(-1, sc->sc_memfd, 0, &error);
    192       1.1     pooka 	if (sc->sc_kq == -1)
    193       1.1     pooka 		goto fail;
    194       1.1     pooka 
    195       1.1     pooka 	sprintf(ifp->if_xname, "shmif%d", mynum);
    196       1.1     pooka 	ifp->if_softc = sc;
    197  1.10.2.1  uebayasi 	ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST;
    198       1.1     pooka 	ifp->if_init = shmif_init;
    199       1.1     pooka 	ifp->if_ioctl = shmif_ioctl;
    200       1.1     pooka 	ifp->if_start = shmif_start;
    201       1.1     pooka 	ifp->if_stop = shmif_stop;
    202  1.10.2.2  uebayasi 	ifp->if_mtu = ETHERMTU;
    203       1.1     pooka 
    204       1.1     pooka 	if_attach(ifp);
    205       1.1     pooka 	ether_ifattach(ifp, enaddr);
    206       1.1     pooka 
    207  1.10.2.1  uebayasi 	aprint_verbose("shmif%d: bus %s\n", mynum, path);
    208  1.10.2.1  uebayasi 	aprint_verbose("shmif%d: Ethernet address %s\n",
    209  1.10.2.1  uebayasi 	    mynum, ether_sprintf(enaddr));
    210  1.10.2.1  uebayasi 
    211       1.2     pooka 	if (ifnum)
    212       1.2     pooka 		*ifnum = mynum;
    213       1.1     pooka 	return 0;
    214       1.1     pooka 
    215       1.1     pooka  fail:
    216       1.1     pooka 	panic("rump_shmemif_create: fixme");
    217       1.1     pooka }
    218       1.1     pooka 
    219       1.1     pooka static int
    220       1.1     pooka shmif_init(struct ifnet *ifp)
    221       1.1     pooka {
    222       1.4     pooka 	int error = 0;
    223       1.4     pooka 
    224       1.4     pooka 	if (rump_threads) {
    225       1.4     pooka 		error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    226       1.4     pooka 		    shmif_rcv, ifp, NULL, "shmif");
    227       1.4     pooka 	} else {
    228       1.4     pooka 		printf("WARNING: threads not enabled, shmif NOT working\n");
    229       1.4     pooka 	}
    230       1.1     pooka 
    231       1.1     pooka 	ifp->if_flags |= IFF_RUNNING;
    232       1.4     pooka 	return error;
    233       1.1     pooka }
    234       1.1     pooka 
    235       1.1     pooka static int
    236       1.1     pooka shmif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    237       1.1     pooka {
    238       1.1     pooka 	int s, rv;
    239       1.1     pooka 
    240       1.1     pooka 	s = splnet();
    241       1.1     pooka 	rv = ether_ioctl(ifp, cmd, data);
    242       1.7     pooka 	if (rv == ENETRESET)
    243       1.7     pooka 		rv = 0;
    244       1.1     pooka 	splx(s);
    245       1.1     pooka 
    246       1.1     pooka 	return rv;
    247       1.1     pooka }
    248       1.1     pooka 
    249       1.1     pooka /* send everything in-context */
    250       1.1     pooka static void
    251       1.1     pooka shmif_start(struct ifnet *ifp)
    252       1.1     pooka {
    253       1.1     pooka 	struct shmif_sc *sc = ifp->if_softc;
    254  1.10.2.2  uebayasi 	struct shmif_mem *busmem = sc->sc_busmem;
    255       1.1     pooka 	struct mbuf *m, *m0;
    256  1.10.2.2  uebayasi 	uint32_t dataoff;
    257  1.10.2.2  uebayasi 	uint32_t pktsize, pktwrote;
    258       1.1     pooka 	bool wrote = false;
    259  1.10.2.1  uebayasi 	bool wrap;
    260       1.1     pooka 	int error;
    261       1.1     pooka 
    262  1.10.2.2  uebayasi 	ifp->if_flags |= IFF_OACTIVE;
    263  1.10.2.2  uebayasi 
    264       1.1     pooka 	for (;;) {
    265  1.10.2.1  uebayasi 		struct shmif_pkthdr sp;
    266  1.10.2.1  uebayasi 		struct timeval tv;
    267  1.10.2.1  uebayasi 
    268       1.1     pooka 		IF_DEQUEUE(&ifp->if_snd, m0);
    269       1.1     pooka 		if (m0 == NULL) {
    270       1.1     pooka 			break;
    271       1.1     pooka 		}
    272       1.1     pooka 
    273  1.10.2.1  uebayasi 		pktsize = 0;
    274  1.10.2.1  uebayasi 		for (m = m0; m != NULL; m = m->m_next) {
    275  1.10.2.1  uebayasi 			pktsize += m->m_len;
    276  1.10.2.1  uebayasi 		}
    277  1.10.2.2  uebayasi 		KASSERT(pktsize <= ETHERMTU + ETHER_HDR_LEN);
    278       1.1     pooka 
    279  1.10.2.1  uebayasi 		getmicrouptime(&tv);
    280  1.10.2.1  uebayasi 		sp.sp_len = pktsize;
    281  1.10.2.1  uebayasi 		sp.sp_sec = tv.tv_sec;
    282  1.10.2.1  uebayasi 		sp.sp_usec = tv.tv_usec;
    283  1.10.2.1  uebayasi 
    284  1.10.2.2  uebayasi 		shmif_lockbus(busmem);
    285  1.10.2.2  uebayasi 		KASSERT(busmem->shm_magic == SHMIF_MAGIC);
    286  1.10.2.2  uebayasi 		busmem->shm_last = shmif_nextpktoff(busmem, busmem->shm_last);
    287  1.10.2.1  uebayasi 
    288  1.10.2.1  uebayasi 		wrap = false;
    289  1.10.2.2  uebayasi 		dataoff = shmif_buswrite(busmem,
    290  1.10.2.2  uebayasi 		    busmem->shm_last, &sp, sizeof(sp), &wrap);
    291  1.10.2.2  uebayasi 		pktwrote = 0;
    292       1.1     pooka 		for (m = m0; m != NULL; m = m->m_next) {
    293  1.10.2.2  uebayasi 			pktwrote += m->m_len;
    294  1.10.2.2  uebayasi 			dataoff = shmif_buswrite(busmem, dataoff,
    295  1.10.2.1  uebayasi 			    mtod(m, void *), m->m_len, &wrap);
    296       1.1     pooka 		}
    297  1.10.2.2  uebayasi 		KASSERT(pktwrote == pktsize);
    298  1.10.2.2  uebayasi 		if (wrap) {
    299  1.10.2.2  uebayasi 			busmem->shm_gen++;
    300  1.10.2.2  uebayasi 			DPRINTF(("bus generation now %d\n", busmem->shm_gen));
    301  1.10.2.2  uebayasi 		}
    302  1.10.2.2  uebayasi 		shmif_unlockbus(busmem);
    303       1.1     pooka 
    304       1.1     pooka 		m_freem(m0);
    305       1.1     pooka 		wrote = true;
    306       1.1     pooka 
    307       1.1     pooka 		DPRINTF(("shmif_start: send %d bytes at off %d\n",
    308  1.10.2.2  uebayasi 		    pktsize, busmem->shm_last));
    309       1.1     pooka 	}
    310  1.10.2.2  uebayasi 
    311  1.10.2.2  uebayasi 	ifp->if_flags &= ~IFF_OACTIVE;
    312  1.10.2.2  uebayasi 
    313       1.1     pooka 	/* wakeup */
    314       1.1     pooka 	if (wrote)
    315  1.10.2.1  uebayasi 		rumpuser_pwrite(sc->sc_memfd,
    316  1.10.2.1  uebayasi 		    &busversion, sizeof(busversion), IFMEM_WAKEUP, &error);
    317       1.1     pooka }
    318       1.1     pooka 
    319       1.1     pooka static void
    320       1.1     pooka shmif_stop(struct ifnet *ifp, int disable)
    321       1.1     pooka {
    322       1.1     pooka 
    323       1.1     pooka 	panic("%s: unimpl", __func__);
    324       1.1     pooka }
    325       1.1     pooka 
    326  1.10.2.2  uebayasi 
    327  1.10.2.2  uebayasi /*
    328  1.10.2.2  uebayasi  * Check if we have been sleeping too long.  Basically,
    329  1.10.2.2  uebayasi  * our in-sc nextpkt must by first <= nextpkt <= last"+1".
    330  1.10.2.2  uebayasi  * We use the fact that first is guaranteed to never overlap
    331  1.10.2.2  uebayasi  * with the last frame in the ring.
    332  1.10.2.2  uebayasi  */
    333  1.10.2.2  uebayasi static __inline bool
    334  1.10.2.2  uebayasi stillvalid_p(struct shmif_sc *sc)
    335  1.10.2.2  uebayasi {
    336  1.10.2.2  uebayasi 	struct shmif_mem *busmem = sc->sc_busmem;
    337  1.10.2.2  uebayasi 	unsigned gendiff = busmem->shm_gen - sc->sc_devgen;
    338  1.10.2.2  uebayasi 	uint32_t lastoff, devoff;
    339  1.10.2.2  uebayasi 
    340  1.10.2.2  uebayasi 	KASSERT(busmem->shm_first != busmem->shm_last);
    341  1.10.2.2  uebayasi 
    342  1.10.2.2  uebayasi 	/* normalize onto a 2x busmem chunk */
    343  1.10.2.2  uebayasi 	devoff = sc->sc_nextpacket;
    344  1.10.2.2  uebayasi 	lastoff = shmif_nextpktoff(busmem, busmem->shm_last);
    345  1.10.2.2  uebayasi 
    346  1.10.2.2  uebayasi 	/* trivial case */
    347  1.10.2.2  uebayasi 	if (gendiff > 1)
    348  1.10.2.2  uebayasi 		return false;
    349  1.10.2.2  uebayasi 	KASSERT(gendiff <= 1);
    350  1.10.2.2  uebayasi 
    351  1.10.2.2  uebayasi 	/* Normalize onto 2x busmem chunk */
    352  1.10.2.2  uebayasi 	if (busmem->shm_first >= lastoff) {
    353  1.10.2.2  uebayasi 		lastoff += BUSMEM_DATASIZE;
    354  1.10.2.2  uebayasi 		if (gendiff == 0)
    355  1.10.2.2  uebayasi 			devoff += BUSMEM_DATASIZE;
    356  1.10.2.2  uebayasi 	} else {
    357  1.10.2.2  uebayasi 		if (gendiff)
    358  1.10.2.2  uebayasi 			return false;
    359  1.10.2.2  uebayasi 	}
    360  1.10.2.2  uebayasi 
    361  1.10.2.2  uebayasi 	return devoff >= busmem->shm_first && devoff <= lastoff;
    362  1.10.2.2  uebayasi }
    363  1.10.2.2  uebayasi 
    364       1.1     pooka static void
    365       1.1     pooka shmif_rcv(void *arg)
    366       1.1     pooka {
    367       1.1     pooka 	struct ifnet *ifp = arg;
    368       1.1     pooka 	struct shmif_sc *sc = ifp->if_softc;
    369  1.10.2.2  uebayasi 	struct shmif_mem *busmem = sc->sc_busmem;
    370       1.1     pooka 	struct mbuf *m = NULL;
    371       1.1     pooka 	struct ether_header *eth;
    372  1.10.2.2  uebayasi 	uint32_t nextpkt;
    373  1.10.2.1  uebayasi 	bool wrap;
    374       1.1     pooka 	int error;
    375       1.1     pooka 
    376       1.1     pooka 	for (;;) {
    377  1.10.2.1  uebayasi 		struct shmif_pkthdr sp;
    378  1.10.2.1  uebayasi 
    379       1.1     pooka 		if (m == NULL) {
    380       1.1     pooka 			m = m_gethdr(M_WAIT, MT_DATA);
    381       1.1     pooka 			MCLGET(m, M_WAIT);
    382       1.1     pooka 		}
    383       1.1     pooka 
    384  1.10.2.2  uebayasi 		DPRINTF(("waiting %d/%d\n", sc->sc_nextpacket, sc->sc_devgen));
    385       1.1     pooka 		KASSERT(m->m_flags & M_EXT);
    386  1.10.2.2  uebayasi 
    387  1.10.2.2  uebayasi 		shmif_lockbus(busmem);
    388  1.10.2.2  uebayasi 		KASSERT(busmem->shm_magic == SHMIF_MAGIC);
    389  1.10.2.2  uebayasi 		KASSERT(busmem->shm_gen >= sc->sc_devgen);
    390       1.1     pooka 
    391       1.1     pooka 		/* need more data? */
    392  1.10.2.2  uebayasi 		if (sc->sc_devgen == busmem->shm_gen &&
    393  1.10.2.2  uebayasi 		    shmif_nextpktoff(busmem, busmem->shm_last)
    394  1.10.2.2  uebayasi 		     == sc->sc_nextpacket) {
    395  1.10.2.2  uebayasi 			shmif_unlockbus(busmem);
    396       1.1     pooka 			error = 0;
    397       1.1     pooka 			rumpuser_writewatchfile_wait(sc->sc_kq, NULL, &error);
    398       1.1     pooka 			if (__predict_false(error))
    399       1.1     pooka 				printf("shmif_rcv: wait failed %d\n", error);
    400       1.1     pooka 			continue;
    401       1.1     pooka 		}
    402       1.1     pooka 
    403  1.10.2.2  uebayasi 		if (stillvalid_p(sc)) {
    404  1.10.2.2  uebayasi 			nextpkt = sc->sc_nextpacket;
    405  1.10.2.2  uebayasi 		} else {
    406  1.10.2.2  uebayasi 			KASSERT(busmem->shm_gen > 0);
    407  1.10.2.2  uebayasi 			nextpkt = busmem->shm_first;
    408  1.10.2.2  uebayasi 			if (busmem->shm_first > busmem->shm_last)
    409  1.10.2.2  uebayasi 				sc->sc_devgen = busmem->shm_gen - 1;
    410  1.10.2.2  uebayasi 			else
    411  1.10.2.2  uebayasi 				sc->sc_devgen = busmem->shm_gen;
    412  1.10.2.2  uebayasi 			DPRINTF(("dev %p overrun, new data: %d/%d\n",
    413  1.10.2.2  uebayasi 			    sc, nextpkt, sc->sc_devgen));
    414  1.10.2.2  uebayasi 		}
    415  1.10.2.2  uebayasi 
    416  1.10.2.2  uebayasi 		/*
    417  1.10.2.2  uebayasi 		 * If our read pointer is ahead the bus last write, our
    418  1.10.2.2  uebayasi 		 * generation must be one behind.
    419  1.10.2.2  uebayasi 		 */
    420  1.10.2.2  uebayasi 		KASSERT(!(nextpkt > busmem->shm_last
    421  1.10.2.2  uebayasi 		    && sc->sc_devgen == busmem->shm_gen));
    422  1.10.2.2  uebayasi 
    423  1.10.2.1  uebayasi 		wrap = false;
    424  1.10.2.2  uebayasi 		nextpkt = shmif_busread(busmem, &sp,
    425  1.10.2.2  uebayasi 		    nextpkt, sizeof(sp), &wrap);
    426  1.10.2.2  uebayasi 		KASSERT(sp.sp_len <= ETHERMTU + ETHER_HDR_LEN);
    427  1.10.2.2  uebayasi 		nextpkt = shmif_busread(busmem, mtod(m, void *),
    428  1.10.2.2  uebayasi 		    nextpkt, sp.sp_len, &wrap);
    429       1.1     pooka 
    430       1.1     pooka 		DPRINTF(("shmif_rcv: read packet of length %d at %d\n",
    431  1.10.2.1  uebayasi 		    sp.sp_len, nextpkt));
    432       1.1     pooka 
    433  1.10.2.2  uebayasi 		sc->sc_nextpacket = nextpkt;
    434  1.10.2.1  uebayasi 		shmif_unlockbus(sc->sc_busmem);
    435       1.1     pooka 
    436  1.10.2.2  uebayasi 		if (wrap) {
    437  1.10.2.2  uebayasi 			sc->sc_devgen++;
    438  1.10.2.2  uebayasi 			DPRINTF(("dev %p generation now %d\n",
    439  1.10.2.2  uebayasi 			    sc, sc->sc_devgen));
    440  1.10.2.2  uebayasi 		}
    441  1.10.2.2  uebayasi 
    442  1.10.2.1  uebayasi 		m->m_len = m->m_pkthdr.len = sp.sp_len;
    443       1.1     pooka 		m->m_pkthdr.rcvif = ifp;
    444       1.1     pooka 
    445      1.10     pooka 		/* if it's from us, don't pass up and reuse storage space */
    446       1.1     pooka 		eth = mtod(m, struct ether_header *);
    447       1.1     pooka 		if (memcmp(eth->ether_shost, sc->sc_myaddr, 6) != 0) {
    448  1.10.2.1  uebayasi 			KERNEL_LOCK(1, NULL);
    449       1.1     pooka 			ifp->if_input(ifp, m);
    450  1.10.2.1  uebayasi 			KERNEL_UNLOCK_ONE(NULL);
    451       1.1     pooka 			m = NULL;
    452       1.1     pooka 		}
    453       1.1     pooka 	}
    454       1.1     pooka 
    455       1.1     pooka 	panic("shmif_worker is a lazy boy %d\n", error);
    456       1.1     pooka }
    457