diff mbox series

[ethtool-next,2/2] netlink: tsinfo: add statistics support

Message ID 20240416203723.104062-3-rrameshbabu@nvidia.com (mailing list archive)
State Superseded
Delegated to: Michal Kubecek
Headers show
Series Userspace code for ethtool HW TS statistics | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Rahul Rameshbabu April 16, 2024, 8:37 p.m. UTC
If stats flag is present, report back statistics for tsinfo if the netlink
response body contains statistics information.

Signed-off-by: Rahul Rameshbabu <rrameshbabu@nvidia.com>
Reviewed-by: Carolina Jubran <cjubran@nvidia.com>
Reviewed-by: Cosmin Ratiu <cratiu@nvidia.com>
---
 netlink/tsinfo.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 65 insertions(+), 1 deletion(-)

Comments

Jacob Keller April 16, 2024, 10:08 p.m. UTC | #1
> -----Original Message-----
> From: Rahul Rameshbabu <rrameshbabu@nvidia.com>
> Sent: Tuesday, April 16, 2024 1:37 PM
> To: netdev@vger.kernel.org
> Cc: Vadim Fedorenko <vadim.fedorenko@linux.dev>; Keller, Jacob E
> <jacob.e.keller@intel.com>; Paolo Abeni <pabeni@redhat.com>; Jakub Kicinski
> <kuba@kernel.org>; David S. Miller <davem@davemloft.net>; Eric Dumazet
> <edumazet@google.com>; Gal Pressman <gal@nvidia.com>; Tariq Toukan
> <tariqt@nvidia.com>; M, Saeed <saeedm@nvidia.com>; Carolina Jubran
> <cjubran@nvidia.com>; Cosmin Ratiu <cratiu@nvidia.com>; Michal Kubecek
> <mkubecek@suse.cz>; Rahul Rameshbabu <rrameshbabu@nvidia.com>
> Subject: [PATCH ethtool-next 2/2] netlink: tsinfo: add statistics support
> 
> If stats flag is present, report back statistics for tsinfo if the netlink
> response body contains statistics information.
> 
> Signed-off-by: Rahul Rameshbabu <rrameshbabu@nvidia.com>
> Reviewed-by: Carolina Jubran <cjubran@nvidia.com>
> Reviewed-by: Cosmin Ratiu <cratiu@nvidia.com>

Reviewed-by: Jacob Keller <jacob.e.keller@intel.com>

Thanks! I'm hoping I can get to the Intel drivers soon.
Jakub Kicinski April 17, 2024, 12:07 a.m. UTC | #2
On Tue, 16 Apr 2024 13:37:17 -0700 Rahul Rameshbabu wrote:
> +		if (mnl_attr_validate(tb[stats[i].attr], MNL_TYPE_U32)) {
> +			is_u64 = true;
> +			if (mnl_attr_validate(tb[stats[i].attr], MNL_TYPE_U64)) {
> +				fprintf(stderr, "malformed netlink message (statistic)\n");
> +				goto err_close_stats;
> +			}
> +		}

possibly cleaner:

	__u64 val;

	if (!mnl_attr_validate(tb[stats[i].attr], MNL_TYPE_U32)) {
		val = mnl_attr_get_u32(tb[stats[i].attr]);
	} else if (!mnl_attr_validate(tb[stats[i].attr], MNL_TYPE_U64)) {
		val = mnl_attr_get_u64(tb[stats[i].attr]);
	} else {
		fprintf(stderr, "malformed netlink message (statistic)\n");
		goto err_close_stats;
	}
Rahul Rameshbabu April 17, 2024, 1:25 a.m. UTC | #3
On Tue, 16 Apr, 2024 17:07:42 -0700 Jakub Kicinski <kuba@kernel.org> wrote:
> On Tue, 16 Apr 2024 13:37:17 -0700 Rahul Rameshbabu wrote:
>> +		if (mnl_attr_validate(tb[stats[i].attr], MNL_TYPE_U32)) {
>> +			is_u64 = true;
>> +			if (mnl_attr_validate(tb[stats[i].attr], MNL_TYPE_U64)) {
>> +				fprintf(stderr, "malformed netlink message (statistic)\n");
>> +				goto err_close_stats;
>> +			}
>> +		}
>
> possibly cleaner:
>
> 	__u64 val;
>
> 	if (!mnl_attr_validate(tb[stats[i].attr], MNL_TYPE_U32)) {
> 		val = mnl_attr_get_u32(tb[stats[i].attr]);
> 	} else if (!mnl_attr_validate(tb[stats[i].attr], MNL_TYPE_U64)) {
> 		val = mnl_attr_get_u64(tb[stats[i].attr]);
> 	} else {
> 		fprintf(stderr, "malformed netlink message (statistic)\n");
> 		goto err_close_stats;
> 	}

