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