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