Home | History | Annotate | Line # | Download | only in libshmif
if_shmem.c revision 1.2
      1  1.2  pooka /*	$NetBSD: if_shmem.c,v 1.2 2009/02/28 16:15:19 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.2  pooka __KERNEL_RCSID(0, "$NetBSD: if_shmem.c,v 1.2 2009/02/28 16:15:19 pooka Exp $");
     32  1.1  pooka 
     33  1.1  pooka #include <sys/param.h>
     34  1.1  pooka #include <sys/condvar.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.1  pooka 
     40  1.1  pooka #include <net/if.h>
     41  1.1  pooka #include <net/if_ether.h>
     42  1.1  pooka 
     43  1.1  pooka #include <netinet/in.h>
     44  1.1  pooka #include <netinet/in_var.h>
     45  1.1  pooka 
     46  1.1  pooka #include <rump/rump.h>
     47  1.1  pooka #include <rump/rumpuser.h>
     48  1.1  pooka 
     49  1.1  pooka #include "rump_private.h"
     50  1.1  pooka 
     51  1.1  pooka /*
     52  1.1  pooka  * A virtual ethernet interface which uses shared memory from a
     53  1.1  pooka  * memory mapped file as the bus.
     54  1.1  pooka  */
     55  1.1  pooka 
     56  1.1  pooka static int	shmif_init(struct ifnet *);
     57  1.1  pooka static int	shmif_ioctl(struct ifnet *, u_long, void *);
     58  1.1  pooka static void	shmif_start(struct ifnet *);
     59  1.1  pooka static void	shmif_stop(struct ifnet *, int);
     60  1.1  pooka 
     61  1.1  pooka struct shmif_sc {
     62  1.1  pooka 	struct ethercom sc_ec;
     63  1.1  pooka 	uint8_t sc_myaddr[6];
     64  1.1  pooka 	uint8_t *sc_busmem;
     65  1.1  pooka 	int sc_memfd;
     66  1.1  pooka 	int sc_kq;
     67  1.1  pooka 
     68  1.1  pooka 	uint32_t sc_nextpacket;
     69  1.1  pooka 	uint32_t sc_prevgen;
     70  1.1  pooka };
     71  1.1  pooka #define IFMEM_LOCK		(0)
     72  1.1  pooka #define IFMEM_GENERATION	(8)
     73  1.1  pooka #define IFMEM_LASTPACKET	(12)
     74  1.1  pooka #define IFMEM_WAKEUP		(16)
     75  1.1  pooka #define IFMEM_DATA		(20)
     76  1.1  pooka 
     77  1.1  pooka #define BUSCTRL_ATOFF(sc, off)	((uint32_t *)(sc->sc_busmem+(off)))
     78  1.1  pooka 
     79  1.1  pooka #define BUSMEM_SIZE 65536 /* enough? */
     80  1.1  pooka 
     81  1.1  pooka static void shmif_rcv(void *);
     82  1.1  pooka 
     83  1.1  pooka static uint32_t numif;
     84  1.1  pooka 
     85  1.2  pooka /*
     86  1.2  pooka  * This locking needs work and will misbehave severely if:
     87  1.2  pooka  * 1) the backing memory has to be paged in
     88  1.2  pooka  * 2) some lockholder exits while holding the lock
     89  1.2  pooka  */
     90  1.1  pooka static void
     91  1.1  pooka lockbus(struct shmif_sc *sc)
     92  1.1  pooka {
     93  1.1  pooka 
     94  1.1  pooka 	__cpu_simple_lock((__cpu_simple_lock_t *)sc->sc_busmem);
     95  1.1  pooka }
     96  1.1  pooka 
     97  1.1  pooka static void
     98  1.1  pooka unlockbus(struct shmif_sc *sc)
     99  1.1  pooka {
    100  1.1  pooka 
    101  1.1  pooka 	__cpu_simple_unlock((__cpu_simple_lock_t *)sc->sc_busmem);
    102  1.1  pooka }
    103  1.1  pooka 
    104  1.1  pooka static uint32_t
    105  1.1  pooka busread(struct shmif_sc *sc, void *dest, uint32_t off, size_t len)
    106  1.1  pooka {
    107  1.1  pooka 	size_t chunk;
    108  1.1  pooka 
    109  1.1  pooka 	KASSERT(len < (BUSMEM_SIZE - IFMEM_DATA) && off <= BUSMEM_SIZE);
    110  1.1  pooka 	chunk = MIN(len, BUSMEM_SIZE - off);
    111  1.1  pooka 	memcpy(dest, sc->sc_busmem + off, chunk);
    112  1.1  pooka 	len -= chunk;
    113  1.1  pooka 
    114  1.1  pooka 	if (len == 0)
    115  1.1  pooka 		return off + chunk;
    116  1.1  pooka 
    117  1.1  pooka 	/* else, wraps around */
    118  1.1  pooka 	off = IFMEM_DATA;
    119  1.1  pooka 	sc->sc_prevgen = *BUSCTRL_ATOFF(sc, IFMEM_GENERATION);
    120  1.1  pooka 
    121  1.1  pooka 	/* finish reading */
    122  1.1  pooka 	memcpy((uint8_t *)dest + chunk, sc->sc_busmem + off, len);
    123  1.1  pooka 	return off + len;
    124  1.1  pooka }
    125  1.1  pooka 
    126  1.1  pooka static uint32_t
    127  1.1  pooka buswrite(struct shmif_sc *sc, uint32_t off, void *data, size_t len)
    128  1.1  pooka {
    129  1.1  pooka 	size_t chunk;
    130  1.1  pooka 
    131  1.1  pooka 	KASSERT(len < (BUSMEM_SIZE - IFMEM_DATA) && off <= BUSMEM_SIZE);
    132  1.1  pooka 
    133  1.1  pooka 	chunk = MIN(len, BUSMEM_SIZE - off);
    134  1.1  pooka 	memcpy(sc->sc_busmem + off, data, chunk);
    135  1.1  pooka 	len -= chunk;
    136  1.1  pooka 
    137  1.1  pooka 	if (len == 0)
    138  1.1  pooka 		return off + chunk;
    139  1.1  pooka 
    140  1.1  pooka 	DPRINTF(("buswrite wrap: wrote %d bytes to %d, left %d to %d",
    141  1.1  pooka 	    chunk, off, len, IFMEM_DATA));
    142  1.1  pooka 
    143  1.1  pooka 	/* else, wraps around */
    144  1.1  pooka 	off = IFMEM_DATA;
    145  1.1  pooka 	(*BUSCTRL_ATOFF(sc, IFMEM_GENERATION))++;
    146  1.1  pooka 	sc->sc_prevgen = *BUSCTRL_ATOFF(sc, IFMEM_GENERATION);
    147  1.1  pooka 
    148  1.1  pooka 	/* finish writing */
    149  1.1  pooka 	memcpy(sc->sc_busmem + off, (uint8_t *)data + chunk, len);
    150  1.1  pooka 	return off + len;
    151  1.1  pooka }
    152  1.1  pooka 
    153  1.1  pooka static inline uint32_t
    154  1.1  pooka advance(uint32_t oldoff, uint32_t delta)
    155  1.1  pooka {
    156  1.1  pooka 	uint32_t newoff;
    157  1.1  pooka 
    158  1.1  pooka 	newoff = oldoff + delta;
    159  1.1  pooka 	if (newoff >= BUSMEM_SIZE)
    160  1.1  pooka 		newoff -= (BUSMEM_SIZE - IFMEM_DATA);
    161  1.1  pooka 	return newoff;
    162  1.1  pooka 
    163  1.1  pooka }
    164  1.1  pooka 
    165  1.1  pooka static uint32_t
    166  1.1  pooka nextpktoff(struct shmif_sc *sc, uint32_t oldoff)
    167  1.1  pooka {
    168  1.1  pooka 	uint32_t oldlen;
    169  1.1  pooka 
    170  1.1  pooka 	busread(sc, &oldlen, oldoff, 4);
    171  1.1  pooka 	KASSERT(oldlen < BUSMEM_SIZE - IFMEM_DATA);
    172  1.1  pooka 
    173  1.1  pooka 	return advance(oldoff, 4 + oldlen);
    174  1.1  pooka }
    175  1.1  pooka 
    176  1.1  pooka int rump_shmif_create(const char *, int *); /* XXX */
    177  1.1  pooka 
    178  1.1  pooka int
    179  1.1  pooka rump_shmif_create(const char *path, int *ifnum)
    180  1.1  pooka {
    181  1.1  pooka 	struct shmif_sc *sc;
    182  1.1  pooka 	struct ifnet *ifp;
    183  1.1  pooka 	uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0xa0, 0x00, 0x00, 0x00, 0x00 };
    184  1.1  pooka 	uint32_t randnum;
    185  1.1  pooka 	unsigned mynum;
    186  1.1  pooka 	int error;
    187  1.1  pooka 
    188  1.1  pooka 	randnum = arc4random();
    189  1.1  pooka 	memcpy(&enaddr[2], &randnum, 4);
    190  1.1  pooka 	mynum = atomic_inc_uint_nv(&numif)-1;
    191  1.1  pooka 
    192  1.1  pooka 	sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
    193  1.1  pooka 	ifp = &sc->sc_ec.ec_if;
    194  1.1  pooka 	memcpy(sc->sc_myaddr, enaddr, sizeof(enaddr));
    195  1.1  pooka 
    196  1.1  pooka 	sc->sc_memfd = rumpuser_open(path, O_RDWR | O_CREAT, &error);
    197  1.1  pooka 	if (sc->sc_memfd == -1)
    198  1.1  pooka 		goto fail;
    199  1.1  pooka 	sc->sc_busmem = rumpuser_filemmap(sc->sc_memfd, 0, BUSMEM_SIZE,
    200  1.1  pooka 	    1, 1, &error);
    201  1.1  pooka 	if (error)
    202  1.1  pooka 		goto fail;
    203  1.1  pooka 
    204  1.1  pooka 	lockbus(sc);
    205  1.1  pooka 	if (*BUSCTRL_ATOFF(sc, IFMEM_LASTPACKET) == 0)
    206  1.1  pooka 		*BUSCTRL_ATOFF(sc, IFMEM_LASTPACKET) = IFMEM_DATA;
    207  1.1  pooka 	sc->sc_nextpacket = *BUSCTRL_ATOFF(sc, IFMEM_LASTPACKET);
    208  1.1  pooka 	sc->sc_prevgen = *BUSCTRL_ATOFF(sc, IFMEM_GENERATION);
    209  1.1  pooka 	unlockbus(sc);
    210  1.1  pooka 
    211  1.1  pooka 	sc->sc_kq = rumpuser_writewatchfile_setup(-1, sc->sc_memfd, 0, &error);
    212  1.1  pooka 	if (sc->sc_kq == -1)
    213  1.1  pooka 		goto fail;
    214  1.1  pooka 
    215  1.1  pooka 	if (rump_threads) {
    216  1.1  pooka 		error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    217  1.1  pooka 		    shmif_rcv, ifp, NULL, "shmif");
    218  1.1  pooka 		if (error) {
    219  1.1  pooka 			goto fail;
    220  1.1  pooka 		}
    221  1.1  pooka 	} else {
    222  1.1  pooka 		printf("WARNING: threads not enabled, shmif NOT working\n");
    223  1.1  pooka 	}
    224  1.1  pooka 
    225  1.1  pooka 	sprintf(ifp->if_xname, "shmif%d", mynum);
    226  1.1  pooka 	ifp->if_softc = sc;
    227  1.1  pooka 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    228  1.1  pooka 	ifp->if_init = shmif_init;
    229  1.1  pooka 	ifp->if_ioctl = shmif_ioctl;
    230  1.1  pooka 	ifp->if_start = shmif_start;
    231  1.1  pooka 	ifp->if_stop = shmif_stop;
    232  1.1  pooka 	ifp->if_mtu = 1518;
    233  1.1  pooka 
    234  1.1  pooka 	if_attach(ifp);
    235  1.1  pooka 	ether_ifattach(ifp, enaddr);
    236  1.1  pooka 
    237  1.2  pooka 	if (ifnum)
    238  1.2  pooka 		*ifnum = mynum;
    239  1.1  pooka 	return 0;
    240  1.1  pooka 
    241  1.1  pooka  fail:
    242  1.1  pooka 	panic("rump_shmemif_create: fixme");
    243  1.1  pooka }
    244  1.1  pooka 
    245  1.1  pooka static int
    246  1.1  pooka shmif_init(struct ifnet *ifp)
    247  1.1  pooka {
    248  1.1  pooka 
    249  1.1  pooka 	ifp->if_flags |= IFF_RUNNING;
    250  1.1  pooka 	return 0;
    251  1.1  pooka }
    252  1.1  pooka 
    253  1.1  pooka static int
    254  1.1  pooka shmif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    255  1.1  pooka {
    256  1.1  pooka 	int s, rv;
    257  1.1  pooka 
    258  1.1  pooka 	s = splnet();
    259  1.1  pooka 	rv = ether_ioctl(ifp, cmd, data);
    260  1.1  pooka 	splx(s);
    261  1.1  pooka 
    262  1.1  pooka 	return rv;
    263  1.1  pooka }
    264  1.1  pooka 
    265  1.1  pooka /* send everything in-context */
    266  1.1  pooka static void
    267  1.1  pooka shmif_start(struct ifnet *ifp)
    268  1.1  pooka {
    269  1.1  pooka 	struct shmif_sc *sc = ifp->if_softc;
    270  1.1  pooka 	struct mbuf *m, *m0;
    271  1.1  pooka 	uint32_t lastoff, dataoff, npktlenoff;
    272  1.1  pooka 	uint32_t pktsize = 0;
    273  1.1  pooka 	bool wrote = false;
    274  1.1  pooka 	int error;
    275  1.1  pooka 
    276  1.1  pooka 	for (;;) {
    277  1.1  pooka 		IF_DEQUEUE(&ifp->if_snd, m0);
    278  1.1  pooka 		if (m0 == NULL) {
    279  1.1  pooka 			break;
    280  1.1  pooka 		}
    281  1.1  pooka 
    282  1.1  pooka 		lockbus(sc);
    283  1.1  pooka 		lastoff = *BUSCTRL_ATOFF(sc, IFMEM_LASTPACKET);
    284  1.1  pooka 
    285  1.1  pooka 		npktlenoff = nextpktoff(sc, lastoff);
    286  1.1  pooka 		dataoff = advance(npktlenoff, 4);
    287  1.1  pooka 
    288  1.1  pooka 		for (m = m0; m != NULL; m = m->m_next) {
    289  1.1  pooka 			pktsize += m->m_len;
    290  1.1  pooka 			dataoff = buswrite(sc, dataoff, mtod(m, void *),
    291  1.1  pooka 			    m->m_len);
    292  1.1  pooka 		}
    293  1.1  pooka 		buswrite(sc, npktlenoff, &pktsize, 4);
    294  1.1  pooka 		*BUSCTRL_ATOFF(sc, IFMEM_LASTPACKET) = npktlenoff;
    295  1.1  pooka 		unlockbus(sc);
    296  1.1  pooka 
    297  1.1  pooka 		m_freem(m0);
    298  1.1  pooka 		wrote = true;
    299  1.1  pooka 
    300  1.1  pooka 		DPRINTF(("shmif_start: send %d bytes at off %d\n",
    301  1.1  pooka 		    pktsize, npktlenoff));
    302  1.1  pooka 	}
    303  1.1  pooka 	/* wakeup */
    304  1.1  pooka 	if (wrote)
    305  1.1  pooka 		rumpuser_pwrite(sc->sc_memfd, &error, 4, IFMEM_WAKEUP, &error);
    306  1.1  pooka }
    307  1.1  pooka 
    308  1.1  pooka static void
    309  1.1  pooka shmif_stop(struct ifnet *ifp, int disable)
    310  1.1  pooka {
    311  1.1  pooka 
    312  1.1  pooka 	panic("%s: unimpl", __func__);
    313  1.1  pooka }
    314  1.1  pooka 
    315  1.1  pooka static void
    316  1.1  pooka shmif_rcv(void *arg)
    317  1.1  pooka {
    318  1.1  pooka 	struct ifnet *ifp = arg;
    319  1.1  pooka 	struct shmif_sc *sc = ifp->if_softc;
    320  1.1  pooka 	struct mbuf *m = NULL;
    321  1.1  pooka 	struct ether_header *eth;
    322  1.1  pooka 	uint32_t nextpkt, pktlen, lastpkt, busgen, lastnext;
    323  1.1  pooka 	int error;
    324  1.1  pooka 
    325  1.1  pooka 	for (;;) {
    326  1.1  pooka 		if (m == NULL) {
    327  1.1  pooka 			m = m_gethdr(M_WAIT, MT_DATA);
    328  1.1  pooka 			MCLGET(m, M_WAIT);
    329  1.1  pooka 		}
    330  1.1  pooka 
    331  1.1  pooka 		DPRINTF(("waiting %d/%d\n", sc->sc_nextpacket, sc->sc_prevgen));
    332  1.1  pooka 
    333  1.1  pooka 		KASSERT(m->m_flags & M_EXT);
    334  1.1  pooka 		lockbus(sc);
    335  1.1  pooka 		lastpkt = *BUSCTRL_ATOFF(sc, IFMEM_LASTPACKET);
    336  1.1  pooka 		busgen = *BUSCTRL_ATOFF(sc, IFMEM_GENERATION);
    337  1.1  pooka 		lastnext = nextpktoff(sc, lastpkt);
    338  1.1  pooka 		if ((lastnext > sc->sc_nextpacket && busgen > sc->sc_prevgen)
    339  1.1  pooka 		    || (busgen > sc->sc_prevgen+1)) {
    340  1.1  pooka 			nextpkt = lastpkt;
    341  1.1  pooka 			sc->sc_prevgen = busgen;
    342  1.1  pooka 			printf("DROPPING\n");
    343  1.1  pooka 		} else {
    344  1.1  pooka 			nextpkt = sc->sc_nextpacket;
    345  1.1  pooka 		}
    346  1.1  pooka 
    347  1.1  pooka 		/* need more data? */
    348  1.1  pooka 		if (lastnext == nextpkt && sc->sc_prevgen == busgen){
    349  1.1  pooka 			unlockbus(sc);
    350  1.1  pooka 			error = 0;
    351  1.1  pooka 			rumpuser_writewatchfile_wait(sc->sc_kq, NULL, &error);
    352  1.1  pooka 			if (__predict_false(error))
    353  1.1  pooka 				printf("shmif_rcv: wait failed %d\n", error);
    354  1.1  pooka 			continue;
    355  1.1  pooka 		}
    356  1.1  pooka 
    357  1.1  pooka 		busread(sc, &pktlen, nextpkt, 4);
    358  1.1  pooka 		busread(sc, mtod(m, void *), advance(nextpkt, 4), pktlen);
    359  1.1  pooka 
    360  1.1  pooka 		DPRINTF(("shmif_rcv: read packet of length %d at %d\n",
    361  1.1  pooka 		    pktlen, nextpkt));
    362  1.1  pooka 
    363  1.1  pooka 		sc->sc_nextpacket = nextpktoff(sc, nextpkt);
    364  1.1  pooka 		sc->sc_prevgen = busgen;
    365  1.1  pooka 		unlockbus(sc);
    366  1.1  pooka 
    367  1.1  pooka 		m->m_len = m->m_pkthdr.len = pktlen;
    368  1.1  pooka 		m->m_pkthdr.rcvif = ifp;
    369  1.1  pooka 
    370  1.1  pooka 		/* if it's to us, don't pass up and reuse storage space */
    371  1.1  pooka 		eth = mtod(m, struct ether_header *);
    372  1.1  pooka 		if (memcmp(eth->ether_shost, sc->sc_myaddr, 6) != 0) {
    373  1.1  pooka 			ifp->if_input(ifp, m);
    374  1.1  pooka 			m = NULL;
    375  1.1  pooka 		}
    376  1.1  pooka 	}
    377  1.1  pooka 
    378  1.1  pooka 	panic("shmif_worker is a lazy boy %d\n", error);
    379  1.1  pooka }
    380