LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Alexander Lobakin <alexandr.lobakin@intel.com>
To: Michal Kubecek <mkubecek@suse.cz>
Cc: "David S. Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>,
	Alexander Lobakin <alexandr.lobakin@intel.com>,
	Jesse Brandeburg <jesse.brandeburg@intel.com>,
	Lukasz Czapnik <lukasz.czapnik@intel.com>,
	Marcin Kubiak <marcin.kubiak@intel.com>,
	Michal Kubiak <michal.kubiak@intel.com>,
	Michal Swiatkowski <michal.swiatkowski@intel.com>,
	Jonathan Corbet <corbet@lwn.net>,
	Netanel Belgazal <netanel@amazon.com>,
	Arthur Kiyanovski <akiyano@amazon.com>,
	Guy Tzalik <gtzalik@amazon.com>,
	Saeed Bishara <saeedb@amazon.com>,
	Ioana Ciornei <ioana.ciornei@nxp.com>,
	Claudiu Manoil <claudiu.manoil@nxp.com>,
	Thomas Petazzoni <thomas.petazzoni@bootlin.com>,
	Marcin Wojtas <mw@semihalf.com>,
	Russell King <linux@armlinux.org.uk>,
	Edward Cree <ecree.xilinx@gmail.com>,
	Martin Habets <habetsm.xilinx@gmail.com>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	Jason Wang <jasowang@redhat.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Jesper Dangaard Brouer <hawk@kernel.org>,
	John Fastabend <john.fastabend@gmail.com>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
	Yonghong Song <yhs@fb.com>, KP Singh <kpsingh@kernel.org>,
	Shay Agroskin <shayagr@amazon.com>,
	Sameeh Jubran <sameehj@amazon.com>,
	Alexander Duyck <alexanderduyck@fb.com>,
	Danielle Ratson <danieller@nvidia.com>,
	Ido Schimmel <idosch@nvidia.com>, Andrew Lunn <andrew@lunn.ch>,
	Vladyslav Tarasiuk <vladyslavt@nvidia.com>,
	Arnd Bergmann <arnd@arndb.de>,
	Andrew Morton <akpm@linux-foundation.org>,
	Jian Shen <shenjian15@huawei.com>,
	Petr Vorel <petr.vorel@gmail.com>, Dan Murphy <dmurphy@ti.com>,
	Yangbo Lu <yangbo.lu@nxp.com>,
	Zheng Yongjun <zhengyongjun3@huawei.com>,
	Heiner Kallweit <hkallweit1@gmail.com>,
	YueHaibing <yuehaibing@huawei.com>,
	Johannes Berg <johannes@sipsolutions.net>,
	netdev@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	virtualization@lists.linux-foundation.org, bpf@vger.kernel.org
Subject: [PATCH ethtool-next 2/5] stats: factor out one stat field printing
Date: Tue,  3 Aug 2021 18:51:37 +0200	[thread overview]
Message-ID: <20210803165140.172-3-alexandr.lobakin@intel.com> (raw)
In-Reply-To: <20210803165140.172-1-alexandr.lobakin@intel.com>

Move the code that takes one stat field nlattr, validates and prints
it in either stdout or JSON, into a separate function.
It will later be reused by per-channel statistics printing.

Signed-off-by: Alexander Lobakin <alexandr.lobakin@intel.com>
Reviewed-by: Jesse Brandeburg <jesse.brandeburg@intel.com>
---
 netlink/stats.c | 53 +++++++++++++++++++++++++++++++------------------
 1 file changed, 34 insertions(+), 19 deletions(-)

diff --git a/netlink/stats.c b/netlink/stats.c
index 9f609a4ec550..9d950b77d656 100644
--- a/netlink/stats.c
+++ b/netlink/stats.c
@@ -87,6 +87,36 @@ err_close_rmon:
 	return 1;
 }
 
