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