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