Include a pointer to the beginning of the buffer and add support

to the packet dumper.  This helps in situations where the juicy
details are in a bus multiple generations old.
This commit is contained in:
pooka 2010-08-12 21:41:47 +00:00
parent 15b01eef79
commit ce68b7ae71
5 changed files with 275 additions and 156 deletions

View File

@ -1,9 +1,9 @@
# $NetBSD: Makefile,v 1.1 2009/02/28 15:28:46 pooka Exp $
# $NetBSD: Makefile,v 1.2 2010/08/12 21:41:47 pooka Exp $
#
LIB= rumpnet_shmif
SRCS= if_shmem.c
SRCS= if_shmem.c shmif_busops.c
CPPFLAGS+= -I${.CURDIR}/../../../librump/rumpkern

View File

@ -1,4 +1,4 @@
/* $NetBSD: dumpbus.c,v 1.5 2010/08/12 18:22:40 pooka Exp $ */
/* $NetBSD: dumpbus.c,v 1.6 2010/08/12 21:41:47 pooka Exp $ */
/*
* Little utility to convert shmif bus traffic to a pcap file
@ -13,12 +13,14 @@
#include <err.h>
#include <fcntl.h>
#include <pcap.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "shmifvar.h"
#include "shmif_busops.c"
static void
usage(void)
@ -28,16 +30,19 @@ usage(void)
exit(1);
}
#define BUFSIZE 64*1024
int
main(int argc, char *argv[])
{
struct stat sb;
void *busmem;
const char *pcapfile = NULL;
uint8_t *curbus, *buslast;
uint32_t curbus, buslast;
struct shmif_mem *bmem;
int fd, pfd, i, ch;
uint32_t pktlen;
int bonus;
char *buf;
while ((ch = getopt(argc, argv, "p:")) != -1) {
switch (ch) {
@ -55,6 +60,10 @@ main(int argc, char *argv[])
if (argc != 1)
usage();
buf = malloc(BUFSIZE);
if (buf == NULL)
err(1, "malloc");
fd = open(argv[0], O_RDONLY);
if (fd == -1)
err(1, "open bus");
@ -72,13 +81,10 @@ main(int argc, char *argv[])
if (bmem->shm_version != SHMIF_VERSION)
errx(1, "bus vesrsion %d, program %d",
bmem->shm_version, SHMIF_VERSION);
printf("bus version %d, lock: %d, generation: %d, lastoff: 0x%x\n",
bmem->shm_version, bmem->shm_lock, bmem->shm_gen, bmem->shm_last);
if (bmem->shm_gen != 0) {
printf("this dumper can manage only generation 0, sorry\n");
exit(0);
}
printf("bus version %d, lock: %d, generation: %" PRIu64
", firstoff: 0x%04x, lastoff: 0x%04x\n",
bmem->shm_version, bmem->shm_lock, bmem->shm_gen,
bmem->shm_first, bmem->shm_last);
if (pcapfile) {
struct pcap_file_header phdr;
@ -102,25 +108,41 @@ main(int argc, char *argv[])
err(1, "phdr write");
}
curbus = bmem->shm_data;
buslast = bmem->shm_data + bmem->shm_last;
curbus = bmem->shm_first;
buslast = bmem->shm_last;
if (curbus == BUSMEM_DATASIZE)
curbus = 0;
bonus = 0;
if (buslast < curbus)
bonus = 1;
assert(sizeof(pktlen) == PKTLEN_SIZE);
i = 0;
while (curbus <= buslast) {
while (curbus <= buslast || bonus) {
struct pcap_pkthdr packhdr;
uint32_t oldoff;
bool wrap;
pktlen = *(uint32_t *)curbus;
curbus += sizeof(pktlen);
wrap = false;
oldoff = curbus;
curbus = shmif_busread(bmem,
&pktlen, oldoff, PKTLEN_SIZE, &wrap);
if (wrap)
bonus = 0;
if (pktlen == 0)
continue;
printf("packet %d, offset 0x%x, length 0x%x\n",
i++, curbus - bmem->shm_data, pktlen);
printf("packet %d, offset 0x%04x, length 0x%04x\n",
i++, curbus, pktlen);
if (!pcapfile || pktlen == 0) {
curbus += pktlen;
curbus = shmif_busread(bmem,
buf, curbus, pktlen, &wrap);
if (wrap)
bonus = 0;
continue;
}
@ -129,9 +151,11 @@ main(int argc, char *argv[])
if (write(pfd, &packhdr, sizeof(packhdr)) != sizeof(packhdr))
err(1, "error writing packethdr");
if (write(pfd, curbus, pktlen) != pktlen)
curbus = shmif_busread(bmem, buf, curbus, pktlen, &wrap);
if (write(pfd, buf, pktlen) != pktlen)
err(1, "write packet");
curbus += pktlen;
if (wrap)
bonus = 0;
}
return 0;

View File

@ -1,4 +1,4 @@
/* $NetBSD: if_shmem.c,v 1.18 2010/08/12 18:39:54 pooka Exp $ */
/* $NetBSD: if_shmem.c,v 1.19 2010/08/12 21:41:47 pooka Exp $ */
/*
* Copyright (c) 2009 Antti Kantee. All Rights Reserved.
@ -28,7 +28,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_shmem.c,v 1.18 2010/08/12 18:39:54 pooka Exp $");
__KERNEL_RCSID(0, "$NetBSD: if_shmem.c,v 1.19 2010/08/12 21:41:47 pooka Exp $");
#include <sys/param.h>
#include <sys/atomic.h>
@ -50,12 +50,6 @@ __KERNEL_RCSID(0, "$NetBSD: if_shmem.c,v 1.18 2010/08/12 18:39:54 pooka Exp $");
#include "rump_private.h"
#include "rump_net_private.h"
#if 0
#define DPRINTF(x) rumpuser_dprintf x
#else
#define DPRINTF(x)
#endif
/*
* A virtual ethernet interface which uses shared memory from a
* memory mapped file as the bus.
@ -79,115 +73,12 @@ struct shmif_sc {
uint32_t sc_prevgen;
};
#define BUSMEM_SIZE (1024*1024)
#define BUSMEM_DATASIZE (BUSMEM_SIZE - sizeof(struct shmif_mem))
static const uint32_t busversion = SHMIF_VERSION;
static void shmif_rcv(void *);
static uint32_t numif;
#define LOCK_UNLOCKED 0
#define LOCK_LOCKED 1
/*
* This locking needs work and will misbehave severely if:
* 1) the backing memory has to be paged in
* 2) some lockholder exits while holding the lock
*/
static void
lockbus(struct shmif_sc *sc)
{
while (atomic_cas_32(&sc->sc_busmem->shm_lock,
LOCK_UNLOCKED, LOCK_LOCKED) == LOCK_LOCKED)
continue;
membar_enter();
}
static void
unlockbus(struct shmif_sc *sc)
{
unsigned int old;
membar_exit();
old = atomic_swap_32(&sc->sc_busmem->shm_lock, LOCK_UNLOCKED);
KASSERT(old == LOCK_LOCKED);
}
static uint32_t
busread(struct shmif_sc *sc, void *dest, uint32_t off, size_t len)
{
size_t chunk;
KASSERT(len < (BUSMEM_DATASIZE) && off <= BUSMEM_DATASIZE);
chunk = MIN(len, BUSMEM_DATASIZE - off);
memcpy(dest, sc->sc_busmem->shm_data + off, chunk);
len -= chunk;
if (len == 0)
return off + chunk;
/* else, wraps around */
off = 0;
sc->sc_prevgen = sc->sc_busmem->shm_gen;
/* finish reading */
memcpy((uint8_t *)dest + chunk, sc->sc_busmem->shm_data + off, len);
return off + len;
}
static uint32_t
buswrite(struct shmif_sc *sc, uint32_t off, void *data, size_t len)
{
size_t chunk;
KASSERT(len < (BUSMEM_DATASIZE) && off <= BUSMEM_DATASIZE);
chunk = MIN(len, BUSMEM_DATASIZE - off);
memcpy(sc->sc_busmem->shm_data + off, data, chunk);
len -= chunk;
if (len == 0)
return off + chunk;
DPRINTF(("buswrite wrap: wrote %d bytes to %d, left %d to 0",
chunk, off, len));
/* else, wraps around */
off = 0;
sc->sc_prevgen = sc->sc_busmem->shm_gen;
sc->sc_busmem->shm_gen++;
/* finish writing */
memcpy(sc->sc_busmem->shm_data + off, (uint8_t *)data + chunk, len);
return off + len;
}
static inline uint32_t
advance(uint32_t oldoff, uint32_t delta)
{
uint32_t newoff;
newoff = oldoff + delta;
if (newoff >= BUSMEM_DATASIZE)
newoff -= (BUSMEM_DATASIZE);
return newoff;
}
static uint32_t
nextpktoff(struct shmif_sc *sc, uint32_t oldoff)
{
uint32_t oldlen;
busread(sc, &oldlen, oldoff, PKTLEN_SIZE);
KASSERT(oldlen < BUSMEM_DATASIZE);
return advance(oldoff, PKTLEN_SIZE + oldlen);
}
int
rump_shmif_create(const char *path, int *ifnum)
{
@ -217,12 +108,17 @@ rump_shmif_create(const char *path, int *ifnum)
if (sc->sc_busmem->shm_magic && sc->sc_busmem->shm_magic != SHMIF_MAGIC)
panic("bus is not magical");
sc->sc_busmem->shm_magic = SHMIF_MAGIC;
lockbus(sc);
shmif_lockbus(sc->sc_busmem);
/* we're first? initialize bus */
if (sc->sc_busmem->shm_magic == 0) {
sc->sc_busmem->shm_magic = SHMIF_MAGIC;
sc->sc_busmem->shm_first = BUSMEM_DATASIZE;
}
sc->sc_nextpacket = sc->sc_busmem->shm_last;
sc->sc_prevgen = sc->sc_busmem->shm_gen;
unlockbus(sc);
shmif_unlockbus(sc->sc_busmem);
sc->sc_kq = rumpuser_writewatchfile_setup(-1, sc->sc_memfd, 0, &error);
if (sc->sc_kq == -1)
@ -291,6 +187,7 @@ shmif_start(struct ifnet *ifp)
uint32_t lastoff, dataoff, npktlenoff;
uint32_t pktsize = 0;
bool wrote = false;
bool wrap = false;
int error;
for (;;) {
@ -299,20 +196,25 @@ shmif_start(struct ifnet *ifp)
break;
}
lockbus(sc);
lastoff = sc->sc_busmem->shm_last;
npktlenoff = nextpktoff(sc, lastoff);
dataoff = advance(npktlenoff, PKTLEN_SIZE);
for (m = m0; m != NULL; m = m->m_next) {
pktsize += m->m_len;
dataoff = buswrite(sc, dataoff, mtod(m, void *),
m->m_len);
}
buswrite(sc, npktlenoff, &pktsize, PKTLEN_SIZE);
shmif_lockbus(sc->sc_busmem);
lastoff = sc->sc_busmem->shm_last;
npktlenoff = shmif_nextpktoff(sc->sc_busmem, lastoff);
dataoff = shmif_buswrite(sc->sc_busmem,
npktlenoff, &pktsize, PKTLEN_SIZE, &wrap);
for (m = m0; m != NULL; m = m->m_next) {
dataoff = shmif_buswrite(sc->sc_busmem, dataoff,
mtod(m, void *), m->m_len, &wrap);
}
if (wrap)
sc->sc_busmem->shm_gen++;
sc->sc_busmem->shm_last = npktlenoff;
unlockbus(sc);
shmif_unlockbus(sc->sc_busmem);
m_freem(m0);
wrote = true;
@ -341,6 +243,7 @@ shmif_rcv(void *arg)
struct mbuf *m = NULL;
struct ether_header *eth;
uint32_t nextpkt, pktlen, lastpkt, busgen, lastnext;
bool wrap = false;
int error;
for (;;) {
@ -352,10 +255,10 @@ shmif_rcv(void *arg)
DPRINTF(("waiting %d/%d\n", sc->sc_nextpacket, sc->sc_prevgen));
KASSERT(m->m_flags & M_EXT);
lockbus(sc);
shmif_lockbus(sc->sc_busmem);
lastpkt = sc->sc_busmem->shm_last;
busgen = sc->sc_busmem->shm_gen;
lastnext = nextpktoff(sc, lastpkt);
lastnext = shmif_nextpktoff(sc->sc_busmem, lastpkt);
if ((lastnext > sc->sc_nextpacket && busgen > sc->sc_prevgen)
|| (busgen > sc->sc_prevgen+1)) {
nextpkt = lastpkt;
@ -368,7 +271,7 @@ shmif_rcv(void *arg)
/* need more data? */
if (lastnext == nextpkt && sc->sc_prevgen == busgen){
unlockbus(sc);
shmif_unlockbus(sc->sc_busmem);
error = 0;
rumpuser_writewatchfile_wait(sc->sc_kq, NULL, &error);
if (__predict_false(error))
@ -376,16 +279,19 @@ shmif_rcv(void *arg)
continue;
}
busread(sc, &pktlen, nextpkt, PKTLEN_SIZE);
busread(sc, mtod(m, void *),
advance(nextpkt, PKTLEN_SIZE), pktlen);
shmif_busread(sc->sc_busmem,
&pktlen, nextpkt, PKTLEN_SIZE, &wrap);
shmif_busread(sc->sc_busmem, mtod(m, void *),
shmif_advance(nextpkt, PKTLEN_SIZE), pktlen, &wrap);
if (wrap)
sc->sc_prevgen = sc->sc_busmem->shm_gen;
DPRINTF(("shmif_rcv: read packet of length %d at %d\n",
pktlen, nextpkt));
sc->sc_nextpacket = nextpktoff(sc, nextpkt);
sc->sc_nextpacket = shmif_nextpktoff(sc->sc_busmem, nextpkt);
sc->sc_prevgen = busgen;
unlockbus(sc);
shmif_unlockbus(sc->sc_busmem);
m->m_len = m->m_pkthdr.len = pktlen;
m->m_pkthdr.rcvif = ifp;

View File

@ -0,0 +1,165 @@
/* $NetBSD: shmif_busops.c,v 1.1 2010/08/12 21:41:47 pooka Exp $ */
/*
* Copyright (c) 2009 Antti Kantee. All Rights Reserved.
*
* Development of this software was supported by The Nokia Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: shmif_busops.c,v 1.1 2010/08/12 21:41:47 pooka Exp $");
#include <sys/param.h>
#include <sys/atomic.h>
#include "shmifvar.h"
#ifndef _KERNEL
#include <assert.h>
#define KASSERT(a) assert(a)
#else
#include <rump/rumpuser.h>
#endif
#define LOCK_UNLOCKED 0
#define LOCK_LOCKED 1
/*
* This locking needs work and will misbehave severely if:
* 1) the backing memory has to be paged in
* 2) some lockholder exits while holding the lock
*/
void
shmif_lockbus(struct shmif_mem *busmem)
{
while (atomic_cas_32(&busmem->shm_lock,
LOCK_UNLOCKED, LOCK_LOCKED) == LOCK_LOCKED)
continue;
membar_enter();
}
void
shmif_unlockbus(struct shmif_mem *busmem)
{
unsigned int old;
membar_exit();
old = atomic_swap_32(&busmem->shm_lock, LOCK_UNLOCKED);
KASSERT(old == LOCK_LOCKED);
}
uint32_t
shmif_advance(uint32_t oldoff, uint32_t delta)
{
uint32_t newoff;
newoff = oldoff + delta;
if (newoff >= BUSMEM_DATASIZE)
newoff -= (BUSMEM_DATASIZE);
return newoff;
}
uint32_t
shmif_busread(struct shmif_mem *busmem, void *dest, uint32_t off, size_t len,
bool *wrap)
{
size_t chunk;
KASSERT(len < (BUSMEM_DATASIZE) && off <= BUSMEM_DATASIZE);
chunk = MIN(len, BUSMEM_DATASIZE - off);
memcpy(dest, busmem->shm_data + off, chunk);
len -= chunk;
if (len == 0)
return off + chunk;
/* else, wraps around */
off = 0;
*wrap = true;
/* finish reading */
memcpy((uint8_t *)dest + chunk, busmem->shm_data + off, len);
return off + len;
}
void
shmif_advancefirst(struct shmif_mem *busmem, uint32_t off, size_t len)
{
while (off <= busmem->shm_first + PKTLEN_SIZE
&& off+len > busmem->shm_first) {
DPRINTF(("advancefirst: old offset %d, ", busmem->shm_first));
busmem->shm_first = shmif_nextpktoff(busmem, busmem->shm_first);
DPRINTF(("new offset: %d\n", busmem->shm_first));
}
}
uint32_t
shmif_buswrite(struct shmif_mem *busmem, uint32_t off, void *data, size_t len,
bool *wrap)
{
size_t chunk;
KASSERT(len < (BUSMEM_DATASIZE) && off <= BUSMEM_DATASIZE);
chunk = MIN(len, BUSMEM_DATASIZE - off);
len -= chunk;
shmif_advancefirst(busmem, off, chunk + (len ? 1 : 0));
memcpy(busmem->shm_data + off, data, chunk);
DPRINTF(("buswrite: wrote %d bytes to %d", chunk, off));
if (len == 0) {
DPRINTF(("\n"));
return off + chunk;
}
DPRINTF((", wrapped bytes %d to 0\n", len));
/* else, wraps around */
off = 0;
*wrap = true;
shmif_advancefirst(busmem, off, len);
/* finish writing */
memcpy(busmem->shm_data + off, (uint8_t *)data + chunk, len);
return off + len;
}
uint32_t
shmif_nextpktoff(struct shmif_mem *busmem, uint32_t oldoff)
{
uint32_t oldlen;
bool dummy;
shmif_busread(busmem, &oldlen, oldoff, PKTLEN_SIZE, &dummy);
KASSERT(oldlen < BUSMEM_DATASIZE);
return shmif_advance(oldoff, PKTLEN_SIZE + oldlen);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: shmifvar.h,v 1.2 2010/08/12 18:17:23 pooka Exp $ */
/* $NetBSD: shmifvar.h,v 1.3 2010/08/12 21:41:47 pooka Exp $ */
/*-
* Copyright (c) 2009 Antti Kantee. All Rights Reserved.
@ -37,12 +37,13 @@ struct shmif_mem {
uint32_t shm_magic;
uint32_t shm_version;
uint32_t shm_gen;
uint64_t shm_gen;
uint32_t shm_first;
uint32_t shm_last;
uint32_t shm_lock;
uint32_t shm_spare[2];
uint32_t shm_spare[1];
uint8_t shm_data[0];
};
@ -51,4 +52,27 @@ struct shmif_mem {
#define IFMEM_WAKEUP (offsetof(struct shmif_mem, shm_version))
#define PKTLEN_SIZE (sizeof(uint32_t))
#define BUSMEM_SIZE (1024*1024)
#define BUSMEM_DATASIZE (BUSMEM_SIZE - sizeof(struct shmif_mem))
#if 0
#ifdef _KERNEL
#define DPRINTF(x) rumpuser_dprintf x
#else
#define DPRINTF(x) printf x
#endif
#else
#define DPRINTF(x)
#endif
void shmif_lockbus(struct shmif_mem *);
void shmif_unlockbus(struct shmif_mem *);
uint32_t shmif_advance(uint32_t, uint32_t);
uint32_t shmif_busread(struct shmif_mem *,
void *, uint32_t, size_t, bool *);
void shmif_advancefirst(struct shmif_mem *, uint32_t, size_t);
uint32_t shmif_buswrite(struct shmif_mem *, uint32_t,
void *, size_t, bool *);
uint32_t shmif_nextpktoff(struct shmif_mem *, uint32_t);
#endif