Home | History | Annotate | Line # | Download | only in libshmif
if_shmem.c revision 1.31
      1  1.31  pooka /*	$NetBSD: if_shmem.c,v 1.31 2010/11/16 20:08:24 pooka 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.31  pooka __KERNEL_RCSID(0, "$NetBSD: if_shmem.c,v 1.31 2010/11/16 20:08:24 pooka Exp $");
     32   1.1  pooka 
     33   1.1  pooka #include <sys/param.h>
     34  1.13  pooka #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.31  pooka #include <sys/vmem.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.29  pooka static int shmif_clone(struct if_clone *, int);
     54  1.29  pooka static int shmif_unclone(struct ifnet *);
     55  1.29  pooka 
     56  1.29  pooka struct if_clone shmif_cloner =
     57  1.29  pooka     IF_CLONE_INITIALIZER("shmif", shmif_clone, shmif_unclone);
     58  1.29  pooka 
     59   1.1  pooka /*
     60  1.28  pooka  * Do r/w prefault for backend pages when attaching the interface.
     61  1.29  pooka  * At least logically thinking improves performance (although no
     62  1.29  pooka  * mlocking is done, so they might go away).
     63  1.28  pooka  */
     64  1.28  pooka #define PREFAULT_RW
     65  1.28  pooka 
     66  1.28  pooka /*
     67   1.1  pooka  * A virtual ethernet interface which uses shared memory from a
     68   1.1  pooka  * memory mapped file as the bus.
     69   1.1  pooka  */
     70   1.1  pooka 
     71   1.1  pooka static int	shmif_init(struct ifnet *);
     72   1.1  pooka static int	shmif_ioctl(struct ifnet *, u_long, void *);
     73   1.1  pooka static void	shmif_start(struct ifnet *);
     74   1.1  pooka static void	shmif_stop(struct ifnet *, int);
     75   1.1  pooka 
     76  1.16  pooka #include "shmifvar.h"
     77  1.16  pooka 
     78   1.1  pooka struct shmif_sc {
     79   1.1  pooka 	struct ethercom sc_ec;
     80   1.1  pooka 	uint8_t sc_myaddr[6];
     81  1.16  pooka 	struct shmif_mem *sc_busmem;
     82   1.1  pooka 	int sc_memfd;
     83   1.1  pooka 	int sc_kq;
     84   1.1  pooka 
     85  1.29  pooka 	char *sc_backfile;
     86  1.29  pooka 	size_t sc_backfilelen;
     87  1.29  pooka 
     88  1.26  pooka 	uint64_t sc_devgen;
     89   1.1  pooka 	uint32_t sc_nextpacket;
     90   1.1  pooka };
     91   1.1  pooka 
     92  1.17  pooka static const uint32_t busversion = SHMIF_VERSION;
     93   1.1  pooka 
     94   1.1  pooka static void shmif_rcv(void *);
     95   1.1  pooka 
     96  1.23  pooka #define LOCK_UNLOCKED	0
     97  1.23  pooka #define LOCK_LOCKED	1
     98  1.23  pooka #define LOCK_COOLDOWN	1001
     99  1.23  pooka 
    100  1.31  pooka vmem_t *shmif_units;
    101  1.31  pooka 
    102  1.23  pooka /*
    103  1.23  pooka  * This locking needs work and will misbehave severely if:
    104  1.23  pooka  * 1) the backing memory has to be paged in
    105  1.23  pooka  * 2) some lockholder exits while holding the lock
    106  1.23  pooka  */
    107  1.23  pooka static void
    108  1.23  pooka shmif_lockbus(struct shmif_mem *busmem)
    109  1.23  pooka {
    110  1.23  pooka 	int i = 0;
    111  1.23  pooka 
    112  1.23  pooka 	while (__predict_false(atomic_cas_32(&busmem->shm_lock,
    113  1.23  pooka 	    LOCK_UNLOCKED, LOCK_LOCKED) == LOCK_LOCKED)) {
    114  1.23  pooka 		if (__predict_false(++i > LOCK_COOLDOWN)) {
    115  1.23  pooka 			uint64_t sec, nsec;
    116  1.23  pooka 			int error;
    117  1.23  pooka 
    118  1.23  pooka 			sec = 0;
    119  1.23  pooka 			nsec = 1000*1000; /* 1ms */
    120  1.23  pooka 			rumpuser_nanosleep(&sec, &nsec, &error);
    121  1.23  pooka 			i = 0;
    122  1.23  pooka 		}
    123  1.23  pooka 		continue;
    124  1.23  pooka 	}
    125  1.23  pooka 	membar_enter();
    126  1.23  pooka }
    127  1.23  pooka 
    128  1.23  pooka static void
    129  1.23  pooka shmif_unlockbus(struct shmif_mem *busmem)
    130  1.23  pooka {
    131  1.23  pooka 	unsigned int old;
    132  1.23  pooka 
    133  1.23  pooka 	membar_exit();
    134  1.23  pooka 	old = atomic_swap_32(&busmem->shm_lock, LOCK_UNLOCKED);
    135  1.23  pooka 	KASSERT(old == LOCK_LOCKED);
    136  1.23  pooka }
    137  1.23  pooka 
    138  1.29  pooka static int
    139  1.29  pooka allocif(int unit, struct shmif_sc **scp)
    140   1.1  pooka {
    141  1.29  pooka 	uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0xa0, 0x00, 0x00, 0x00, 0x00 };
    142   1.1  pooka 	struct shmif_sc *sc;
    143   1.1  pooka 	struct ifnet *ifp;
    144   1.1  pooka 	uint32_t randnum;
    145  1.29  pooka 	unsigned mynum = unit;
    146   1.1  pooka 
    147   1.1  pooka 	randnum = arc4random();
    148  1.15  pooka 	memcpy(&enaddr[2], &randnum, sizeof(randnum));
    149   1.1  pooka 
    150   1.1  pooka 	sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
    151  1.29  pooka 	sc->sc_memfd = -1;
    152  1.29  pooka 
    153   1.1  pooka 	ifp = &sc->sc_ec.ec_if;
    154   1.1  pooka 	memcpy(sc->sc_myaddr, enaddr, sizeof(enaddr));
    155   1.1  pooka 
    156  1.29  pooka 	sprintf(ifp->if_xname, "shmif%d", mynum);
    157  1.29  pooka 	ifp->if_softc = sc;
    158  1.29  pooka 	ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST;
    159  1.29  pooka 	ifp->if_init = shmif_init;
    160  1.29  pooka 	ifp->if_ioctl = shmif_ioctl;
    161  1.29  pooka 	ifp->if_start = shmif_start;
    162  1.29  pooka 	ifp->if_stop = shmif_stop;
    163  1.29  pooka 	ifp->if_mtu = ETHERMTU;
    164  1.29  pooka 
    165  1.29  pooka 	if_attach(ifp);
    166  1.29  pooka 	ether_ifattach(ifp, enaddr);
    167  1.29  pooka 
    168  1.29  pooka 	aprint_verbose("shmif%d: Ethernet address %s\n",
    169  1.29  pooka 	    mynum, ether_sprintf(enaddr));
    170  1.29  pooka 
    171  1.29  pooka 	if (scp)
    172  1.29  pooka 		*scp = sc;
    173  1.29  pooka 
    174  1.29  pooka 	return 0;
    175  1.29  pooka }
    176  1.29  pooka 
    177  1.29  pooka static int
    178  1.29  pooka initbackend(struct shmif_sc *sc, int memfd)
    179  1.29  pooka {
    180  1.29  pooka 	volatile uint8_t v;
    181  1.29  pooka 	volatile uint8_t *p;
    182  1.29  pooka 	int error;
    183  1.29  pooka 
    184  1.29  pooka 	sc->sc_busmem = rumpuser_filemmap(memfd, 0, BUSMEM_SIZE,
    185   1.5  pooka 	    RUMPUSER_FILEMMAP_TRUNCATE | RUMPUSER_FILEMMAP_SHARED
    186   1.5  pooka 	    | RUMPUSER_FILEMMAP_READ | RUMPUSER_FILEMMAP_WRITE, &error);
    187   1.1  pooka 	if (error)
    188  1.29  pooka 		return error;
    189  1.17  pooka 
    190  1.29  pooka 	if (sc->sc_busmem->shm_magic
    191  1.29  pooka 	    && sc->sc_busmem->shm_magic != SHMIF_MAGIC) {
    192  1.29  pooka 		printf("bus is not magical");
    193  1.29  pooka 		rumpuser_unmap(sc->sc_busmem, BUSMEM_SIZE);
    194  1.29  pooka 		return ENOEXEC;
    195  1.29  pooka 	}
    196  1.28  pooka 
    197  1.28  pooka 	/* Prefault in pages to minimize runtime penalty with buslock */
    198  1.28  pooka 	for (p = (uint8_t *)sc->sc_busmem;
    199  1.28  pooka 	    p < (uint8_t *)sc->sc_busmem + BUSMEM_SIZE;
    200  1.28  pooka 	    p += PAGE_SIZE)
    201  1.28  pooka 		v = *p;
    202  1.28  pooka 
    203  1.19  pooka 	shmif_lockbus(sc->sc_busmem);
    204  1.19  pooka 	/* we're first?  initialize bus */
    205  1.19  pooka 	if (sc->sc_busmem->shm_magic == 0) {
    206  1.19  pooka 		sc->sc_busmem->shm_magic = SHMIF_MAGIC;
    207  1.19  pooka 		sc->sc_busmem->shm_first = BUSMEM_DATASIZE;
    208  1.19  pooka 	}
    209  1.19  pooka 
    210  1.16  pooka 	sc->sc_nextpacket = sc->sc_busmem->shm_last;
    211  1.26  pooka 	sc->sc_devgen = sc->sc_busmem->shm_gen;
    212  1.28  pooka 
    213  1.28  pooka #ifdef PREFAULT_RW
    214  1.28  pooka 	for (p = (uint8_t *)sc->sc_busmem;
    215  1.28  pooka 	    p < (uint8_t *)sc->sc_busmem + BUSMEM_SIZE;
    216  1.28  pooka 	    p += PAGE_SIZE) {
    217  1.28  pooka 		v = *p;
    218  1.28  pooka 		*p = v;
    219  1.28  pooka 	}
    220  1.28  pooka #endif
    221  1.19  pooka 	shmif_unlockbus(sc->sc_busmem);
    222   1.1  pooka 
    223  1.29  pooka 	sc->sc_kq = rumpuser_writewatchfile_setup(-1, memfd, 0, &error);
    224   1.1  pooka 	if (sc->sc_kq == -1)
    225  1.29  pooka 		return error;
    226   1.1  pooka 
    227  1.29  pooka 	sc->sc_memfd = memfd;
    228  1.29  pooka 	return 0;
    229  1.29  pooka }
    230  1.29  pooka 
    231  1.29  pooka static void
    232  1.29  pooka finibackend(struct shmif_sc *sc)
    233  1.29  pooka {
    234  1.29  pooka 	int dummy;
    235  1.29  pooka 
    236  1.29  pooka 	kmem_free(sc->sc_backfile, sc->sc_backfilelen);
    237  1.29  pooka 	sc->sc_backfile = NULL;
    238  1.29  pooka 	sc->sc_backfilelen = 0;
    239  1.29  pooka 
    240  1.29  pooka 	rumpuser_unmap(sc->sc_busmem, BUSMEM_SIZE);
    241  1.29  pooka 	rumpuser_close(sc->sc_memfd, &dummy);
    242  1.29  pooka 	rumpuser_close(sc->sc_kq, &dummy);
    243  1.29  pooka }
    244  1.29  pooka 
    245  1.29  pooka int
    246  1.29  pooka rump_shmif_create(const char *path, int *ifnum)
    247  1.29  pooka {
    248  1.29  pooka 	struct shmif_sc *sc;
    249  1.29  pooka 	int mynum, error, memfd, dummy;
    250  1.29  pooka 
    251  1.29  pooka 	memfd = rumpuser_open(path, O_RDWR | O_CREAT, &error);
    252  1.29  pooka 	if (memfd == -1)
    253  1.29  pooka 		return error;
    254   1.1  pooka 
    255  1.31  pooka 	mynum = vmem_xalloc(shmif_units, 1, 0, 0, 0, 0, 0,
    256  1.31  pooka 	    VM_INSTANTFIT | VM_SLEEP) - 1;
    257  1.31  pooka 
    258  1.29  pooka 	if ((error = allocif(mynum, &sc)) != 0) {
    259  1.31  pooka 		rumpuser_close(memfd, NULL);
    260  1.29  pooka 		return error;
    261  1.29  pooka 	}
    262  1.29  pooka 	error = initbackend(sc, memfd);
    263  1.29  pooka 	if (error) {
    264  1.29  pooka 		rumpuser_close(memfd, &dummy);
    265  1.29  pooka 		/* XXX: free sc */
    266  1.29  pooka 		return error;
    267  1.29  pooka 	}
    268   1.1  pooka 
    269  1.29  pooka 	sc->sc_backfilelen = strlen(path)+1;
    270  1.29  pooka 	sc->sc_backfile = kmem_alloc(sc->sc_backfilelen, KM_SLEEP);
    271  1.29  pooka 	strcpy(sc->sc_backfile, path);
    272  1.12  pooka 
    273   1.2  pooka 	if (ifnum)
    274   1.2  pooka 		*ifnum = mynum;
    275  1.29  pooka 
    276   1.1  pooka 	return 0;
    277  1.29  pooka }
    278   1.1  pooka 
    279  1.29  pooka static int
    280  1.29  pooka shmif_clone(struct if_clone *ifc, int unit)
    281  1.29  pooka {
    282  1.31  pooka 	int unit2;
    283  1.29  pooka 
    284  1.31  pooka 	/*
    285  1.31  pooka 	 * Ok, we know the unit number, but we must still reserve it.
    286  1.31  pooka 	 * Otherwise the wildcard-side of things might get the same one.
    287  1.31  pooka 	 * This is slightly offset-happy due to vmem.  First, we offset
    288  1.31  pooka 	 * the range of unit numbers by +1 since vmem cannot deal with
    289  1.31  pooka 	 * ranges starting from 0.  Second, since vmem_xalloc() allocates
    290  1.31  pooka 	 * from [min,max) (half-*open* interval), we need to add one extra
    291  1.31  pooka 	 * to the one extra we add to maxaddr.  Talk about uuuh.
    292  1.31  pooka 	 */
    293  1.31  pooka 	unit2 = vmem_xalloc(shmif_units, 1, 0, 0, 0, unit+1, unit+3,
    294  1.31  pooka 	    VM_SLEEP | VM_INSTANTFIT);
    295  1.31  pooka 	KASSERT(unit2-1 == unit);
    296  1.29  pooka 
    297  1.29  pooka 	return allocif(unit, NULL);
    298  1.29  pooka }
    299  1.29  pooka 
    300  1.29  pooka static int
    301  1.29  pooka shmif_unclone(struct ifnet *ifp)
    302  1.29  pooka {
    303  1.29  pooka 
    304  1.29  pooka 	return EOPNOTSUPP;
    305   1.1  pooka }
    306   1.1  pooka 
    307   1.1  pooka static int
    308   1.1  pooka shmif_init(struct ifnet *ifp)
    309   1.1  pooka {
    310  1.29  pooka 	struct shmif_sc *sc = ifp->if_softc;
    311   1.4  pooka 	int error = 0;
    312   1.4  pooka 
    313  1.29  pooka 	if (sc->sc_memfd == -1)
    314  1.29  pooka 		return ENXIO;
    315  1.29  pooka 
    316   1.4  pooka 	if (rump_threads) {
    317   1.4  pooka 		error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    318   1.4  pooka 		    shmif_rcv, ifp, NULL, "shmif");
    319   1.4  pooka 	} else {
    320   1.4  pooka 		printf("WARNING: threads not enabled, shmif NOT working\n");
    321   1.4  pooka 	}
    322   1.1  pooka 
    323   1.1  pooka 	ifp->if_flags |= IFF_RUNNING;
    324   1.4  pooka 	return error;
    325   1.1  pooka }
    326   1.1  pooka 
    327   1.1  pooka static int
    328   1.1  pooka shmif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    329   1.1  pooka {
    330  1.29  pooka 	struct shmif_sc *sc = ifp->if_softc;
    331  1.29  pooka 	struct ifdrv *ifd;
    332  1.29  pooka 	char *path;
    333  1.29  pooka 	int s, rv, memfd, dummy;
    334   1.1  pooka 
    335   1.1  pooka 	s = splnet();
    336  1.29  pooka 	switch (cmd) {
    337  1.29  pooka 	case SIOCGLINKSTR:
    338  1.29  pooka 		ifd = data;
    339  1.29  pooka 
    340  1.29  pooka 		if (sc->sc_backfilelen == 0) {
    341  1.29  pooka 			rv = ENOENT;
    342  1.29  pooka 			break;
    343  1.29  pooka 		}
    344  1.29  pooka 
    345  1.29  pooka 		ifd->ifd_len = sc->sc_backfilelen;
    346  1.29  pooka 		if (ifd->ifd_cmd == IFLINKSTR_QUERYLEN) {
    347  1.29  pooka 			rv = 0;
    348  1.29  pooka 			break;
    349  1.29  pooka 		}
    350  1.29  pooka 
    351  1.29  pooka 		if (ifd->ifd_cmd != 0) {
    352  1.29  pooka 			rv = EINVAL;
    353  1.29  pooka 			break;
    354  1.29  pooka 		}
    355  1.29  pooka 
    356  1.29  pooka 		rv = copyoutstr(sc->sc_backfile, ifd->ifd_data,
    357  1.29  pooka 		    MIN(sc->sc_backfilelen, ifd->ifd_len), NULL);
    358  1.29  pooka 		break;
    359  1.29  pooka 	case SIOCSLINKSTR:
    360  1.29  pooka 		if (ifp->if_flags & IFF_UP) {
    361  1.29  pooka 			rv = EBUSY;
    362  1.29  pooka 			break;
    363  1.29  pooka 		}
    364  1.29  pooka 
    365  1.29  pooka 		ifd = data;
    366  1.29  pooka 		if (ifd->ifd_cmd == IFLINKSTR_UNSET) {
    367  1.29  pooka 			finibackend(sc);
    368  1.29  pooka 			rv = 0;
    369  1.29  pooka 			break;
    370  1.29  pooka 		} else if (ifd->ifd_cmd != 0) {
    371  1.29  pooka 			rv = EINVAL;
    372  1.29  pooka 			break;
    373  1.29  pooka 		} else if (sc->sc_backfile) {
    374  1.29  pooka 			rv = EBUSY;
    375  1.29  pooka 			break;
    376  1.29  pooka 		}
    377  1.29  pooka 
    378  1.29  pooka 		if (ifd->ifd_len > MAXPATHLEN) {
    379  1.29  pooka 			rv = E2BIG;
    380  1.29  pooka 			break;
    381  1.29  pooka 		} else if (ifd->ifd_len < 1) {
    382  1.29  pooka 			rv = EINVAL;
    383  1.29  pooka 			break;
    384  1.29  pooka 		}
    385  1.29  pooka 
    386  1.29  pooka 		path = kmem_alloc(ifd->ifd_len, KM_SLEEP);
    387  1.29  pooka 		rv = copyinstr(ifd->ifd_data, path, ifd->ifd_len, NULL);
    388  1.29  pooka 		if (rv) {
    389  1.29  pooka 			kmem_free(path, ifd->ifd_len);
    390  1.29  pooka 			break;
    391  1.29  pooka 		}
    392  1.29  pooka 		memfd = rumpuser_open(path, O_RDWR | O_CREAT, &rv);
    393  1.29  pooka 		if (memfd == -1) {
    394  1.29  pooka 			kmem_free(path, ifd->ifd_len);
    395  1.29  pooka 			break;
    396  1.29  pooka 		}
    397  1.29  pooka 		rv = initbackend(sc, memfd);
    398  1.29  pooka 		if (rv) {
    399  1.29  pooka 			kmem_free(path, ifd->ifd_len);
    400  1.29  pooka 			rumpuser_close(memfd, &dummy);
    401  1.29  pooka 			break;
    402  1.29  pooka 		}
    403  1.29  pooka 		sc->sc_backfile = path;
    404  1.29  pooka 		sc->sc_backfilelen = ifd->ifd_len;
    405  1.29  pooka 
    406  1.29  pooka 		break;
    407  1.29  pooka 	default:
    408  1.29  pooka 		rv = ether_ioctl(ifp, cmd, data);
    409  1.29  pooka 		if (rv == ENETRESET)
    410  1.29  pooka 			rv = 0;
    411  1.29  pooka 		break;
    412  1.29  pooka 	}
    413   1.1  pooka 	splx(s);
    414   1.1  pooka 
    415   1.1  pooka 	return rv;
    416   1.1  pooka }
    417   1.1  pooka 
    418   1.1  pooka /* send everything in-context */
    419   1.1  pooka static void
    420   1.1  pooka shmif_start(struct ifnet *ifp)
    421   1.1  pooka {
    422   1.1  pooka 	struct shmif_sc *sc = ifp->if_softc;
    423  1.26  pooka 	struct shmif_mem *busmem = sc->sc_busmem;
    424   1.1  pooka 	struct mbuf *m, *m0;
    425  1.26  pooka 	uint32_t dataoff;
    426  1.26  pooka 	uint32_t pktsize, pktwrote;
    427   1.1  pooka 	bool wrote = false;
    428  1.24  pooka 	bool wrap;
    429   1.1  pooka 	int error;
    430   1.1  pooka 
    431  1.26  pooka 	ifp->if_flags |= IFF_OACTIVE;
    432  1.26  pooka 
    433   1.1  pooka 	for (;;) {
    434  1.20  pooka 		struct shmif_pkthdr sp;
    435  1.20  pooka 		struct timeval tv;
    436  1.20  pooka 
    437   1.1  pooka 		IF_DEQUEUE(&ifp->if_snd, m0);
    438   1.1  pooka 		if (m0 == NULL) {
    439   1.1  pooka 			break;
    440   1.1  pooka 		}
    441   1.1  pooka 
    442  1.25  pooka 		pktsize = 0;
    443  1.19  pooka 		for (m = m0; m != NULL; m = m->m_next) {
    444  1.19  pooka 			pktsize += m->m_len;
    445  1.19  pooka 		}
    446  1.26  pooka 		KASSERT(pktsize <= ETHERMTU + ETHER_HDR_LEN);
    447  1.19  pooka 
    448  1.20  pooka 		getmicrouptime(&tv);
    449  1.20  pooka 		sp.sp_len = pktsize;
    450  1.20  pooka 		sp.sp_sec = tv.tv_sec;
    451  1.20  pooka 		sp.sp_usec = tv.tv_usec;
    452  1.20  pooka 
    453  1.26  pooka 		shmif_lockbus(busmem);
    454  1.26  pooka 		KASSERT(busmem->shm_magic == SHMIF_MAGIC);
    455  1.26  pooka 		busmem->shm_last = shmif_nextpktoff(busmem, busmem->shm_last);
    456  1.21  pooka 
    457  1.24  pooka 		wrap = false;
    458  1.26  pooka 		dataoff = shmif_buswrite(busmem,
    459  1.26  pooka 		    busmem->shm_last, &sp, sizeof(sp), &wrap);
    460  1.26  pooka 		pktwrote = 0;
    461   1.1  pooka 		for (m = m0; m != NULL; m = m->m_next) {
    462  1.26  pooka 			pktwrote += m->m_len;
    463  1.26  pooka 			dataoff = shmif_buswrite(busmem, dataoff,
    464  1.19  pooka 			    mtod(m, void *), m->m_len, &wrap);
    465   1.1  pooka 		}
    466  1.26  pooka 		KASSERT(pktwrote == pktsize);
    467  1.27  pooka 		if (wrap) {
    468  1.26  pooka 			busmem->shm_gen++;
    469  1.27  pooka 			DPRINTF(("bus generation now %d\n", busmem->shm_gen));
    470  1.27  pooka 		}
    471  1.26  pooka 		shmif_unlockbus(busmem);
    472   1.1  pooka 
    473   1.1  pooka 		m_freem(m0);
    474   1.1  pooka 		wrote = true;
    475   1.1  pooka 
    476   1.1  pooka 		DPRINTF(("shmif_start: send %d bytes at off %d\n",
    477  1.27  pooka 		    pktsize, busmem->shm_last));
    478   1.1  pooka 	}
    479  1.26  pooka 
    480  1.26  pooka 	ifp->if_flags &= ~IFF_OACTIVE;
    481  1.26  pooka 
    482   1.1  pooka 	/* wakeup */
    483   1.1  pooka 	if (wrote)
    484  1.15  pooka 		rumpuser_pwrite(sc->sc_memfd,
    485  1.15  pooka 		    &busversion, sizeof(busversion), IFMEM_WAKEUP, &error);
    486   1.1  pooka }
    487   1.1  pooka 
    488   1.1  pooka static void
    489   1.1  pooka shmif_stop(struct ifnet *ifp, int disable)
    490   1.1  pooka {
    491   1.1  pooka 
    492   1.1  pooka 	panic("%s: unimpl", __func__);
    493   1.1  pooka }
    494   1.1  pooka 
    495  1.27  pooka 
    496  1.27  pooka /*
    497  1.27  pooka  * Check if we have been sleeping too long.  Basically,
    498  1.27  pooka  * our in-sc nextpkt must by first <= nextpkt <= last"+1".
    499  1.27  pooka  * We use the fact that first is guaranteed to never overlap
    500  1.27  pooka  * with the last frame in the ring.
    501  1.27  pooka  */
    502  1.27  pooka static __inline bool
    503  1.27  pooka stillvalid_p(struct shmif_sc *sc)
    504  1.27  pooka {
    505  1.27  pooka 	struct shmif_mem *busmem = sc->sc_busmem;
    506  1.27  pooka 	unsigned gendiff = busmem->shm_gen - sc->sc_devgen;
    507  1.27  pooka 	uint32_t lastoff, devoff;
    508  1.27  pooka 
    509  1.27  pooka 	KASSERT(busmem->shm_first != busmem->shm_last);
    510  1.27  pooka 
    511  1.27  pooka 	/* normalize onto a 2x busmem chunk */
    512  1.27  pooka 	devoff = sc->sc_nextpacket;
    513  1.27  pooka 	lastoff = shmif_nextpktoff(busmem, busmem->shm_last);
    514  1.27  pooka 
    515  1.27  pooka 	/* trivial case */
    516  1.27  pooka 	if (gendiff > 1)
    517  1.27  pooka 		return false;
    518  1.27  pooka 	KASSERT(gendiff <= 1);
    519  1.27  pooka 
    520  1.27  pooka 	/* Normalize onto 2x busmem chunk */
    521  1.27  pooka 	if (busmem->shm_first >= lastoff) {
    522  1.27  pooka 		lastoff += BUSMEM_DATASIZE;
    523  1.27  pooka 		if (gendiff == 0)
    524  1.27  pooka 			devoff += BUSMEM_DATASIZE;
    525  1.27  pooka 	} else {
    526  1.27  pooka 		if (gendiff)
    527  1.27  pooka 			return false;
    528  1.27  pooka 	}
    529  1.27  pooka 
    530  1.27  pooka 	return devoff >= busmem->shm_first && devoff <= lastoff;
    531  1.27  pooka }
    532  1.27  pooka 
    533   1.1  pooka static void
    534   1.1  pooka shmif_rcv(void *arg)
    535   1.1  pooka {
    536   1.1  pooka 	struct ifnet *ifp = arg;
    537   1.1  pooka 	struct shmif_sc *sc = ifp->if_softc;
    538  1.26  pooka 	struct shmif_mem *busmem = sc->sc_busmem;
    539   1.1  pooka 	struct mbuf *m = NULL;
    540   1.1  pooka 	struct ether_header *eth;
    541  1.27  pooka 	uint32_t nextpkt;
    542  1.24  pooka 	bool wrap;
    543   1.1  pooka 	int error;
    544   1.1  pooka 
    545   1.1  pooka 	for (;;) {
    546  1.20  pooka 		struct shmif_pkthdr sp;
    547  1.20  pooka 
    548   1.1  pooka 		if (m == NULL) {
    549   1.1  pooka 			m = m_gethdr(M_WAIT, MT_DATA);
    550   1.1  pooka 			MCLGET(m, M_WAIT);
    551   1.1  pooka 		}
    552   1.1  pooka 
    553  1.26  pooka 		DPRINTF(("waiting %d/%d\n", sc->sc_nextpacket, sc->sc_devgen));
    554  1.26  pooka 		KASSERT(m->m_flags & M_EXT);
    555   1.1  pooka 
    556  1.26  pooka 		shmif_lockbus(busmem);
    557  1.26  pooka 		KASSERT(busmem->shm_magic == SHMIF_MAGIC);
    558  1.27  pooka 		KASSERT(busmem->shm_gen >= sc->sc_devgen);
    559   1.1  pooka 
    560   1.1  pooka 		/* need more data? */
    561  1.27  pooka 		if (sc->sc_devgen == busmem->shm_gen &&
    562  1.26  pooka 		    shmif_nextpktoff(busmem, busmem->shm_last)
    563  1.26  pooka 		     == sc->sc_nextpacket) {
    564  1.26  pooka 			shmif_unlockbus(busmem);
    565   1.1  pooka 			error = 0;
    566   1.1  pooka 			rumpuser_writewatchfile_wait(sc->sc_kq, NULL, &error);
    567   1.1  pooka 			if (__predict_false(error))
    568   1.1  pooka 				printf("shmif_rcv: wait failed %d\n", error);
    569   1.1  pooka 			continue;
    570   1.1  pooka 		}
    571   1.1  pooka 
    572  1.27  pooka 		if (stillvalid_p(sc)) {
    573  1.27  pooka 			nextpkt = sc->sc_nextpacket;
    574  1.27  pooka 		} else {
    575  1.27  pooka 			KASSERT(busmem->shm_gen > 0);
    576  1.26  pooka 			nextpkt = busmem->shm_first;
    577  1.26  pooka 			if (busmem->shm_first > busmem->shm_last)
    578  1.27  pooka 				sc->sc_devgen = busmem->shm_gen - 1;
    579  1.26  pooka 			else
    580  1.27  pooka 				sc->sc_devgen = busmem->shm_gen;
    581  1.27  pooka 			DPRINTF(("dev %p overrun, new data: %d/%d\n",
    582  1.27  pooka 			    sc, nextpkt, sc->sc_devgen));
    583  1.26  pooka 		}
    584  1.26  pooka 
    585  1.26  pooka 		/*
    586  1.26  pooka 		 * If our read pointer is ahead the bus last write, our
    587  1.26  pooka 		 * generation must be one behind.
    588  1.26  pooka 		 */
    589  1.26  pooka 		KASSERT(!(nextpkt > busmem->shm_last
    590  1.27  pooka 		    && sc->sc_devgen == busmem->shm_gen));
    591  1.26  pooka 
    592  1.24  pooka 		wrap = false;
    593  1.26  pooka 		nextpkt = shmif_busread(busmem, &sp,
    594  1.26  pooka 		    nextpkt, sizeof(sp), &wrap);
    595  1.26  pooka 		KASSERT(sp.sp_len <= ETHERMTU + ETHER_HDR_LEN);
    596  1.26  pooka 		nextpkt = shmif_busread(busmem, mtod(m, void *),
    597  1.26  pooka 		    nextpkt, sp.sp_len, &wrap);
    598   1.1  pooka 
    599   1.1  pooka 		DPRINTF(("shmif_rcv: read packet of length %d at %d\n",
    600  1.20  pooka 		    sp.sp_len, nextpkt));
    601   1.1  pooka 
    602  1.26  pooka 		sc->sc_nextpacket = nextpkt;
    603  1.19  pooka 		shmif_unlockbus(sc->sc_busmem);
    604   1.1  pooka 
    605  1.27  pooka 		if (wrap) {
    606  1.26  pooka 			sc->sc_devgen++;
    607  1.27  pooka 			DPRINTF(("dev %p generation now %d\n",
    608  1.27  pooka 			    sc, sc->sc_devgen));
    609  1.27  pooka 		}
    610  1.26  pooka 
    611  1.20  pooka 		m->m_len = m->m_pkthdr.len = sp.sp_len;
    612   1.1  pooka 		m->m_pkthdr.rcvif = ifp;
    613   1.1  pooka 
    614  1.10  pooka 		/* if it's from us, don't pass up and reuse storage space */
    615   1.1  pooka 		eth = mtod(m, struct ether_header *);
    616   1.1  pooka 		if (memcmp(eth->ether_shost, sc->sc_myaddr, 6) != 0) {
    617  1.22  pooka 			KERNEL_LOCK(1, NULL);
    618   1.1  pooka 			ifp->if_input(ifp, m);
    619  1.22  pooka 			KERNEL_UNLOCK_ONE(NULL);
    620   1.1  pooka 			m = NULL;
    621   1.1  pooka 		}
    622   1.1  pooka 	}
    623   1.1  pooka 
    624   1.1  pooka 	panic("shmif_worker is a lazy boy %d\n", error);
    625   1.1  pooka }
    626