Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/rump/net/lib/libshmif Include a pointer to the beginning...



details:   https://anonhg.NetBSD.org/src/rev/52360b33db56
branches:  trunk
changeset: 757067:52360b33db56
user:      pooka <pooka%NetBSD.org@localhost>
date:      Thu Aug 12 21:41:47 2010 +0000

description:
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.

diffstat:

 sys/rump/net/lib/libshmif/Makefile       |    4 +-
 sys/rump/net/lib/libshmif/dumpbus.c      |   62 +++++++---
 sys/rump/net/lib/libshmif/if_shmem.c     |  170 ++++++------------------------
 sys/rump/net/lib/libshmif/shmif_busops.c |  165 ++++++++++++++++++++++++++++++
 sys/rump/net/lib/libshmif/shmifvar.h     |   30 ++++-
 5 files changed, 275 insertions(+), 156 deletions(-)

diffs (truncated from 642 to 300 lines):

diff -r 3f29439120ff -r 52360b33db56 sys/rump/net/lib/libshmif/Makefile
--- a/sys/rump/net/lib/libshmif/Makefile        Thu Aug 12 20:16:27 2010 +0000
+++ b/sys/rump/net/lib/libshmif/Makefile        Thu Aug 12 21:41:47 2010 +0000
@@ -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
 
diff -r 3f29439120ff -r 52360b33db56 sys/rump/net/lib/libshmif/dumpbus.c
--- a/sys/rump/net/lib/libshmif/dumpbus.c       Thu Aug 12 20:16:27 2010 +0000
+++ b/sys/rump/net/lib/libshmif/dumpbus.c       Thu Aug 12 21:41:47 2010 +0000
@@ -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 @@
        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 @@
        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 @@
        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 @@
                        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 @@
 
                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;
diff -r 3f29439120ff -r 52360b33db56 sys/rump/net/lib/libshmif/if_shmem.c
--- a/sys/rump/net/lib/libshmif/if_shmem.c      Thu Aug 12 20:16:27 2010 +0000
+++ b/sys/rump/net/lib/libshmif/if_shmem.c      Thu Aug 12 21:41:47 2010 +0000
@@ -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 @@
 #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 @@
        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)
 {



Home | Main Index | Thread Index | Old Index