From: Thomas Huth <thuth@redhat.com>
To: qemu-devel@nongnu.org, Stefan Hajnoczi <stefanha@redhat.com>,
Jason Wang <jasowang@redhat.com>
Cc: Markus Armbruster <armbru@redhat.com>
Subject: [Qemu-devel] [PATCH v2 4/5] net/dump: Add dump option for netdev devices
Date: Mon, 13 Jul 2015 09:39:25 +0200 [thread overview]
Message-ID: <1436773166-12113-5-git-send-email-thuth@redhat.com> (raw)
In-Reply-To: <1436773166-12113-1-git-send-email-thuth@redhat.com>
So far it is not possible to dump network traffic with the "-netdev"
option yet - this is only possible with the "-net" option and an
internal "hub".
This patch now fixes this ugliness by adding a proper, generic
dumpfile parameter to the "-netdev" option.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
include/net/net.h | 1 +
net/clients.h | 2 ++
net/dump.c | 28 ++++++++++++++++++++++++++++
net/net.c | 38 +++++++++++++++++++++++++++++++++++++-
qapi-schema.json | 12 ++++++++++--
5 files changed, 78 insertions(+), 3 deletions(-)
diff --git a/include/net/net.h b/include/net/net.h
index b265047..62abc98 100644
--- a/include/net/net.h
+++ b/include/net/net.h
@@ -98,6 +98,7 @@ struct NetClientState {
NetClientDestructor *destructor;
unsigned int queue_index;
unsigned rxfilter_notify_enabled:1;
+ unsigned netdev_dump_enabled:1;
NetDumpState nds;
};
diff --git a/net/clients.h b/net/clients.h
index 403dc88..e120ed0 100644
--- a/net/clients.h
+++ b/net/clients.h
@@ -29,6 +29,8 @@
int net_init_dump(const NetClientOptions *opts, const char *name,
NetClientState *peer, Error **errp);
+int netdev_init_dumpfile(const Netdev *netdev, const char *name,
+ Error **errp);
void net_dump_cleanup(NetClientState *nc);
ssize_t net_dump_receive(NetClientState *nc, const uint8_t *buf, size_t size);
ssize_t net_dump_receive_iov(NetClientState *nc, const struct iovec *iov,
diff --git a/net/dump.c b/net/dump.c
index fa63f0b..2cb3b4b 100644
--- a/net/dump.c
+++ b/net/dump.c
@@ -28,6 +28,7 @@
#include "qemu/iov.h"
#include "qemu/log.h"
#include "qemu/timer.h"
+#include "net/vhost_net.h"
#include "hub.h"
#define PCAP_MAGIC 0xa1b2c3d4
@@ -197,3 +198,30 @@ int net_init_dump(const NetClientOptions *opts, const char *name,
}
return rc;
}
+
+int netdev_init_dumpfile(const Netdev *netdev, const char *name,
+ Error **errp)
+{
+ NetClientState *nc;
+ int dumplen = 65536;
+ int rc;
+
+ nc = qemu_find_netdev(name);
+ assert(nc);
+ if (get_vhost_net(nc)) {
+ error_setg(errp, "dumping does not work with vhost enabled");
+ return -1;
+ }
+
+ if (netdev->has_dumplen) {
+ dumplen = netdev->dumplen;
+ }
+
+ rc = net_dump_state_init(nc, netdev->dumpfile, dumplen, errp);
+ if (rc == 0) {
+ nc->netdev_dump_enabled = true;
+ }
+ /* TODO: Setup for multiqueue devices? */
+
+ return rc;
+}
diff --git a/net/net.c b/net/net.c
index 6ff7fec..1bdb731 100644
--- a/net/net.c
+++ b/net/net.c
@@ -360,6 +360,11 @@ static void qemu_cleanup_net_client(NetClientState *nc)
{
QTAILQ_REMOVE(&net_clients, nc, next);
+ if (nc->netdev_dump_enabled) {
+ net_dump_cleanup(nc);
+ nc->netdev_dump_enabled = 0;
+ }
+
if (nc->info->cleanup) {
nc->info->cleanup(nc);
}
@@ -571,6 +576,21 @@ ssize_t qemu_deliver_packet(NetClientState *sender,
return 0;
}
+ /*
+ * Log network traffic into a dump file. Note: This should ideally
+ * be done after calling the ->receive() function below to make sure
+ * that we only log the packets that have really been sent. However,
+ * this does not work right with slirp networking since it immediately
+ * sends reply packets during the receive() function already, so we
+ * would get a wrong order of the packets in the dump file in that case.
+ */
+ if (sender->netdev_dump_enabled) {
+ net_dump_receive(sender, data, size);
+ }
+ if (nc->netdev_dump_enabled) {
+ net_dump_receive(nc, data, size);
+ }
+
if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
ret = nc->info->receive_raw(nc, data, size);
} else {
@@ -687,6 +707,13 @@ ssize_t qemu_deliver_packet_iov(NetClientState *sender,
return 0;
}
+ if (sender->netdev_dump_enabled) {
+ net_dump_receive_iov(sender, iov, iovcnt);
+ }
+ if (nc->netdev_dump_enabled) {
+ net_dump_receive_iov(nc, iov, iovcnt);
+ }
+
if (nc->info->receive_iov) {
ret = nc->info->receive_iov(nc, iov, iovcnt);
} else {
@@ -880,7 +907,6 @@ static int net_init_nic(const NetClientOptions *opts, const char *name,
return idx;
}
-
static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])(
const NetClientOptions *opts,
const char *name,
@@ -966,6 +992,16 @@ static int net_client_init1(const void *object, int is_netdev, Error **errp)
}
return -1;
}
+
+ if (is_netdev) {
+ const Netdev *netdev = object;
+ if (netdev->has_dumpfile) {
+ if (netdev_init_dumpfile(netdev, name, errp)) {
+ return -1;
+ }
+ }
+ }
+
return 0;
}
diff --git a/qapi-schema.json b/qapi-schema.json
index 1285b8c..e7cd21f 100644
--- a/qapi-schema.json
+++ b/qapi-schema.json
@@ -2531,14 +2531,22 @@
#
# @id: identifier for monitor commands.
#
+# @dumpfile: #optional name of a file where network traffic should be logged
+# (since: 2.5)
+#
+# @dumplen: #optional maximum length of the network packets in the dump
+# (since: 2.5)
+#
# @opts: device type specific properties
#
# Since 1.2
##
{ 'struct': 'Netdev',
'data': {
- 'id': 'str',
- 'opts': 'NetClientOptions' } }
+ 'id': 'str',
+ '*dumpfile': 'str',
+ '*dumplen': 'int32',
+ 'opts': 'NetClientOptions' } }
##
# @InetSocketAddress
--
1.8.3.1
next prev parent reply other threads:[~2015-07-13 7:39 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-07-13 7:39 [Qemu-devel] [PATCH v2 0/5] For QEMU 2.5: Network traffic dumping for -netdev devices Thomas Huth
2015-07-13 7:39 ` [Qemu-devel] [PATCH v2 1/5] net/dump: Add support for receive_iov function Thomas Huth
2015-07-13 7:39 ` [Qemu-devel] [PATCH v2 2/5] net/dump: Move DumpState into NetClientState Thomas Huth
2015-07-13 7:39 ` [Qemu-devel] [PATCH v2 3/5] net/dump: Rework net-dump init functions Thomas Huth
2015-07-13 7:39 ` Thomas Huth [this message]
2015-07-13 7:39 ` [Qemu-devel] [PATCH v2 5/5] qemu options: Add information about dumpfile to help text Thomas Huth
2015-07-22 6:35 ` [Qemu-devel] [PATCH v2 0/5] For QEMU 2.5: Network traffic dumping for -netdev devices Jason Wang
2015-07-22 10:52 ` Yang Hongyang
2015-07-22 10:55 ` [Qemu-devel] [PATCH] RFC/net: Add a net filter Yang Hongyang
2015-07-22 11:06 ` Daniel P. Berrange
2015-07-22 15:16 ` Yang Hongyang
2015-07-22 13:05 ` Thomas Huth
2015-07-22 15:06 ` Yang Hongyang
2015-07-22 13:26 ` Stefan Hajnoczi
2015-07-22 14:57 ` Yang Hongyang
2015-07-23 11:57 ` Stefan Hajnoczi
2015-07-23 5:59 ` Jason Wang
2015-07-27 5:27 ` Yang Hongyang
2015-07-27 6:02 ` Yang Hongyang
2015-07-27 6:39 ` Jason Wang
2015-07-27 7:00 ` Yang Hongyang
2015-07-27 7:31 ` Jason Wang
2015-07-27 7:45 ` Yang Hongyang
2015-07-27 8:01 ` Jason Wang
2015-07-27 8:39 ` Yang Hongyang
2015-07-27 9:16 ` Jason Wang
2015-07-27 10:03 ` Yang Hongyang
2015-07-28 3:28 ` Jason Wang
2015-07-28 4:00 ` Yang Hongyang
2015-07-28 8:52 ` Yang Hongyang
2015-07-28 9:19 ` Yang Hongyang
2015-07-28 9:30 ` Jason Wang
2015-07-28 9:41 ` Yang Hongyang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1436773166-12113-5-git-send-email-thuth@redhat.com \
--to=thuth@redhat.com \
--cc=armbru@redhat.com \
--cc=jasowang@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).