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 <th...@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