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