@@ -98,6 +98,7 @@ struct NetClientState {
NetClientDestructor *destructor;
unsigned int queue_index;
unsigned rxfilter_notify_enabled:1;
+ unsigned netdev_dump_enabled:1;
NetDumpState nds;
};
@@ -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,
@@ -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;
+}
@@ -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;
}
@@ -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
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(-)