Home | History | Annotate | Line # | Download | only in libshmif
if_shmem.c revision 1.26
      1 /*	$NetBSD: if_shmem.c,v 1.26 2010/08/16 17:33:52 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.26 2010/08/16 17:33:52 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 /*
     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 	uint64_t sc_devgen;
     73 	uint32_t sc_nextpacket;
     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_devgen = 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 = ETHERMTU;
    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 shmif_mem *busmem = sc->sc_busmem;
    227 	struct mbuf *m, *m0;
    228 	uint32_t dataoff;
    229 	uint32_t pktsize, pktwrote;
    230 	bool wrote = false;
    231 	bool wrap;
    232 	int error;
    233 
    234 	ifp->if_flags |= IFF_OACTIVE;
    235 
    236 	for (;;) {
    237 		struct shmif_pkthdr sp;
    238 		struct timeval tv;
    239 
    240 		IF_DEQUEUE(&ifp->if_snd, m0);
    241 		if (m0 == NULL) {
    242 			break;
    243 		}
    244 
    245 		pktsize = 0;
    246 		for (m = m0; m != NULL; m = m->m_next) {
    247 			pktsize += m->m_len;
    248 		}
    249 		KASSERT(pktsize <= ETHERMTU + ETHER_HDR_LEN);
    250 
    251 		getmicrouptime(&tv);
    252 		sp.sp_len = pktsize;
    253 		sp.sp_sec = tv.tv_sec;
    254 		sp.sp_usec = tv.tv_usec;
    255 
    256 		shmif_lockbus(busmem);
    257 		KASSERT(busmem->shm_magic == SHMIF_MAGIC);
    258 		busmem->shm_last = shmif_nextpktoff(busmem, busmem->shm_last);
    259 
    260 		wrap = false;
    261 		dataoff = shmif_buswrite(busmem,
    262 		    busmem->shm_last, &sp, sizeof(sp), &wrap);
    263 		pktwrote = 0;
    264 		for (m = m0; m != NULL; m = m->m_next) {
    265 			pktwrote += m->m_len;
    266 			dataoff = shmif_buswrite(busmem, dataoff,
    267 			    mtod(m, void *), m->m_len, &wrap);
    268 		}
    269 		KASSERT(pktwrote == pktsize);
    270 
    271 		if (wrap)
    272 			busmem->shm_gen++;
    273 		shmif_unlockbus(busmem);
    274 
    275 		m_freem(m0);
    276 		wrote = true;
    277 
    278 		DPRINTF(("shmif_start: send %d bytes at off %d\n",
    279 		    pktsize, npktlenoff));
    280 	}
    281 
    282 	ifp->if_flags &= ~IFF_OACTIVE;
    283 
    284 	/* wakeup */
    285 	if (wrote)
    286 		rumpuser_pwrite(sc->sc_memfd,
    287 		    &busversion, sizeof(busversion), IFMEM_WAKEUP, &error);
    288 }
    289 
    290 static void
    291 shmif_stop(struct ifnet *ifp, int disable)
    292 {
    293 
    294 	panic("%s: unimpl", __func__);
    295 }
    296 
    297 static void
    298 shmif_rcv(void *arg)
    299 {
    300 	struct ifnet *ifp = arg;
    301 	struct shmif_sc *sc = ifp->if_softc;
    302 	struct shmif_mem *busmem = sc->sc_busmem;
    303 	struct mbuf *m = NULL;
    304 	struct ether_header *eth;
    305 	uint32_t nextpkt, busgen;
    306 	bool wrap;
    307 	int error;
    308 
    309 	for (;;) {
    310 		struct shmif_pkthdr sp;
    311 
    312 		if (m == NULL) {
    313 			m = m_gethdr(M_WAIT, MT_DATA);
    314 			MCLGET(m, M_WAIT);
    315 		}
    316 
    317 		DPRINTF(("waiting %d/%d\n", sc->sc_nextpacket, sc->sc_devgen));
    318 		KASSERT(m->m_flags & M_EXT);
    319 
    320 		shmif_lockbus(busmem);
    321 		busgen = busmem->shm_gen;
    322 		KASSERT(busmem->shm_magic == SHMIF_MAGIC);
    323 		KASSERT(busgen >= sc->sc_devgen);
    324 
    325 		/* need more data? */
    326 		if (sc->sc_devgen == busgen &&
    327 		    shmif_nextpktoff(busmem, busmem->shm_last)
    328 		     == sc->sc_nextpacket) {
    329 			shmif_unlockbus(busmem);
    330 			error = 0;
    331 			rumpuser_writewatchfile_wait(sc->sc_kq, NULL, &error);
    332 			if (__predict_false(error))
    333 				printf("shmif_rcv: wait failed %d\n", error);
    334 			continue;
    335 		}
    336 
    337 		/*
    338 		 * Check if we have been sleeping too long.  There are
    339 		 * basically two scenarios:
    340 		 *  1) our next packet is behind the first packet and
    341 		 *     we are a generation behind
    342 		 *  2) we are over two generations behind
    343 		 */
    344 		if ((sc->sc_nextpacket < busmem->shm_first
    345 		  && sc->sc_devgen < busgen) || (sc->sc_devgen+1 < busgen)) {
    346 			KASSERT(busgen > 0);
    347 			nextpkt = busmem->shm_first;
    348 			if (busmem->shm_first > busmem->shm_last)
    349 				sc->sc_devgen = busgen - 1;
    350 			else
    351 				sc->sc_devgen = busgen;
    352 		} else {
    353 			nextpkt = sc->sc_nextpacket;
    354 		}
    355 
    356 		/*
    357 		 * If our read pointer is ahead the bus last write, our
    358 		 * generation must be one behind.
    359 		 */
    360 		KASSERT(!(nextpkt > busmem->shm_last
    361 		    && sc->sc_devgen == busgen));
    362 
    363 		wrap = false;
    364 		nextpkt = shmif_busread(busmem, &sp,
    365 		    nextpkt, sizeof(sp), &wrap);
    366 		KASSERT(sp.sp_len <= ETHERMTU + ETHER_HDR_LEN);
    367 		nextpkt = shmif_busread(busmem, mtod(m, void *),
    368 		    nextpkt, sp.sp_len, &wrap);
    369 
    370 		DPRINTF(("shmif_rcv: read packet of length %d at %d\n",
    371 		    sp.sp_len, nextpkt));
    372 
    373 		sc->sc_nextpacket = nextpkt;
    374 		shmif_unlockbus(sc->sc_busmem);
    375 
    376 		if (wrap)
    377 			sc->sc_devgen++;
    378 
    379 		m->m_len = m->m_pkthdr.len = sp.sp_len;
    380 		m->m_pkthdr.rcvif = ifp;
    381 
    382 		/* if it's from us, don't pass up and reuse storage space */
    383 		eth = mtod(m, struct ether_header *);
    384 		if (memcmp(eth->ether_shost, sc->sc_myaddr, 6) != 0) {
    385 			KERNEL_LOCK(1, NULL);
    386 			ifp->if_input(ifp, m);
    387 			KERNEL_UNLOCK_ONE(NULL);
    388 			m = NULL;
    389 		}
    390 	}
    391 
    392 	panic("shmif_worker is a lazy boy %d\n", error);
    393 }
    394