From patchwork Mon Jul 13 07:39:25 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Huth X-Patchwork-Id: 494253 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id CB5531402B9 for ; Mon, 13 Jul 2015 17:40:19 +1000 (AEST) Received: from localhost ([::1]:53333 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZEYLa-0004Vb-0Y for incoming@patchwork.ozlabs.org; Mon, 13 Jul 2015 03:40:18 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35075) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZEYKw-0003Wb-R5 for qemu-devel@nongnu.org; Mon, 13 Jul 2015 03:39:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZEYKv-00044g-4L for qemu-devel@nongnu.org; Mon, 13 Jul 2015 03:39:38 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57062) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZEYKu-00044M-TY for qemu-devel@nongnu.org; Mon, 13 Jul 2015 03:39:37 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (Postfix) with ESMTPS id 8BB122620 for ; Mon, 13 Jul 2015 07:39:36 +0000 (UTC) Received: from thh440s.redhat.com (vpn1-4-235.ams2.redhat.com [10.36.4.235]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t6D7dQ8D007406; Mon, 13 Jul 2015 03:39:34 -0400 From: Thomas Huth To: qemu-devel@nongnu.org, Stefan Hajnoczi , Jason Wang Date: Mon, 13 Jul 2015 09:39:25 +0200 Message-Id: <1436773166-12113-5-git-send-email-thuth@redhat.com> In-Reply-To: <1436773166-12113-1-git-send-email-thuth@redhat.com> References: <1436773166-12113-1-git-send-email-thuth@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Markus Armbruster Subject: [Qemu-devel] [PATCH v2 4/5] net/dump: Add dump option for netdev devices X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- 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