Home | History | Annotate | Line # | Download | only in libshmif
if_shmem.c revision 1.19
      1  1.19   pooka /*	$NetBSD: if_shmem.c,v 1.19 2010/08/12 21:41:47 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.19   pooka __KERNEL_RCSID(0, "$NetBSD: if_shmem.c,v 1.19 2010/08/12 21:41:47 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.3  martin #include <sys/atomic.h>
     40   1.1   pooka 
     41   1.1   pooka #include <net/if.h>
     42   1.1   pooka #include <net/if_ether.h>
     43   1.1   pooka 
     44   1.1   pooka #include <netinet/in.h>
     45   1.1   pooka #include <netinet/in_var.h>
     46   1.1   pooka 
     47   1.1   pooka #include <rump/rump.h>
     48   1.1   pooka #include <rump/rumpuser.h>
     49   1.1   pooka 
     50   1.1   pooka #include "rump_private.h"
     51   1.9   pooka #include "rump_net_private.h"
     52   1.1   pooka 
     53   1.1   pooka /*
     54   1.1   pooka  * A virtual ethernet interface which uses shared memory from a
     55   1.1   pooka  * memory mapped file as the bus.
     56   1.1   pooka  */
     57   1.1   pooka 
     58   1.1   pooka static int	shmif_init(struct ifnet *);
     59   1.1   pooka static int	shmif_ioctl(struct ifnet *, u_long, void *);
     60   1.1   pooka static void	shmif_start(struct ifnet *);
     61   1.1   pooka static void	shmif_stop(struct ifnet *, int);
     62   1.1   pooka 
     63  1.16   pooka #include "shmifvar.h"
     64  1.16   pooka 
     65   1.1   pooka struct shmif_sc {
     66   1.1   pooka 	struct ethercom sc_ec;
     67   1.1   pooka 	uint8_t sc_myaddr[6];
     68  1.16   pooka 	struct shmif_mem *sc_busmem;
     69   1.1   pooka 	int sc_memfd;
     70   1.1   pooka 	int sc_kq;
     71   1.1   pooka 
     72   1.1   pooka 	uint32_t sc_nextpacket;
     73   1.1   pooka 	uint32_t sc_prevgen;
     74   1.1   pooka };
     75   1.1   pooka 
     76  1.17   pooka static const uint32_t busversion = SHMIF_VERSION;
     77   1.1   pooka 
     78   1.1   pooka static void shmif_rcv(void *);
     79   1.1   pooka 
     80   1.1   pooka static uint32_t numif;
     81   1.1   pooka 
     82   1.1   pooka int
     83   1.1   pooka rump_shmif_create(const char *path, int *ifnum)
     84   1.1   pooka {
     85   1.1   pooka 	struct shmif_sc *sc;
     86   1.1   pooka 	struct ifnet *ifp;
     87   1.1   pooka 	uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0xa0, 0x00, 0x00, 0x00, 0x00 };
     88   1.1   pooka 	uint32_t randnum;
     89   1.1   pooka 	unsigned mynum;
     90   1.1   pooka 	int error;
     91   1.1   pooka 
     92   1.1   pooka 	randnum = arc4random();
     93  1.15   pooka 	memcpy(&enaddr[2], &randnum, sizeof(randnum));
     94   1.1   pooka 	mynum = atomic_inc_uint_nv(&numif)-1;
     95   1.1   pooka 
     96   1.1   pooka 	sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
     97   1.1   pooka 	ifp = &sc->sc_ec.ec_if;
     98   1.1   pooka 	memcpy(sc->sc_myaddr, enaddr, sizeof(enaddr));
     99   1.1   pooka 
    100   1.1   pooka 	sc->sc_memfd = rumpuser_open(path, O_RDWR | O_CREAT, &error);
    101   1.1   pooka 	if (sc->sc_memfd == -1)
    102   1.1   pooka 		goto fail;
    103   1.1   pooka 	sc->sc_busmem = rumpuser_filemmap(sc->sc_memfd, 0, BUSMEM_SIZE,
    104   1.5   pooka 	    RUMPUSER_FILEMMAP_TRUNCATE | RUMPUSER_FILEMMAP_SHARED
    105   1.5   pooka 	    | RUMPUSER_FILEMMAP_READ | RUMPUSER_FILEMMAP_WRITE, &error);
    106   1.1   pooka 	if (error)
    107   1.1   pooka 		goto fail;
    108   1.1   pooka 
    109  1.17   pooka 	if (sc->sc_busmem->shm_magic && sc->sc_busmem->shm_magic != SHMIF_MAGIC)
    110  1.17   pooka 		panic("bus is not magical");
    111  1.17   pooka 
    112  1.19   pooka 	shmif_lockbus(sc->sc_busmem);
    113  1.19   pooka 	/* we're first?  initialize bus */
    114  1.19   pooka 	if (sc->sc_busmem->shm_magic == 0) {
    115  1.19   pooka 		sc->sc_busmem->shm_magic = SHMIF_MAGIC;
    116  1.19   pooka 		sc->sc_busmem->shm_first = BUSMEM_DATASIZE;
    117  1.19   pooka 	}
    118  1.19   pooka 
    119  1.16   pooka 	sc->sc_nextpacket = sc->sc_busmem->shm_last;
    120  1.16   pooka 	sc->sc_prevgen = sc->sc_busmem->shm_gen;
    121  1.19   pooka 	shmif_unlockbus(sc->sc_busmem);
    122   1.1   pooka 
    123   1.1   pooka 	sc->sc_kq = rumpuser_writewatchfile_setup(-1, sc->sc_memfd, 0, &error);
    124   1.1   pooka 	if (sc->sc_kq == -1)
    125   1.1   pooka 		goto fail;
    126   1.1   pooka 
    127   1.1   pooka 	sprintf(ifp->if_xname, "shmif%d", mynum);
    128   1.1   pooka 	ifp->if_softc = sc;
    129  1.13   pooka 	ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST;
    130   1.1   pooka 	ifp->if_init = shmif_init;
    131   1.1   pooka 	ifp->if_ioctl = shmif_ioctl;
    132   1.1   pooka 	ifp->if_start = shmif_start;
    133   1.1   pooka 	ifp->if_stop = shmif_stop;
    134   1.1   pooka 	ifp->if_mtu = 1518;
    135   1.1   pooka 
    136   1.1   pooka 	if_attach(ifp);
    137   1.1   pooka 	ether_ifattach(ifp, enaddr);
    138   1.1   pooka 
    139  1.12   pooka 	aprint_verbose("shmif%d: bus %s\n", mynum, path);
    140  1.12   pooka 	aprint_verbose("shmif%d: Ethernet address %s\n",
    141  1.12   pooka 	    mynum, ether_sprintf(enaddr));
    142  1.12   pooka 
    143   1.2   pooka 	if (ifnum)
    144   1.2   pooka 		*ifnum = mynum;
    145   1.1   pooka 	return 0;
    146   1.1   pooka 
    147   1.1   pooka  fail:
    148   1.1   pooka 	panic("rump_shmemif_create: fixme");
    149   1.1   pooka }
    150   1.1   pooka 
    151   1.1   pooka static int
    152   1.1   pooka shmif_init(struct ifnet *ifp)
    153   1.1   pooka {
    154   1.4   pooka 	int error = 0;
    155   1.4   pooka 
    156   1.4   pooka 	if (rump_threads) {
    157   1.4   pooka 		error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    158   1.4   pooka 		    shmif_rcv, ifp, NULL, "shmif");
    159   1.4   pooka 	} else {
    160   1.4   pooka 		printf("WARNING: threads not enabled, shmif NOT working\n");
    161   1.4   pooka 	}
    162   1.1   pooka 
    163   1.1   pooka 	ifp->if_flags |= IFF_RUNNING;
    164   1.4   pooka 	return error;
    165   1.1   pooka }
    166   1.1   pooka 
    167   1.1   pooka static int
    168   1.1   pooka shmif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    169   1.1   pooka {
    170   1.1   pooka 	int s, rv;
    171   1.1   pooka 
    172   1.1   pooka 	s = splnet();
    173   1.1   pooka 	rv = ether_ioctl(ifp, cmd, data);
    174   1.7   pooka 	if (rv == ENETRESET)
    175   1.7   pooka 		rv = 0;
    176   1.1   pooka 	splx(s);
    177   1.1   pooka 
    178   1.1   pooka 	return rv;
    179   1.1   pooka }
    180   1.1   pooka 
    181   1.1   pooka /* send everything in-context */
    182   1.1   pooka static void
    183   1.1   pooka shmif_start(struct ifnet *ifp)
    184   1.1   pooka {
    185   1.1   pooka 	struct shmif_sc *sc = ifp->if_softc;
    186   1.1   pooka 	struct mbuf *m, *m0;
    187   1.1   pooka 	uint32_t lastoff, dataoff, npktlenoff;
    188   1.1   pooka 	uint32_t pktsize = 0;
    189   1.1   pooka 	bool wrote = false;
    190  1.19   pooka 	bool wrap = false;
    191   1.1   pooka 	int error;
    192   1.1   pooka 
    193   1.1   pooka 	for (;;) {
    194   1.1   pooka 		IF_DEQUEUE(&ifp->if_snd, m0);
    195   1.1   pooka 		if (m0 == NULL) {
    196   1.1   pooka 			break;
    197   1.1   pooka 		}
    198   1.1   pooka 
    199  1.19   pooka 		for (m = m0; m != NULL; m = m->m_next) {
    200  1.19   pooka 			pktsize += m->m_len;
    201  1.19   pooka 		}
    202  1.19   pooka 
    203  1.19   pooka 		shmif_lockbus(sc->sc_busmem);
    204  1.16   pooka 		lastoff = sc->sc_busmem->shm_last;
    205  1.19   pooka 		npktlenoff = shmif_nextpktoff(sc->sc_busmem, lastoff);
    206   1.1   pooka 
    207  1.19   pooka 		dataoff = shmif_buswrite(sc->sc_busmem,
    208  1.19   pooka 		    npktlenoff, &pktsize, PKTLEN_SIZE, &wrap);
    209   1.1   pooka 		for (m = m0; m != NULL; m = m->m_next) {
    210  1.19   pooka 			dataoff = shmif_buswrite(sc->sc_busmem, dataoff,
    211  1.19   pooka 			    mtod(m, void *), m->m_len, &wrap);
    212   1.1   pooka 		}
    213  1.19   pooka 
    214  1.19   pooka 		if (wrap)
    215  1.19   pooka 			sc->sc_busmem->shm_gen++;
    216  1.16   pooka 		sc->sc_busmem->shm_last = npktlenoff;
    217  1.19   pooka 		shmif_unlockbus(sc->sc_busmem);
    218   1.1   pooka 
    219   1.1   pooka 		m_freem(m0);
    220   1.1   pooka 		wrote = true;
    221   1.1   pooka 
    222   1.1   pooka 		DPRINTF(("shmif_start: send %d bytes at off %d\n",
    223   1.1   pooka 		    pktsize, npktlenoff));
    224   1.1   pooka 	}
    225   1.1   pooka 	/* wakeup */
    226   1.1   pooka 	if (wrote)
    227  1.15   pooka 		rumpuser_pwrite(sc->sc_memfd,
    228  1.15   pooka 		    &busversion, sizeof(busversion), IFMEM_WAKEUP, &error);
    229   1.1   pooka }
    230   1.1   pooka 
    231   1.1   pooka static void
    232   1.1   pooka shmif_stop(struct ifnet *ifp, int disable)
    233   1.1   pooka {
    234   1.1   pooka 
    235   1.1   pooka 	panic("%s: unimpl", __func__);
    236   1.1   pooka }
    237   1.1   pooka 
    238   1.1   pooka static void
    239   1.1   pooka shmif_rcv(void *arg)
    240   1.1   pooka {
    241   1.1   pooka 	struct ifnet *ifp = arg;
    242   1.1   pooka 	struct shmif_sc *sc = ifp->if_softc;
    243   1.1   pooka 	struct mbuf *m = NULL;
    244   1.1   pooka 	struct ether_header *eth;
    245   1.1   pooka 	uint32_t nextpkt, pktlen, lastpkt, busgen, lastnext;
    246  1.19   pooka 	bool wrap = false;
    247   1.1   pooka 	int error;
    248   1.1   pooka 
    249   1.1   pooka 	for (;;) {
    250   1.1   pooka 		if (m == NULL) {
    251   1.1   pooka 			m = m_gethdr(M_WAIT, MT_DATA);
    252   1.1   pooka 			MCLGET(m, M_WAIT);
    253   1.1   pooka 		}
    254   1.1   pooka 
    255   1.1   pooka 		DPRINTF(("waiting %d/%d\n", sc->sc_nextpacket, sc->sc_prevgen));
    256   1.1   pooka 
    257   1.1   pooka 		KASSERT(m->m_flags & M_EXT);
    258  1.19   pooka 		shmif_lockbus(sc->sc_busmem);
    259  1.16   pooka 		lastpkt = sc->sc_busmem->shm_last;
    260  1.16   pooka 		busgen = sc->sc_busmem->shm_gen;
    261  1.19   pooka 		lastnext = shmif_nextpktoff(sc->sc_busmem, lastpkt);
    262   1.1   pooka 		if ((lastnext > sc->sc_nextpacket && busgen > sc->sc_prevgen)
    263   1.1   pooka 		    || (busgen > sc->sc_prevgen+1)) {
    264   1.1   pooka 			nextpkt = lastpkt;
    265   1.1   pooka 			sc->sc_prevgen = busgen;
    266  1.11   pooka 			rumpuser_dprintf("shmif_rcv: generation overrun, "
    267  1.11   pooka 			    "skipping invalid packets\n");
    268   1.1   pooka 		} else {
    269   1.1   pooka 			nextpkt = sc->sc_nextpacket;
    270   1.1   pooka 		}
    271   1.1   pooka 
    272   1.1   pooka 		/* need more data? */
    273   1.1   pooka 		if (lastnext == nextpkt && sc->sc_prevgen == busgen){
    274  1.19   pooka 			shmif_unlockbus(sc->sc_busmem);
    275   1.1   pooka 			error = 0;
    276   1.1   pooka 			rumpuser_writewatchfile_wait(sc->sc_kq, NULL, &error);
    277   1.1   pooka 			if (__predict_false(error))
    278   1.1   pooka 				printf("shmif_rcv: wait failed %d\n", error);
    279   1.1   pooka 			continue;
    280   1.1   pooka 		}
    281   1.1   pooka 
    282  1.19   pooka 		shmif_busread(sc->sc_busmem,
    283  1.19   pooka 		    &pktlen, nextpkt, PKTLEN_SIZE, &wrap);
    284  1.19   pooka 		shmif_busread(sc->sc_busmem, mtod(m, void *),
    285  1.19   pooka 		    shmif_advance(nextpkt, PKTLEN_SIZE), pktlen, &wrap);
    286  1.19   pooka 		if (wrap)
    287  1.19   pooka 			sc->sc_prevgen = sc->sc_busmem->shm_gen;
    288   1.1   pooka 
    289   1.1   pooka 		DPRINTF(("shmif_rcv: read packet of length %d at %d\n",
    290   1.1   pooka 		    pktlen, nextpkt));
    291   1.1   pooka 
    292  1.19   pooka 		sc->sc_nextpacket = shmif_nextpktoff(sc->sc_busmem, nextpkt);
    293   1.1   pooka 		sc->sc_prevgen = busgen;
    294  1.19   pooka 		shmif_unlockbus(sc->sc_busmem);
    295   1.1   pooka 
    296   1.1   pooka 		m->m_len = m->m_pkthdr.len = pktlen;
    297   1.1   pooka 		m->m_pkthdr.rcvif = ifp;
    298   1.1   pooka 
    299  1.10   pooka 		/* if it's from us, don't pass up and reuse storage space */
    300   1.1   pooka 		eth = mtod(m, struct ether_header *);
    301   1.1   pooka 		if (memcmp(eth->ether_shost, sc->sc_myaddr, 6) != 0) {
    302   1.1   pooka 			ifp->if_input(ifp, m);
    303   1.1   pooka 			m = NULL;
    304   1.1   pooka 		}
    305   1.1   pooka 	}
    306   1.1   pooka 
    307   1.1   pooka 	panic("shmif_worker is a lazy boy %d\n", error);
    308   1.1   pooka }
    309