[PATCH ethtool-next 3/5] stats: add support for per-channel statistics [blocks]

From: Alexander Lobakin
Date: Tue Aug 03 2021 - 12:52:22 EST


Treat ETHTOOL_A_STATS_GRP_STAT_BLOCK as a block of the standard
stats for one channel and print them prefixed with "channel%u-".
The index will be started from 0 and incremented automatically
for each new attr of that type.
This means that stats blocks should follow each other one by one
according to their channel number, otherwise the output can mess
up with the indices.

Signed-off-by: Alexander Lobakin <alexandr.lobakin@xxxxxxxxx>
Reviewed-by: Jesse Brandeburg <jesse.brandeburg@xxxxxxxxx>
---
netlink/stats.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 63 insertions(+), 4 deletions(-)

diff --git a/netlink/stats.c b/netlink/stats.c
index 9d950b77d656..36a9dad97f15 100644
--- a/netlink/stats.c
+++ b/netlink/stats.c
@@ -87,8 +87,8 @@ err_close_rmon:
return 1;
}

-static int parse_stat(const struct nlattr *attr, const char *grp_name,
- const struct stringset *stat_str)
+static int parse_stat(const struct nlattr *attr, const char *ch_name,
+ const char *grp_name, const struct stringset *stat_str)
{
const struct nlattr *stat;
unsigned long long val;
@@ -108,8 +108,12 @@ static int parse_stat(const struct nlattr *attr, const char *grp_name,
if (!name || !name[0])
return 0;

- if (!is_json_context())
+ if (!is_json_context()) {
+ if (ch_name)
+ fprintf(stdout, "%s-", ch_name);
+
fprintf(stdout, "%s-%s: ", grp_name, name);
+ }

val = mnl_attr_get_u64(stat);
print_u64(PRINT_ANY, name, "%llu\n", val);
@@ -117,12 +121,62 @@ static int parse_stat(const struct nlattr *attr, const char *grp_name,
return 0;
}

+static int parse_one_block(const struct nlattr *block, const char *grp_name,
+ const struct stringset *stat_str,
+ unsigned int channel)
+{
+ char ch_name[ETH_GSTRING_LEN];
+ const struct nlattr *attr;
+
+ snprintf(ch_name, sizeof(ch_name), "channel%u", channel);
+ open_json_object(ch_name);
+
+ mnl_attr_for_each_nested(attr, block) {
+ if (mnl_attr_get_type(attr) != ETHTOOL_A_STATS_GRP_STAT ||
+ parse_stat(attr, ch_name, grp_name, stat_str))
+ goto err_close_block;
+ }
+
+ close_json_object();
+
+ return 0;
+
+err_close_block:
+ close_json_object();
+
+ return 1;
+}
+
+static int parse_blocks(const struct nlattr *grp, const char *grp_name,
+ const struct stringset *stat_str)
+{
+ const struct nlattr *attr;
+ unsigned int channel = 0;
+
+ open_json_array("per-channel", "");
+
+ mnl_attr_for_each_nested(attr, grp) {
+ if (mnl_attr_get_type(attr) == ETHTOOL_A_STATS_GRP_STAT_BLOCK &&
+ parse_one_block(attr, grp_name, stat_str, channel++))
+ goto err_close_block;
+ }
+
+ close_json_array("");
+
+ return 0;
+
+err_close_block:
+ close_json_array("");
+
+ return 1;
+}
+
static int parse_grp(struct nl_context *nlctx, const struct nlattr *grp,
const struct stringset *std_str)
{
const struct nlattr *tb[ETHTOOL_A_STATS_GRP_SS_ID + 1] = {};
DECLARE_ATTR_TB_INFO(tb);
- bool hist_rx = false, hist_tx = false;
+ bool hist_rx = false, hist_tx = false, blocks = false;
const struct stringset *stat_str;
const struct nlattr *attr;
unsigned int ss_id, id;
@@ -156,6 +210,9 @@ static int parse_grp(struct nl_context *nlctx, const struct nlattr *grp,
case ETHTOOL_A_STATS_GRP_HIST_TX:
hist_tx = true;
continue;
+ case ETHTOOL_A_STATS_GRP_STAT_BLOCK:
+ blocks = true;
+ continue;
default:
continue;
}
@@ -170,6 +227,8 @@ static int parse_grp(struct nl_context *nlctx, const struct nlattr *grp,
if (hist_tx)
parse_rmon_hist(grp, std_name, "tx-pktsNtoM", "tx",
ETHTOOL_A_STATS_GRP_HIST_TX);
+ if (blocks)
+ parse_blocks(grp, std_name, stat_str);

close_json_object();

--
2.31.1