qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Huth <thuth@redhat.com>
To: Stefan Hajnoczi <stefanha@redhat.com>,
	Jason Wang <jasowang@redhat.com>,
	qemu-devel@nongnu.org, yanghy@cn.fujitsu.com
Cc: Markus Armbruster <armbru@redhat.com>,
	"Michael S. Tsirkin" <mst@redhat.com>
Subject: [Qemu-devel] [PATCH RFC 4/5] net/dump: Provide the dumping facility as a net filter
Date: Thu, 27 Aug 2015 04:33:23 +0200	[thread overview]
Message-ID: <1440642804-29001-5-git-send-email-thuth@redhat.com> (raw)
In-Reply-To: <1440642804-29001-1-git-send-email-thuth@redhat.com>

Add glue code to use the dumping functions as a netdev
filter, too.

Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 net/dump.c       | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 net/filter.c     |  1 +
 net/filters.h    |  2 ++
 qapi-schema.json | 20 +++++++++++++++++++-
 4 files changed, 76 insertions(+), 1 deletion(-)

diff --git a/net/dump.c b/net/dump.c
index 8317102..b09d2b9 100644
--- a/net/dump.c
+++ b/net/dump.c
@@ -28,7 +28,9 @@
 #include "qemu/iov.h"
 #include "qemu/log.h"
 #include "qemu/timer.h"
+#include "net/filter.h"
 #include "hub.h"
+#include "filters.h"
 
 typedef struct DumpState {
     int64_t start_ts;
@@ -224,3 +226,55 @@ int net_init_dump(const NetClientOptions *opts, const char *name,
     }
     return rc;
 }
+
+/* Dumping via filter */
+
+typedef struct NetFilterDumpState {
+    NetFilterState nf;
+    DumpState ds;
+} NetFilterDumpState;
+
+static ssize_t filter_dump_receive_iov(NetFilterState *nf, NetClientState *sndr,
+                                       unsigned flags,
+                                       const struct iovec *iov, int iovcnt,
+                                       NetPacketSent *sent_cb)
+{
+    NetFilterDumpState *nfds = DO_UPCAST(NetFilterDumpState, nf, nf);
+
+    dump_receive_iov(&nfds->ds, iov, iovcnt);
+    return 0;
+}
+
+static void filter_dump_cleanup(NetFilterState *nf)
+{
+    NetFilterDumpState *nfds = DO_UPCAST(NetFilterDumpState, nf, nf);
+
+    dump_cleanup(&nfds->ds);
+}
+
+static NetFilterInfo net_filter_dump_info = {
+    .type = NET_FILTER_OPTIONS_KIND_DUMP,
+    .size = sizeof(NetFilterDumpState),
+    .receive_iov = filter_dump_receive_iov,
+    .cleanup = filter_dump_cleanup,
+};
+
+int net_init_filter_dump(const NetFilterOptions *opts, const char *name,
+                         int chain, NetClientState *netdev, Error **errp)
+{
+    NetFilterState *nf;
+    NetFilterDumpState *nfds;
+    const NetFilterDumpOptions *dumpopt;
+    int dump_len = 65536;
+
+    assert(opts->kind == NET_FILTER_OPTIONS_KIND_DUMP);
+    dumpopt = opts->dump;
+    if (dumpopt->has_maxlen) {
+        dump_len = dumpopt->maxlen;
+    }
+
+    nf = qemu_new_net_filter(&net_filter_dump_info, netdev, name, chain);
+    nfds = DO_UPCAST(NetFilterDumpState, nf, nf);
+
+    return net_dump_state_init(&nfds->ds, dumpopt->file, dump_len, errp);
+}
diff --git a/net/filter.c b/net/filter.c
index f23fc7f..536d236 100644
--- a/net/filter.c
+++ b/net/filter.c
@@ -216,6 +216,7 @@ typedef int (NetFilterInit)(const NetFilterOptions *opts,
 static
 NetFilterInit * const net_filter_init_fun[NET_FILTER_OPTIONS_KIND_MAX] = {
     [NET_FILTER_OPTIONS_KIND_BUFFER] = net_init_filter_buffer,
+    [NET_FILTER_OPTIONS_KIND_DUMP] = net_init_filter_dump,
 };
 
 static int net_filter_init1(const NetFilter *netfilter, Error **errp)
diff --git a/net/filters.h b/net/filters.h
index 3b546db..f63bde1 100644
--- a/net/filters.h
+++ b/net/filters.h
@@ -13,5 +13,7 @@
 
 int net_init_filter_buffer(const NetFilterOptions *opts, const char *name,
                            int chain, NetClientState *netdev, Error **errp);
+int net_init_filter_dump(const NetFilterOptions *opts, const char *name,
+                           int chain, NetClientState *netdev, Error **errp);
 
 #endif /* QEMU_NET_FILTERS_H */
diff --git a/qapi-schema.json b/qapi-schema.json
index 7882641..71caca9 100644
--- a/qapi-schema.json
+++ b/qapi-schema.json
@@ -2599,6 +2599,23 @@
     '*interval':     'uint32' } }
 
 ##
+# @NetFilterDumpOptions
+#
+# a dumping filter for network backends.
+#
+# @file: Name of the file where the dump data should be written into.
+#
+# @maxlen: Crop big packets to this size before writing them into the
+#          dump file.
+#
+# Since 2.5
+##
+{ 'struct': 'NetFilterDumpOptions',
+  'data': {
+    'file': 'str',
+    '*maxlen':  'int32' } }
+
+##
 # @NetFilterOptions
 #
 # A discriminated record of network filters.
@@ -2608,7 +2625,8 @@
 ##
 { 'union': 'NetFilterOptions',
   'data': {
-    'buffer':     'NetFilterBufferOptions'} }
+    'buffer':     'NetFilterBufferOptions',
+    'dump':       'NetFilterDumpOptions' } }
 
 ##
 # @NetFilter
-- 
1.8.3.1

  parent reply	other threads:[~2015-08-27  2:33 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-27  2:33 [Qemu-devel] [PATCH RFC 0/5] Network traffic dumping via netfilter Thomas Huth
2015-08-27  2:33 ` [Qemu-devel] [PATCH RFC 1/5] net/dump: Add support for receive_iov function Thomas Huth
2015-08-27  2:33 ` [Qemu-devel] [PATCH RFC 2/5] net/dump: Rework net-dump init functions Thomas Huth
2015-08-27  2:33 ` [Qemu-devel] [PATCH RFC 3/5] net/dump: Separate the NetClientState from the DumpState Thomas Huth
2015-08-27  2:33 ` Thomas Huth [this message]
2015-08-27  3:03   ` [Qemu-devel] [PATCH RFC 4/5] net/dump: Provide the dumping facility as a net filter Yang Hongyang
2015-09-01 19:52   ` Eric Blake
2015-09-01 21:19     ` Thomas Huth
2015-08-27  2:33 ` [Qemu-devel] [PATCH RFC 5/5] net/dump: Add documentation Thomas Huth
2015-08-27  3:29 ` [Qemu-devel] [PATCH RFC 0/5] Network traffic dumping via netfilter Jason Wang

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=1440642804-29001-5-git-send-email-thuth@redhat.com \
    --to=thuth@redhat.com \
    --cc=armbru@redhat.com \
    --cc=jasowang@redhat.com \
    --cc=mst@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    --cc=yanghy@cn.fujitsu.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).