+static int parse_stat(const struct nlattr *attr, const char *grp_name,
+		      const struct stringset *stat_str)
+{
+	const struct nlattr *stat;
+	unsigned long long val;
+	const char *name;
+	unsigned int s;
+	int ret;
+
+	stat = mnl_attr_get_payload(attr);
+	ret = mnl_attr_validate(stat, MNL_TYPE_U64);
+	if (ret) {
+		fprintf(stderr, "invalid kernel response - bad statistic entry\n");
+		return 1;
+	}
+
+	s = mnl_attr_get_type(stat);
+	name = get_string(stat_str, s);
+	if (!name || !name[0])
+		return 0;
+
+	if (!is_json_context())
+		fprintf(stdout, "%s-%s: ", grp_name, name);
+
+	val = mnl_attr_get_u64(stat);
+	print_u64(PRINT_ANY, name, "%llu\n", val);
+
+	return 0;
+}
+
 static int parse_grp(struct nl_context *nlctx, const struct nlattr *grp,
 		     const struct stringset *std_str)
 {
@@ -94,10 +124,9 @@ static int parse_grp(struct nl_context *nlctx, const struct nlattr *grp,
 	DECLARE_ATTR_TB_INFO(tb);
 	bool hist_rx = false, hist_tx = false;
 	const struct stringset *stat_str;
-	const struct nlattr *attr, *stat;
-	const char *std_name, *name;
-	unsigned int ss_id, id, s;
-	unsigned long long val;
+	const struct nlattr *attr;
+	unsigned int ss_id, id;
+	const char *std_name;
 	int ret;
 
 	ret = mnl_attr_parse_nested(grp, attr_cb, &tb_info);
@@ -131,22 +160,8 @@ static int parse_grp(struct nl_context *nlctx, const struct nlattr *grp,
 			continue;
 		}
 
-		stat = mnl_attr_get_payload(attr);
-		ret = mnl_attr_validate(stat, MNL_TYPE_U64);
-		if (ret) {
-			fprintf(stderr, "invalid kernel response - bad statistic entry\n");
+		if (parse_stat(attr, NULL, std_name, stat_str))
 			goto err_close_grp;
-		}
-		s = mnl_attr_get_type(stat);
-		name = get_string(stat_str, s);
-		if (!name || !name[0])
-			continue;
-
-		if (!is_json_context())
-			fprintf(stdout, "%s-%s: ", std_name, name);
-
-		val = mnl_attr_get_u64(stat);
-		print_u64(PRINT_ANY, name, "%llu\n", val);
 	}
 
 	if (hist_rx)
-- 
2.31.1


  parent reply	other threads:[~2021-08-03 16:52 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-03 16:51 [PATCH ethtool-next 0/5] stats: add support for per-channel and XDP standard stats Alexander Lobakin
2021-08-03 16:51 ` [PATCH ethtool-next 1/5] sync UAPI header copies Alexander Lobakin
2021-08-03 16:51 ` Alexander Lobakin [this message]
2021-08-03 16:51 ` [PATCH ethtool-next 3/5] stats: add support for per-channel statistics [blocks] Alexander Lobakin
2021-08-03 16:51 ` [PATCH ethtool-next 4/5] man: fix typo for "rmon" standard stat type Alexander Lobakin
2021-08-03 16:51 ` [PATCH ethtool-next 5/5] man: mention XDP standard statistics in help and man page Alexander Lobakin

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=20210803165140.172-3-alexandr.lobakin@intel.com \
    --to=alexandr.lobakin@intel.com \
    --cc=akiyano@amazon.com \
    --cc=akpm@linux-foundation.org \
    --cc=alexanderduyck@fb.com \
    --cc=andrew@lunn.ch \
    --cc=andrii@kernel.org \
    --cc=arnd@arndb.de \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=claudiu.manoil@nxp.com \
    --cc=corbet@lwn.net \
    --cc=daniel@iogearbox.net \
    --cc=danieller@nvidia.com \
    --cc=davem@davemloft.net \
    --cc=dmurphy@ti.com \
    --cc=ecree.xilinx@gmail.com \
    --cc=gtzalik@amazon.com \
    --cc=habetsm.xilinx@gmail.com \
    --cc=hawk@kernel.org \
    --cc=hkallweit1@gmail.com \
    --cc=idosch@nvidia.com \
    --cc=ioana.ciornei@nxp.com \
    --cc=jasowang@redhat.com \
    --cc=jesse.brandeburg@intel.com \
    --cc=johannes@sipsolutions.net \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=lukasz.czapnik@intel.com \
    --cc=marcin.kubiak@intel.com \
    --cc=michal.kubiak@intel.com \
    --cc=michal.swiatkowski@intel.com \
    --cc=mkubecek@suse.cz \
    --cc=mst@redhat.com \
    --cc=mw@semihalf.com \
    --cc=netanel@amazon.com \
    --cc=netdev@vger.kernel.org \
    --cc=petr.vorel@gmail.com \
    --cc=saeedb@amazon.com \
    --cc=sameehj@amazon.com \
    --cc=shayagr@amazon.com \
    --cc=shenjian15@huawei.com \
    --cc=songliubraving@fb.com \
    --cc=thomas.petazzoni@bootlin.com \
    --cc=virtualization@lists.linux-foundation.org \
    --cc=vladyslavt@nvidia.com \
    --cc=yangbo.lu@nxp.com \
    --cc=yhs@fb.com \
    --cc=yuehaibing@huawei.com \
    --cc=zhengyongjun3@huawei.com \
    --subject='Re: [PATCH ethtool-next 2/5] stats: factor out one stat field printing' \
    /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

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).