From b97bdf9424b2294830474c7c725d53d2db32c1b0 Mon Sep 17 00:00:00 2001 From: pooka Date: Fri, 13 Aug 2010 10:13:44 +0000 Subject: [PATCH] Include a timestamp in the frame header. When converting to pcap, it can give some idea of when packets were sent. nb. it's the sending host's timestamp, not an observer timestamp like in the typical pcap case. --- sys/rump/net/lib/libshmif/dumpbus.c | 27 ++++++++++++------------ sys/rump/net/lib/libshmif/if_shmem.c | 27 +++++++++++++++++------- sys/rump/net/lib/libshmif/shmif_busops.c | 14 ++++++------ sys/rump/net/lib/libshmif/shmifvar.h | 12 ++++++++--- 4 files changed, 48 insertions(+), 32 deletions(-) diff --git a/sys/rump/net/lib/libshmif/dumpbus.c b/sys/rump/net/lib/libshmif/dumpbus.c index a130178d9674..cd4aec7f0e0d 100644 --- a/sys/rump/net/lib/libshmif/dumpbus.c +++ b/sys/rump/net/lib/libshmif/dumpbus.c @@ -1,4 +1,4 @@ -/* $NetBSD: dumpbus.c,v 1.6 2010/08/12 21:41:47 pooka Exp $ */ +/* $NetBSD: dumpbus.c,v 1.7 2010/08/13 10:13:44 pooka Exp $ */ /* * Little utility to convert shmif bus traffic to a pcap file @@ -40,7 +40,6 @@ main(int argc, char *argv[]) uint32_t curbus, buslast; struct shmif_mem *bmem; int fd, pfd, i, ch; - uint32_t pktlen; int bonus; char *buf; @@ -117,42 +116,42 @@ main(int argc, char *argv[]) if (buslast < curbus) bonus = 1; - assert(sizeof(pktlen) == PKTLEN_SIZE); - i = 0; while (curbus <= buslast || bonus) { struct pcap_pkthdr packhdr; + struct shmif_pkthdr sp; uint32_t oldoff; bool wrap; wrap = false; oldoff = curbus; - curbus = shmif_busread(bmem, - &pktlen, oldoff, PKTLEN_SIZE, &wrap); + curbus = shmif_busread(bmem, &sp, oldoff, sizeof(sp), &wrap); if (wrap) bonus = 0; - if (pktlen == 0) + if (sp.sp_len == 0) continue; - printf("packet %d, offset 0x%04x, length 0x%04x\n", - i++, curbus, pktlen); + printf("packet %d, offset 0x%04x, length 0x%04x, ts %d/%06d\n", + i++, curbus, sp.sp_len, sp.sp_sec, sp.sp_usec); - if (!pcapfile || pktlen == 0) { + if (!pcapfile || sp.sp_len == 0) { curbus = shmif_busread(bmem, - buf, curbus, pktlen, &wrap); + buf, curbus, sp.sp_len, &wrap); if (wrap) bonus = 0; continue; } memset(&packhdr, 0, sizeof(packhdr)); - packhdr.caplen = packhdr.len = pktlen; + packhdr.caplen = packhdr.len = sp.sp_len; + packhdr.ts.tv_sec = sp.sp_sec; + packhdr.ts.tv_usec = sp.sp_usec; if (write(pfd, &packhdr, sizeof(packhdr)) != sizeof(packhdr)) err(1, "error writing packethdr"); - curbus = shmif_busread(bmem, buf, curbus, pktlen, &wrap); - if (write(pfd, buf, pktlen) != pktlen) + curbus = shmif_busread(bmem, buf, curbus, sp.sp_len, &wrap); + if (write(pfd, buf, sp.sp_len) != sp.sp_len) err(1, "write packet"); if (wrap) bonus = 0; diff --git a/sys/rump/net/lib/libshmif/if_shmem.c b/sys/rump/net/lib/libshmif/if_shmem.c index f8ddb0526264..da9c5304f4a8 100644 --- a/sys/rump/net/lib/libshmif/if_shmem.c +++ b/sys/rump/net/lib/libshmif/if_shmem.c @@ -1,4 +1,4 @@ -/* $NetBSD: if_shmem.c,v 1.19 2010/08/12 21:41:47 pooka Exp $ */ +/* $NetBSD: if_shmem.c,v 1.20 2010/08/13 10:13:44 pooka Exp $ */ /* * Copyright (c) 2009 Antti Kantee. All Rights Reserved. @@ -28,7 +28,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: if_shmem.c,v 1.19 2010/08/12 21:41:47 pooka Exp $"); +__KERNEL_RCSID(0, "$NetBSD: if_shmem.c,v 1.20 2010/08/13 10:13:44 pooka Exp $"); #include #include @@ -191,6 +191,9 @@ shmif_start(struct ifnet *ifp) int error; for (;;) { + struct shmif_pkthdr sp; + struct timeval tv; + IF_DEQUEUE(&ifp->if_snd, m0); if (m0 == NULL) { break; @@ -204,8 +207,14 @@ shmif_start(struct ifnet *ifp) lastoff = sc->sc_busmem->shm_last; npktlenoff = shmif_nextpktoff(sc->sc_busmem, lastoff); + getmicrouptime(&tv); + + sp.sp_len = pktsize; + sp.sp_sec = tv.tv_sec; + sp.sp_usec = tv.tv_usec; + dataoff = shmif_buswrite(sc->sc_busmem, - npktlenoff, &pktsize, PKTLEN_SIZE, &wrap); + npktlenoff, &sp, sizeof(sp), &wrap); for (m = m0; m != NULL; m = m->m_next) { dataoff = shmif_buswrite(sc->sc_busmem, dataoff, mtod(m, void *), m->m_len, &wrap); @@ -242,11 +251,13 @@ shmif_rcv(void *arg) struct shmif_sc *sc = ifp->if_softc; struct mbuf *m = NULL; struct ether_header *eth; - uint32_t nextpkt, pktlen, lastpkt, busgen, lastnext; + uint32_t nextpkt, lastpkt, busgen, lastnext; bool wrap = false; int error; for (;;) { + struct shmif_pkthdr sp; + if (m == NULL) { m = m_gethdr(M_WAIT, MT_DATA); MCLGET(m, M_WAIT); @@ -280,20 +291,20 @@ shmif_rcv(void *arg) } shmif_busread(sc->sc_busmem, - &pktlen, nextpkt, PKTLEN_SIZE, &wrap); + &sp, nextpkt, sizeof(sp), &wrap); shmif_busread(sc->sc_busmem, mtod(m, void *), - shmif_advance(nextpkt, PKTLEN_SIZE), pktlen, &wrap); + shmif_advance(nextpkt, sizeof(sp)), sp.sp_len, &wrap); if (wrap) sc->sc_prevgen = sc->sc_busmem->shm_gen; DPRINTF(("shmif_rcv: read packet of length %d at %d\n", - pktlen, nextpkt)); + sp.sp_len, nextpkt)); sc->sc_nextpacket = shmif_nextpktoff(sc->sc_busmem, nextpkt); sc->sc_prevgen = busgen; shmif_unlockbus(sc->sc_busmem); - m->m_len = m->m_pkthdr.len = pktlen; + m->m_len = m->m_pkthdr.len = sp.sp_len; m->m_pkthdr.rcvif = ifp; /* if it's from us, don't pass up and reuse storage space */ diff --git a/sys/rump/net/lib/libshmif/shmif_busops.c b/sys/rump/net/lib/libshmif/shmif_busops.c index 54d27ba93b4e..1bcb440ec4af 100644 --- a/sys/rump/net/lib/libshmif/shmif_busops.c +++ b/sys/rump/net/lib/libshmif/shmif_busops.c @@ -1,4 +1,4 @@ -/* $NetBSD: shmif_busops.c,v 1.1 2010/08/12 21:41:47 pooka Exp $ */ +/* $NetBSD: shmif_busops.c,v 1.2 2010/08/13 10:13:44 pooka Exp $ */ /* * Copyright (c) 2009 Antti Kantee. All Rights Reserved. @@ -28,7 +28,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: shmif_busops.c,v 1.1 2010/08/12 21:41:47 pooka Exp $"); +__KERNEL_RCSID(0, "$NetBSD: shmif_busops.c,v 1.2 2010/08/13 10:13:44 pooka Exp $"); #include #include @@ -109,7 +109,7 @@ void shmif_advancefirst(struct shmif_mem *busmem, uint32_t off, size_t len) { - while (off <= busmem->shm_first + PKTLEN_SIZE + while (off <= busmem->shm_first + sizeof(struct shmif_pkthdr) && off+len > busmem->shm_first) { DPRINTF(("advancefirst: old offset %d, ", busmem->shm_first)); busmem->shm_first = shmif_nextpktoff(busmem, busmem->shm_first); @@ -155,11 +155,11 @@ shmif_buswrite(struct shmif_mem *busmem, uint32_t off, void *data, size_t len, uint32_t shmif_nextpktoff(struct shmif_mem *busmem, uint32_t oldoff) { - uint32_t oldlen; + struct shmif_pkthdr sp; bool dummy; - shmif_busread(busmem, &oldlen, oldoff, PKTLEN_SIZE, &dummy); - KASSERT(oldlen < BUSMEM_DATASIZE); + shmif_busread(busmem, &sp, oldoff, sizeof(sp), &dummy); + KASSERT(sp.sp_len < BUSMEM_DATASIZE); - return shmif_advance(oldoff, PKTLEN_SIZE + oldlen); + return shmif_advance(oldoff, sizeof(sp) + sp.sp_len); } diff --git a/sys/rump/net/lib/libshmif/shmifvar.h b/sys/rump/net/lib/libshmif/shmifvar.h index 8e9c52cf1e96..d78ade8a7347 100644 --- a/sys/rump/net/lib/libshmif/shmifvar.h +++ b/sys/rump/net/lib/libshmif/shmifvar.h @@ -1,4 +1,4 @@ -/* $NetBSD: shmifvar.h,v 1.3 2010/08/12 21:41:47 pooka Exp $ */ +/* $NetBSD: shmifvar.h,v 1.4 2010/08/13 10:13:44 pooka Exp $ */ /*- * Copyright (c) 2009 Antti Kantee. All Rights Reserved. @@ -31,7 +31,7 @@ #define _RUMP_NET_SHMIFVAR_H_ #define SHMIF_MAGIC 0xca11d054 -#define SHMIF_VERSION 1 +#define SHMIF_VERSION 2 struct shmif_mem { uint32_t shm_magic; @@ -50,7 +50,13 @@ struct shmif_mem { #define IFMEM_DATA (offsetof(struct shmif_mem, shm_data)) #define IFMEM_WAKEUP (offsetof(struct shmif_mem, shm_version)) -#define PKTLEN_SIZE (sizeof(uint32_t)) + +struct shmif_pkthdr { + uint32_t sp_len; + + uint32_t sp_sec; + uint32_t sp_usec; +}; #define BUSMEM_SIZE (1024*1024) #define BUSMEM_DATASIZE (BUSMEM_SIZE - sizeof(struct shmif_mem))