[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [Patch] Add -net dump option
From: |
Tristan Gingold |
Subject: |
[Qemu-devel] [Patch] Add -net dump option |
Date: |
Wed, 4 Feb 2009 11:22:51 +0100 |
User-agent: |
Mutt/1.4.2.3i |
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 <address@hidden>
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
address@hidden -net dump[,address@hidden,address@hidden,address@hidden
+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}
+one as the user stack may send replies before the initial packet was propagated
+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;
+
+#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);
+}
+
+static int net_tcpdump_init(VLANState *vlan, const char *device,
+ const char *name, const char *filename, int len)
+{
+ struct pcap_file_hdr hdr;
+
+ tcpdump_file = fopen(filename, "wb");
+ if (!tcpdump_file) {
+ fprintf(stderr, "-net dump: can't open %s\n", filename);
+ exit(1);
+ }
+
+ tcpdump_caplen = len;
+
+ hdr.magic = TCPDUMP_MAGIC;
+ hdr.version_major = 2;
+ hdr.version_minor = 4;
+ hdr.thiszone = 0;
+ hdr.sigfigs = 0;
+ hdr.snaplen = tcpdump_caplen;
+ hdr.linktype = 1;
+
+ fwrite(&hdr, sizeof(hdr), 1, tcpdump_file);
+
+ tcpdump_vc = qemu_new_vlan_client(vlan, device, name,
+ tcpdump_receive, NULL, NULL);
+ snprintf(tcpdump_vc->info_str, sizeof(tcpdump_vc->info_str),
+ "tcpdump to %s (len=%d)", filename, len);
+ return 0;
+}
+
/* network connection */
typedef struct NetSocketState {
VLANClientState *vc;
@@ -1701,6 +1770,18 @@ int net_client_init(const char *device, const char *p)
ret = net_vde_init(vlan, device, name, vde_sock, vde_port, vde_group,
vde_mode);
} else
#endif
+ if (!strcmp(device, "dump")) {
+ int len = 64;
+ if (get_param_value(buf, sizeof(buf), "len", p) > 0) {
+ len = strtol(buf, NULL, 0);
+ }
+
+ if (!get_param_value(buf, sizeof(buf), "file", p)) {
+ strcpy(buf, "qemu.tcpdump");
+ }
+ vlan->nb_host_devs++;
+ ret = net_tcpdump_init(vlan, device, name, buf, len);
+ } else
{
fprintf(stderr, "Unknown network device: %s\n", device);
if (name)
diff --git a/vl.c b/vl.c
index 3676537..69dbe63 100644
--- a/vl.c
+++ b/vl.c
@@ -3943,6 +3943,8 @@ static void help(int exitcode)
" Use group 'groupname' and mode 'octalmode' to
change default\n"
" ownership and permissions for communication
port.\n"
#endif
+ "-net dump[,vlan=n][,file=f][,len=n]\n"
+ " dump traffic on vlan 'n' to file 'f' (max n bytes
per packet)\n"
"-net none use it alone to have zero network devices; if no
-net option\n"
" is provided, the default is '-net nic -net user'\n"
#ifdef CONFIG_SLIRP
- [Qemu-devel] [Patch] Add -net dump option,
Tristan Gingold <=
Re: [Qemu-devel] [Patch] Add -net dump option, Luca Bigliardi, 2009/02/06