From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1LVVps-0000E0-V7 for qemu-devel@nongnu.org; Fri, 06 Feb 2009 13:53:57 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1LVVps-0000Dh-CG for qemu-devel@nongnu.org; Fri, 06 Feb 2009 13:53:56 -0500 Received: from [199.232.76.173] (port=39979 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LVVps-0000Db-8M for qemu-devel@nongnu.org; Fri, 06 Feb 2009 13:53:56 -0500 Received: from e39.co.us.ibm.com ([32.97.110.160]:60658) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1LVVpr-0003Jh-Nb for qemu-devel@nongnu.org; Fri, 06 Feb 2009 13:53:56 -0500 Received: from d03relay02.boulder.ibm.com (d03relay02.boulder.ibm.com [9.17.195.227]) by e39.co.us.ibm.com (8.13.1/8.13.1) with ESMTP id n16Ipac5028628 for ; Fri, 6 Feb 2009 11:51:36 -0700 Received: from d03av04.boulder.ibm.com (d03av04.boulder.ibm.com [9.17.195.170]) by d03relay02.boulder.ibm.com (8.13.8/8.13.8/NCO v9.1) with ESMTP id n16Irisf171970 for ; Fri, 6 Feb 2009 11:53:50 -0700 Received: from d03av04.boulder.ibm.com (loopback [127.0.0.1]) by d03av04.boulder.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id n16IriZC024529 for ; Fri, 6 Feb 2009 11:53:44 -0700 Received: from squirrel.codemonkey.ws (sig-9-65-84-242.mts.ibm.com [9.65.84.242]) by d03av04.boulder.ibm.com (8.12.11.20060308/8.12.11) with ESMTP id n16IrfCJ024460 for ; Fri, 6 Feb 2009 11:53:42 -0700 Message-ID: <498C8722.1050306@us.ibm.com> Date: Fri, 06 Feb 2009 12:53:22 -0600 From: Anthony Liguori MIME-Version: 1.0 Subject: Re: [Qemu-devel] [Patch] Add -net dump option References: <20090204102251.GA16599@ulanbator.act-europe.fr> In-Reply-To: <20090204102251.GA16599@ulanbator.act-europe.fr> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Tristan Gingold wrote: > Hi, > > this patch add a new network pseudo nic that dump traffic to a tcpdump > compatible file. I have found this feature useful to debug network protocols > with the user stack. > > Signed-off-by: Tristan Gingold > Tristan. > > > diff --git a/qemu-doc.texi b/qemu-doc.texi > index b2fa19e..73c133a 100644 > --- a/qemu-doc.texi > +++ b/qemu-doc.texi > @@ -739,6 +739,15 @@ vde_switch -F -sock /tmp/myswitch > qemu linux.img -net nic -net vde,sock=/tmp/myswitch > @end example > > +@item -net dump[,vlan=@var{n}][,file=@var{file}][,len=@var{len}] > +Dump network traffic on VLAN @var{n} to file @var{file} (@file{qemu.tcpdump} by > +default). At most @var{len} bytes (64 by default) by packet are stored. The > +dump can be analyzed with tools such as tcpdump. > + > +For better results you'd better to use this option before the @samp{-net user} > For better results, you should use this option before the @samp{-net user} > +one as the user stack may send replies before the initial packet was propagated > I'd rather you fix this properly. > +to all receivers. > + > @item -net none > Indicate that no network devices should be configured. It is used to > override the default configuration (@option{-net nic -net user}) which > > diff --git a/net.c b/net.c > index 8d9b3de..872a17e 100644 > --- a/net.c > +++ b/net.c > @@ -1064,6 +1064,75 @@ static int net_vde_init(VLANState *vlan, const char *model, > } > #endif > > +static VLANClientState *tcpdump_vc; > +static FILE *tcpdump_file; > +static int tcpdump_caplen; > Instead of it being globals, you should have a proper state. That way you can use this functionality with multiple VLANs. > +#define TCPDUMP_MAGIC 0xa1b2c3d4 > + > +struct pcap_file_hdr { > + uint32_t magic; > + uint16_t version_major; > + uint16_t version_minor; > + int32_t thiszone; > + uint32_t sigfigs; > + uint32_t snaplen; > + uint32_t linktype; > +}; > + > +struct pcap_sf_pkthdr { > + struct { > + int32_t tv_sec; > + int32_t tv_usec; > + } ts; > + uint32_t caplen; > + uint32_t len; > +}; > + > +static void tcpdump_receive(void *opaque, const uint8_t *buf, int size) > +{ > + struct pcap_sf_pkthdr hdr; > + int64_t ts = muldiv64 (qemu_get_clock(vm_clock),1000000, ticks_per_sec); > + int caplen = size > tcpdump_caplen ? tcpdump_caplen : size; > + > + hdr.ts.tv_sec = ts / 1000000000LL; > + hdr.ts.tv_usec = ts % 1000000; > + hdr.caplen = caplen; > + hdr.len = size; > + fwrite(&hdr, sizeof(hdr), 1, tcpdump_file); > + fwrite(buf, caplen, 1, tcpdump_file); > You should have some error checking here. Are tcpdump files always native endian? Regards, Anthony Liguori