I think this refactor is nice as well. Will wait 24 hours before
reposting but will send out a v2.

--
Thanks,

Rahul Rameshbabu
diff mbox series

Patch

diff --git a/netlink/tsinfo.c b/netlink/tsinfo.c
index c6571ff..ef5985a 100644
--- a/netlink/tsinfo.c
+++ b/netlink/tsinfo.c
@@ -5,6 +5,7 @@ 
  */
 
 #include <errno.h>
+#include <inttypes.h>
 #include <string.h>
 #include <stdio.h>
 
@@ -15,6 +16,61 @@ 
 
 /* TSINFO_GET */
 
+static int tsinfo_show_stats(const struct nlattr *nest)
+{
+	const struct nlattr *tb[ETHTOOL_A_TS_STAT_MAX + 1] = {};
+	DECLARE_ATTR_TB_INFO(tb);
+	static const struct {
+		unsigned int attr;
+		char *name;
+	} stats[] = {
+		{ ETHTOOL_A_TS_STAT_TX_PKTS, "tx_pkts" },
+		{ ETHTOOL_A_TS_STAT_TX_LOST, "tx_lost" },
+		{ ETHTOOL_A_TS_STAT_TX_ERR, "tx_err" },
+	};
+	bool header = false;
+	bool is_u64 = false;
+	unsigned int i;
+	int ret;
+
+	ret = mnl_attr_parse_nested(nest, attr_cb, &tb_info);
+	if (ret < 0)
+		return ret;
+
+	open_json_object("statistics");
+	for (i = 0; i < ARRAY_SIZE(stats); i++) {
+		char fmt[64];
+
+		if (!tb[stats[i].attr])
+			continue;
+
+		if (!header && !is_json_context()) {
+			printf("Statistics:\n");
+			header = true;
+		}
+
+		if (mnl_attr_validate(tb[stats[i].attr], MNL_TYPE_U32)) {
+			is_u64 = true;
+			if (mnl_attr_validate(tb[stats[i].attr], MNL_TYPE_U64)) {
+				fprintf(stderr, "malformed netlink message (statistic)\n");
+				goto err_close_stats;
+			}
+		}
+
+		snprintf(fmt, sizeof(fmt), "  %s: %%" PRIu64 "\n", stats[i].name);
+		print_u64(PRINT_ANY, stats[i].name, fmt,
+			  is_u64 ? mnl_attr_get_u64(tb[stats[i].attr]) :
+			  mnl_attr_get_u32(tb[stats[i].attr]));
+	}
+	close_json_object();
+
+	return 0;
+
+err_close_stats:
+	close_json_object();
+	return -1;
+}
+
 static void tsinfo_dump_cb(unsigned int idx, const char *name, bool val,
 			   void *data __maybe_unused)
 {
@@ -99,6 +155,12 @@  int tsinfo_reply_cb(const struct nlmsghdr *nlhdr, void *data)
 	if (ret < 0)
 		return err_ret;
 
+	if (tb[ETHTOOL_A_TSINFO_STATS]) {
+		ret = tsinfo_show_stats(tb[ETHTOOL_A_TSINFO_STATS]);
+		if (ret < 0)
+			return err_ret;
+	}
+
 	return MNL_CB_OK;
 }
 
@@ -106,6 +168,7 @@  int nl_tsinfo(struct cmd_context *ctx)
 {
 	struct nl_context *nlctx = ctx->nlctx;
 	struct nl_socket *nlsk = nlctx->ethnl_socket;
+	u32 flags;
 	int ret;
 
 	if (netlink_cmd_check(ctx, ETHTOOL_MSG_TSINFO_GET, true))
@@ -116,8 +179,9 @@  int nl_tsinfo(struct cmd_context *ctx)
 		return 1;
 	}
 
+	flags = get_stats_flag(nlctx, ETHTOOL_MSG_TSINFO_GET, ETHTOOL_A_TSINFO_HEADER);
 	ret = nlsock_prep_get_request(nlsk, ETHTOOL_MSG_TSINFO_GET,
-				      ETHTOOL_A_TSINFO_HEADER, 0);
+				      ETHTOOL_A_TSINFO_HEADER, flags);
 	if (ret < 0)
 		return ret;
 	return nlsock_send_get_request(nlsk, tsinfo_reply_cb);