Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9191EEE0ADC for ; Sat, 7 Feb 2026 13:49:20 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1voifZ-00038q-8W; Sat, 07 Feb 2026 08:48:18 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1voifO-00031D-5m for qemu-devel@nongnu.org; Sat, 07 Feb 2026 08:48:07 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1voifK-0003DS-VK for qemu-devel@nongnu.org; Sat, 07 Feb 2026 08:48:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1770472082; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pqeiNPZr2xEEOl1HXgVWwToeoJpI5kf9x7HKvwPSlNA=; b=BADEuUmjOK9uIFhRQl3RoloFk768wpFha6IxbOQFZHOgeywRnu3x21Mlohs0wgA6gRWXQq IE+h7S74gvCTJK475wHgof6VtodR2uX/wSp3qq+wJzWxozsvCzDCWbkFjt7npVXlXcc1hu f8pwAeamgIXDPoLAd+85ZIWmhg6i3Tk= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-593-zgkR_ncjMFObe-pHrwB92A-1; Sat, 07 Feb 2026 08:47:58 -0500 X-MC-Unique: zgkR_ncjMFObe-pHrwB92A-1 X-Mimecast-MFC-AGG-ID: zgkR_ncjMFObe-pHrwB92A_1770472077 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9CBBF18003FC; Sat, 7 Feb 2026 13:47:57 +0000 (UTC) Received: from S2.redhat.com (unknown [10.72.112.15]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 8096618004AD; Sat, 7 Feb 2026 13:47:54 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, mst@redhat.com, jasowang@redhat.com, zhangckid@gmail.com, lizhijian@fujitsu.com, qemu-devel@nongnu.org Subject: [PATCH 04/12] net/filter-mirror: add indev/outdev statistics for filter-redirector Date: Sat, 7 Feb 2026 21:47:20 +0800 Message-ID: <20260207134728.1495932-4-lulu@redhat.com> In-Reply-To: <20260207134728.1495932-1-lulu@redhat.com> References: <20260207134728.1495932-1-lulu@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Received-SPF: pass client-ip=170.10.129.124; envelope-from=lulu@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Jason Wang Add packet and byte counters for filter-redirector's indev and outdev character devices: - indev_packets/indev_bytes: count packets/bytes received from indev - outdev_packets/outdev_bytes: count packets/bytes sent to outdev The indev counters are updated in redirector_rs_finalize() when a complete packet is read from indev. The outdev counters are updated in filter_redirector_receive_iov() when data is successfully sent to outdev. Design changes: - Add FilterGetStats callback to NetFilterClass, allowing each filter type to implement its own statistics collection - Introduce a flexible 'counters' array in NetFilterStats (QAPI) to allow different filter types to expose their own specific counters - net.c uses the callback instead of directly calling filter-specific functions, improving modularity The QMP query-netfilter-stats command and HMP info netfilter-stats command now include the optional counters array for filters that implement the get_stats callback. Example output: (qemu) info netfilter-stats rd0: netdev=bn0 type=filter-redirector tx: 5 packets, 500 bytes rx: 0 packets, 0 bytes indev: 3 packets, 300 bytes outdev: 5 packets, 500 bytes Signed-off-by: Jason Wang Signed-off-by: Cindy Lu --- include/net/filter.h | 8 ++++++++ net/filter-mirror.c | 42 ++++++++++++++++++++++++++++++++++++++++-- net/net-hmp-cmds.c | 10 ++++++++++ net/net.c | 28 ++++++++++++++++++++++++++++ qapi/net.json | 25 ++++++++++++++++++++++++- 5 files changed, 110 insertions(+), 3 deletions(-) diff --git a/include/net/filter.h b/include/net/filter.h index 04786e4e87..f0673c9163 100644 --- a/include/net/filter.h +++ b/include/net/filter.h @@ -35,6 +35,13 @@ typedef void (FilterStatusChanged) (NetFilterState *nf, Error **errp); typedef void (FilterHandleEvent) (NetFilterState *nf, int event, Error **errp); +/* + * Get filter-specific statistics counters. + * Returns a GList of NetFilterCounter (QAPI type), or NULL if no counters. + * The caller is responsible for freeing the returned list and its contents. + */ +typedef GList *(FilterGetStats) (NetFilterState *nf); + struct NetFilterClass { ObjectClass parent_class; @@ -43,6 +50,7 @@ struct NetFilterClass { FilterCleanup *cleanup; FilterStatusChanged *status_changed; FilterHandleEvent *handle_event; + FilterGetStats *get_stats; /* mandatory */ FilterReceiveIOV *receive_iov; }; diff --git a/net/filter-mirror.c b/net/filter-mirror.c index e1a0650abd..21598b97a7 100644 --- a/net/filter-mirror.c +++ b/net/filter-mirror.c @@ -11,8 +11,12 @@ #include "qemu/osdep.h" #include "net/filter.h" +#define TYPE_FILTER_MIRROR "filter-mirror" +#define TYPE_FILTER_REDIRECTOR "filter-redirector" + #include "net/net.h" #include "qapi/error.h" +#include "qapi/qapi-types-net.h" #include "qom/object.h" #include "qemu/main-loop.h" #include "qemu/error-report.h" @@ -23,12 +27,10 @@ #include "block/aio-wait.h" #include "system/runstate.h" -#define TYPE_FILTER_MIRROR "filter-mirror" typedef struct MirrorState MirrorState; DECLARE_INSTANCE_CHECKER(MirrorState, FILTER_MIRROR, TYPE_FILTER_MIRROR) -#define TYPE_FILTER_REDIRECTOR "filter-redirector" DECLARE_INSTANCE_CHECKER(MirrorState, FILTER_REDIRECTOR, TYPE_FILTER_REDIRECTOR) @@ -44,6 +46,12 @@ struct MirrorState { bool vnet_hdr; bool enable_when_stopped; VMChangeStateEntry *vmsentry; + + /* indev/outdev statistics for filter-redirector */ + uint64_t indev_packets; + uint64_t indev_bytes; + uint64_t outdev_packets; + uint64_t outdev_bytes; }; typedef struct FilterSendCo { @@ -234,6 +242,10 @@ static ssize_t filter_redirector_receive_iov(NetFilterState *nf, ret = filter_send(s, iov, iovcnt); if (ret < 0) { error_report("filter redirector send failed(%s)", strerror(-ret)); + } else if (ret > 0) { + /* Update outdev statistics on successful send */ + s->outdev_packets++; + s->outdev_bytes += ret; } return ret; } else { @@ -283,6 +295,10 @@ static void redirector_rs_finalize(SocketReadState *rs) MirrorState *s = container_of(rs, MirrorState, rs); NetFilterState *nf = NETFILTER(s); + /* Update indev statistics */ + s->indev_packets++; + s->indev_bytes += rs->packet_len; + redirector_to_filter(nf, rs->buf, rs->packet_len); } @@ -487,6 +503,27 @@ static void filter_mirror_class_init(ObjectClass *oc, const void *data) nfc->receive_iov = filter_mirror_receive_iov; } +static GList *filter_redirector_get_stats(NetFilterState *nf) +{ + MirrorState *s = FILTER_REDIRECTOR(nf); + GList *list = NULL; + NetFilterCounter *counter; + + counter = g_new0(NetFilterCounter, 1); + counter->name = g_strdup("indev"); + counter->packets = s->indev_packets; + counter->bytes = s->indev_bytes; + list = g_list_append(list, counter); + + counter = g_new0(NetFilterCounter, 1); + counter->name = g_strdup("outdev"); + counter->packets = s->outdev_packets; + counter->bytes = s->outdev_bytes; + list = g_list_append(list, counter); + + return list; +} + static void filter_redirector_class_init(ObjectClass *oc, const void *data) { NetFilterClass *nfc = NETFILTER_CLASS(oc); @@ -506,6 +543,7 @@ static void filter_redirector_class_init(ObjectClass *oc, const void *data) nfc->cleanup = filter_redirector_cleanup; nfc->receive_iov = filter_redirector_receive_iov; nfc->status_changed = filter_redirector_status_changed; + nfc->get_stats = filter_redirector_get_stats; } static void filter_mirror_init(Object *obj) diff --git a/net/net-hmp-cmds.c b/net/net-hmp-cmds.c index 9bdd6d233f..df9336be1e 100644 --- a/net/net-hmp-cmds.c +++ b/net/net-hmp-cmds.c @@ -192,6 +192,16 @@ void hmp_info_netfilter_stats(Monitor *mon, const QDict *qdict) stats->packets_tx, stats->bytes_tx); monitor_printf(mon, " rx: %" PRIu64 " packets, %" PRIu64 " bytes\n", stats->packets_rx, stats->bytes_rx); + if (stats->counters) { + NetFilterCounterList *counter_entry; + for (counter_entry = stats->counters; counter_entry; + counter_entry = counter_entry->next) { + NetFilterCounter *counter = counter_entry->value; + monitor_printf(mon, + " %s: %" PRIu64 " packets, %" PRIu64 " bytes\n", + counter->name, counter->packets, counter->bytes); + } + } } qapi_free_NetFilterStatsList(stats_list); diff --git a/net/net.c b/net/net.c index 40966fbf8a..b15f4db65e 100644 --- a/net/net.c +++ b/net/net.c @@ -1631,6 +1631,34 @@ NetFilterStatsList *qmp_query_netfilter_stats(const char *name, Error **errp) stats->bytes_tx = nf->bytes_tx; stats->bytes_rx = nf->bytes_rx; + /* Get filter-specific counters via callback */ + { + NetFilterClass *nfc = NETFILTER_GET_CLASS(nf); + if (nfc->get_stats) { + GList *filter_counters = nfc->get_stats(nf); + GList *iter; + + for (iter = filter_counters; iter; iter = iter->next) { + NetFilterCounter *fc = iter->data; + NetFilterCounter *counter; + + counter = g_new0(NetFilterCounter, 1); + counter->name = g_strdup(fc->name); + counter->packets = fc->packets; + counter->bytes = fc->bytes; + QAPI_LIST_PREPEND(stats->counters, counter); + + g_free(fc->name); + g_free(fc); + } + g_list_free(filter_counters); + + if (stats->counters) { + stats->has_counters = true; + } + } + } + QAPI_LIST_APPEND(tail, stats); if (name) { diff --git a/qapi/net.json b/qapi/net.json index 277cdd9e32..c57497d328 100644 --- a/qapi/net.json +++ b/qapi/net.json @@ -1192,6 +1192,25 @@ { 'event': 'NETDEV_VHOST_USER_DISCONNECTED', 'data': { 'netdev-id': 'str' } } +## +# @NetFilterCounter: +# +# A filter-specific counter with packet and byte counts. +# +# @name: the name of the counter (e.g., "indev", "outdev") +# +# @packets: number of packets +# +# @bytes: number of bytes +# +# Since: 10.1 +## +{ 'struct': 'NetFilterCounter', + 'data': { + 'name': 'str', + 'packets': 'uint64', + 'bytes': 'uint64' } } + ## # @NetFilterStats: # @@ -1211,6 +1230,9 @@ # # @bytes-rx: number of received bytes # +# @counters: filter-specific counters (optional, depends on +# filter type) +# # Since: 10.1 ## { 'struct': 'NetFilterStats', @@ -1221,7 +1243,8 @@ 'packets-tx': 'uint64', 'packets-rx': 'uint64', 'bytes-tx': 'uint64', - 'bytes-rx': 'uint64' } } + 'bytes-rx': 'uint64', + '*counters': ['NetFilterCounter'] } } ## # @query-netfilter-stats: -- 2.52.0