Skip to contentSkip to conversation

pause frame stats

PATCH Superseded Jakub Kicinski 3 participants 12 messages Sep 15, 2020 – Sep 24, 2020

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

cover pause frame stats

Hi!

This set adds support for pause frame statistics.

This is the first of this kind of statistics so first support for a new flag (--include-statistics) is added. Next pause frame info is extended to support --json. Last but not least - it's taught to display statistics.

Jakub Kicinski (5):
update UAPI header copies
pause: add --json support
separate FLAGS out in -h
add support for stats in subcommands
pause: add support for dumping statistics

 ethtool.8.in                 |   7 +++
 ethtool.c                    |  17 +++++-
 internal.h                   |   1 +
 netlink/netlink.h            |  12 +++-
 netlink/pause.c              | 111 +++++++++++++++++++++++++++++++----
 uapi/linux/ethtool_netlink.h |  18 +++++-
 6 files changed, 149 insertions(+), 17 deletions(-)
Signature · 1 line
2.26.2
author maintainer
#1 lore raw

PATCH 1/5 update UAPI header copies

Update to kernel commit 945c5704887e.

---
 uapi/linux/ethtool_netlink.h | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
uapi/linux/ethtool_netlink.h +17 -1
index cebdb52e6a05..8d3926c56769 100644
@@ -91,9 +91,12 @@ enum {
 #define ETHTOOL_FLAG_COMPACT_BITSETS	(1 << 0)
 /* provide optional reply for SET or ACT requests */
 #define ETHTOOL_FLAG_OMIT_REPLY	(1 << 1)
+/* request statistics, if supported by the driver */
+#define ETHTOOL_FLAG_STATS		(1 << 2)
 
 #define ETHTOOL_FLAG_ALL (ETHTOOL_FLAG_COMPACT_BITSETS | \
-			  ETHTOOL_FLAG_OMIT_REPLY)
+			  ETHTOOL_FLAG_OMIT_REPLY | \
+			  ETHTOOL_FLAG_STATS)
 
 enum {
 	ETHTOOL_A_HEADER_UNSPEC,
@@ -376,12 +379,25 @@ enum {
 	ETHTOOL_A_PAUSE_AUTONEG,			/* u8 */
 	ETHTOOL_A_PAUSE_RX,				/* u8 */
 	ETHTOOL_A_PAUSE_TX,				/* u8 */
+	ETHTOOL_A_PAUSE_STATS,				/* nest - _PAUSE_STAT_* */
 
 	/* add new constants above here */
 	__ETHTOOL_A_PAUSE_CNT,
 	ETHTOOL_A_PAUSE_MAX = (__ETHTOOL_A_PAUSE_CNT - 1)
 };
 
+enum {
+	ETHTOOL_A_PAUSE_STAT_UNSPEC,
+	ETHTOOL_A_PAUSE_STAT_PAD,
+
+	ETHTOOL_A_PAUSE_STAT_TX_FRAMES,
+	ETHTOOL_A_PAUSE_STAT_RX_FRAMES,
+
+	/* add new constants above here */
+	__ETHTOOL_A_PAUSE_STAT_CNT,
+	ETHTOOL_A_PAUSE_STAT_MAX = (__ETHTOOL_A_PAUSE_STAT_CNT - 1)
+};
+
 /* EEE */
 
 enum {
Signature · 1 line
2.26.2
author maintainer
#2 lore raw

PATCH 2/5 pause: add --json support

In reply to Jakub Kicinski

No change in normal text output:

 # ./ethtool  -a eth0
Pause parameters for eth0:
Autonegotiate:	on
RX:		on
TX:		on
RX negotiated: on
TX negotiated: on

JSON:

 # ./ethtool --json -a eth0
[ {
        "ifname": "eth0",
        "autonegotiate": true,
        "rx": true,
        "tx": true,
        "negotiated": {
            "rx": true,
            "tx": true
        }
    } ]
---
 netlink/netlink.h | 12 ++++++++++--
 netlink/pause.c   | 45 +++++++++++++++++++++++++++++++++++----------
 2 files changed, 45 insertions(+), 12 deletions(-)
netlink/netlink.h +10 -2
index dd4a02bcc916..4916d25ed5c0 100644
@@ -106,9 +106,17 @@ static inline const char *u8_to_bool(const struct nlattr *attr)
 		return "n/a";
 }
 
-static inline void show_bool(const struct nlattr *attr, const char *label)
+static inline void show_bool(const char *key, const char *fmt,
+			     const struct nlattr *attr)
 {
-	printf("%s%s\n", label, u8_to_bool(attr));
+	if (is_json_context()) {
+		if (attr) {
+			print_bool(PRINT_JSON, key, NULL,
+				   mnl_attr_get_u8(attr));
+		}
+	} else {
+		print_string(PRINT_FP, NULL, fmt, u8_to_bool(attr));
+	}
 }
 
 /* misc */
netlink/pause.c +35 -10

71 lines not on this page · raw message

Signature · 1 line
2.26.2
Reviewed-by on v1 2/5
#3 lore raw

Re: [PATCH ethtool-next 2/5] pause: add --json support

Quoted 12 lines · Jakub Kicinski

No change in normal text output:

 # ./ethtool  -a eth0
Pause parameters for eth0:
Autonegotiate:	on
RX:		on
TX:		on
RX negotiated: on
TX negotiated: on

JSON:

Quoted 11 lines
 # ./ethtool --json -a eth0
[ {
        "ifname": "eth0",
        "autonegotiate": true,
        "rx": true,
        "tx": true,
        "negotiated": {
            "rx": true,
            "tx": true
        }
    } ]

Makes sense, we report all of these as a single dictionary with key-values. Putting negotiated as a subsection seems reasonable as well. I guess we could split this so that tx and rx have their own section. but I think this is good.

This looks good to me.

Quoted 17 lines
---
 netlink/netlink.h | 12 ++++++++++--
 netlink/pause.c   | 45 +++++++++++++++++++++++++++++++++++----------
 2 files changed, 45 insertions(+), 12 deletions(-)
netlink/netlink.h +1 -1
index dd4a02bcc916..4916d25ed5c0 100644
@@ -106,9 +106,17 @@ static inline const char *u8_to_bool(const struct nlattr *attr)
 		return "n/a";
 }
 
-static inline void show_bool(const struct nlattr *attr, const char *label)
+static inline void show_bool(const char *key, const char *fmt,

netlink/netlink.h static inline const char *u8_to_bool(const struct nlattr *attr) · line 118

Quoted 10 lines
netlink/netlink.h +8 -1
+			     const struct nlattr *attr)
 {
-	printf("%s%s\n", label, u8_to_bool(attr));
+	if (is_json_context()) {
+		if (attr) {
+			print_bool(PRINT_JSON, key, NULL,
+				   mnl_attr_get_u8(attr));
+		}
+	} else {
+		print_string(PRINT_FP, NULL, fmt, u8_to_bool(attr));

Ok. The change from label to fmt makes sense because we now pass a format string instead of a label, and we use print_string instead of printf.

Quoted 15 lines
netlink/netlink.h +1 -0
+	}
 }
 
 /* misc */
netlink/pause.c +1 -2
index 7b6b3a1d2c10..30ecdccb15eb 100644
@@ -72,8 +72,16 @@ static int pause_autoneg_cb(const struct nlmsghdr *nlhdr, void *data)
 		else if (peer.pause)
 			tx_status = true;
 	}
-	printf("RX negotiated: %s\nTX negotiated: %s\n",
-	       rx_status ? "on" : "off", tx_status ? "on" : "off");
+

netlink/pause.c static int pause_autoneg_cb(const struct nlmsghdr *nlhdr, void *data) · line 85

Quoted 10 lines
netlink/pause.c +9 -0
+	if (is_json_context()) {
+		open_json_object("negotiated");
+		print_bool(PRINT_JSON, "rx", NULL, rx_status);
+		print_bool(PRINT_JSON, "tx", NULL, tx_status);
+		close_json_object();
+	} else {
+		printf("RX negotiated: %s\nTX negotiated: %s\n",
+		       rx_status ? "on" : "off", tx_status ? "on" : "off");
+	}
 

Why not use print_string here like show_bool did?

Quoted 8 lines
netlink/pause.c +0 -2
 	return MNL_CB_OK;
 }
@@ -121,21 +129,34 @@ int pause_reply_cb(const struct nlmsghdr *nlhdr, void *data)
 		return err_ret;
 
 	if (silent)
-		putchar('\n');
-	printf("Pause parameters for %s:\n", nlctx->devname);

netlink/pause.c int pause_reply_cb(const struct nlmsghdr *nlhdr, void *data) · line 138

Quoted 10 lines
netlink/pause.c +7 -3
-	show_bool(tb[ETHTOOL_A_PAUSE_AUTONEG], "Autonegotiate:\t");
-	show_bool(tb[ETHTOOL_A_PAUSE_RX], "RX:\t\t");
-	show_bool(tb[ETHTOOL_A_PAUSE_TX], "TX:\t\t");
+		print_nl();
+
+	open_json_object(NULL);
+
+	print_string(PRINT_ANY, "ifname", "Pause parameters for %s:\n",
+		     nlctx->devname);
+

Ah, nice, so this prints based on the current format and will just print the ifname instead of showing the format string. Neat.

Quoted 40 lines
netlink/pause.c +18 -3
+	show_bool("autonegotiate", "Autonegotiate:\t%s\n",
+		  tb[ETHTOOL_A_PAUSE_AUTONEG]);
+	show_bool("rx", "RX:\t\t%s\n", tb[ETHTOOL_A_PAUSE_RX]);
+	show_bool("tx", "TX:\t\t%s\n", tb[ETHTOOL_A_PAUSE_TX]);
+
 	if (!nlctx->is_monitor && tb[ETHTOOL_A_PAUSE_AUTONEG] &&
 	    mnl_attr_get_u8(tb[ETHTOOL_A_PAUSE_AUTONEG])) {
 		ret = show_pause_autoneg_status(nlctx);
 		if (ret < 0)
-			return err_ret;
+			goto err_close_dev;
 	}
 	if (!silent)
-		putchar('\n');
+		print_nl();
+
+	close_json_object();
 
 	return MNL_CB_OK;
+
+err_close_dev:
+	close_json_object();
+	return err_ret;
 }
 
 int nl_gpause(struct cmd_context *ctx)
@@ -156,7 +177,11 @@ int nl_gpause(struct cmd_context *ctx)
 				      ETHTOOL_A_PAUSE_HEADER, 0);
 	if (ret < 0)
 		return ret;
-	return nlsock_send_get_request(nlsk, pause_reply_cb);
+
+	new_json_obj(ctx->json);
+	ret = nlsock_send_get_request(nlsk, pause_reply_cb);
+	delete_json_obj();
+	return ret;
 }
 
 /* PAUSE_SET */
author maintainer
#4 lore raw

Re: [PATCH ethtool-next 2/5] pause: add --json support

Quoted 6 lines · On Wed, 23 Sep 2020 17:10:30 -0700 Jacob Keller wrote:
Quoted 6 lines
(quoted diff) +4 -2
-	printf("RX negotiated: %s\nTX negotiated: %s\n",
-	       rx_status ? "on" : "off", tx_status ? "on" : "off");
+
+	if (is_json_context()) {
+		open_json_object("negotiated");
+		print_bool(PRINT_JSON, "rx", NULL, rx_status);
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+		print_bool(PRINT_JSON, "tx", NULL, tx_status);
+		close_json_object();
+	} else {
+		printf("RX negotiated: %s\nTX negotiated: %s\n",
+		       rx_status ? "on" : "off", tx_status ? "on" : "off");
+	}

Why not use print_string here like show_bool did?

Yeah.. I did not come up with a good way of reusing the show_bool code so I gave up. Taking another swing at it - how does this look?

netlink/coalesce.c +3 -3
index 65f75cf9a8dd..07a92d04b7a1 100644
@@ -36,9 +36,9 @@ int coalesce_reply_cb(const struct nlmsghdr *nlhdr, void *data)
 	if (silent)
 		putchar('\n');
 	printf("Coalesce parameters for %s:\n", nlctx->devname);
-	printf("Adaptive RX: %s  TX: %s\n",
-	       u8_to_bool(tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_RX]),
-	       u8_to_bool(tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_TX]));
+	show_bool("rx", "Adaptive RX: %s  ",
+		  tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_RX]);
+	show_bool("tx", "TX: %s\n", tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_TX]);
 	show_u32(tb[ETHTOOL_A_COALESCE_STATS_BLOCK_USECS],
 		 "stats-block-usecs: ");
 	show_u32(tb[ETHTOOL_A_COALESCE_RATE_SAMPLE_INTERVAL],
netlink/netlink.h +13 -10
index 4916d25ed5c0..1012e8e32cd8 100644
@@ -98,27 +98,30 @@ static inline void show_u32(const struct nlattr *attr, const char *label)
 		printf("%sn/a\n", label);
 }
 
-static inline const char *u8_to_bool(const struct nlattr *attr)
+static inline const char *u8_to_bool(const uint8_t *val)
 {
-	if (attr)
-		return mnl_attr_get_u8(attr) ? "on" : "off";
+	if (val)
+		return *val ? "on" : "off";
 	else
 		return "n/a";
 }
 
-static inline void show_bool(const char *key, const char *fmt,
-			     const struct nlattr *attr)
+static inline void show_bool_val(const char *key, const char *fmt, uint8_t *val)
 {
 	if (is_json_context()) {
-		if (attr) {
-			print_bool(PRINT_JSON, key, NULL,
-				   mnl_attr_get_u8(attr));
-		}
+		if (val)
+			print_bool(PRINT_JSON, key, NULL, val);
 	} else {
-		print_string(PRINT_FP, NULL, fmt, u8_to_bool(attr));
+		print_string(PRINT_FP, NULL, fmt, u8_to_bool(val));
 	}
 }
 
+static inline void show_bool(const char *key, const char *fmt,
+			     const struct nlattr *attr)
+{
+	show_bool_val(key, fmt, attr ? mnl_attr_get_payload(attr) : NULL);
+}
+
 /* misc */
 
 static inline void copy_devname(char *dst, const char *src)
netlink/pause.c +6 -11
index f9dec9fe887a..5395398ba948 100644
@@ -41,8 +41,8 @@ static int pause_autoneg_cb(const struct nlmsghdr *nlhdr, void *data)
 	struct pause_autoneg_status ours = {};
 	struct pause_autoneg_status peer = {};
 	struct nl_context *nlctx = data;
-	bool rx_status = false;
-	bool tx_status = false;
+	uint8_t rx_status = false;
+	uint8_t tx_status = false;
 	bool silent;
 	int err_ret;
 	int ret;
@@ -74,15 +74,10 @@ static int pause_autoneg_cb(const struct nlmsghdr *nlhdr, void *data)
 			tx_status = true;
 	}
 
-	if (is_json_context()) {
-		open_json_object("negotiated");
-		print_bool(PRINT_JSON, "rx", NULL, rx_status);
-		print_bool(PRINT_JSON, "tx", NULL, tx_status);
-		close_json_object();
-	} else {
-		printf("RX negotiated: %s\nTX negotiated: %s\n",
-		       rx_status ? "on" : "off", tx_status ? "on" : "off");
-	}
+	open_json_object("negotiated");
+	show_bool_val("rx", "RX negotiated: %s\n", &rx_status);
+	show_bool_val("tx", "TX negotiated: %s\n", &tx_status);
+	close_json_object();
 
 	return MNL_CB_OK;
 }
#5 lore raw

Re: [PATCH ethtool-next 2/5] pause: add --json support

Quoted 107 lines · Jakub Kicinski
Quoted 6 lines · On Wed, 23 Sep 2020 17:10:30 -0700 Jacob Keller wrote:
Quoted 6 lines
netlink/pause.c +4 -2
-	printf("RX negotiated: %s\nTX negotiated: %s\n",
-	       rx_status ? "on" : "off", tx_status ? "on" : "off");
+
+	if (is_json_context()) {
+		open_json_object("negotiated");
+		print_bool(PRINT_JSON, "rx", NULL, rx_status);
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+		print_bool(PRINT_JSON, "tx", NULL, tx_status);
+		close_json_object();
+	} else {
+		printf("RX negotiated: %s\nTX negotiated: %s\n",
+		       rx_status ? "on" : "off", tx_status ? "on" : "off");
+	}

Why not use print_string here like show_bool did?

Yeah.. I did not come up with a good way of reusing the show_bool code so I gave up. Taking another swing at it - how does this look?

netlink/coalesce.c +3 -3
index 65f75cf9a8dd..07a92d04b7a1 100644
@@ -36,9 +36,9 @@ int coalesce_reply_cb(const struct nlmsghdr *nlhdr, void *data)
 	if (silent)
 		putchar('\n');
 	printf("Coalesce parameters for %s:\n", nlctx->devname);
-	printf("Adaptive RX: %s  TX: %s\n",
-	       u8_to_bool(tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_RX]),
-	       u8_to_bool(tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_TX]));
+	show_bool("rx", "Adaptive RX: %s  ",
+		  tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_RX]);
+	show_bool("tx", "TX: %s\n", tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_TX]);
 	show_u32(tb[ETHTOOL_A_COALESCE_STATS_BLOCK_USECS],
 		 "stats-block-usecs: ");
 	show_u32(tb[ETHTOOL_A_COALESCE_RATE_SAMPLE_INTERVAL],
netlink/netlink.h +13 -10
index 4916d25ed5c0..1012e8e32cd8 100644
@@ -98,27 +98,30 @@ static inline void show_u32(const struct nlattr *attr, const char *label)
 		printf("%sn/a\n", label);
 }
 
-static inline const char *u8_to_bool(const struct nlattr *attr)
+static inline const char *u8_to_bool(const uint8_t *val)
 {
-	if (attr)
-		return mnl_attr_get_u8(attr) ? "on" : "off";
+	if (val)
+		return *val ? "on" : "off";
 	else
 		return "n/a";
 }
 
-static inline void show_bool(const char *key, const char *fmt,
-			     const struct nlattr *attr)
+static inline void show_bool_val(const char *key, const char *fmt, uint8_t *val)
 {
 	if (is_json_context()) {
-		if (attr) {
-			print_bool(PRINT_JSON, key, NULL,
-				   mnl_attr_get_u8(attr));
-		}
+		if (val)
+			print_bool(PRINT_JSON, key, NULL, val);
 	} else {
-		print_string(PRINT_FP, NULL, fmt, u8_to_bool(attr));
+		print_string(PRINT_FP, NULL, fmt, u8_to_bool(val));
 	}
 }
 
+static inline void show_bool(const char *key, const char *fmt,
+			     const struct nlattr *attr)
+{
+	show_bool_val(key, fmt, attr ? mnl_attr_get_payload(attr) : NULL);
+}
+
 /* misc */
 
 static inline void copy_devname(char *dst, const char *src)
netlink/pause.c +2 -8
index f9dec9fe887a..5395398ba948 100644
@@ -41,8 +41,8 @@ static int pause_autoneg_cb(const struct nlmsghdr *nlhdr, void *data)
 	struct pause_autoneg_status ours = {};
 	struct pause_autoneg_status peer = {};
 	struct nl_context *nlctx = data;
-	bool rx_status = false;
-	bool tx_status = false;
+	uint8_t rx_status = false;
+	uint8_t tx_status = false;
 	bool silent;
 	int err_ret;
 	int ret;
@@ -74,15 +74,10 @@ static int pause_autoneg_cb(const struct nlmsghdr *nlhdr, void *data)
 			tx_status = true;
 	}
 
-	if (is_json_context()) {
-		open_json_object("negotiated");
-		print_bool(PRINT_JSON, "rx", NULL, rx_status);
-		print_bool(PRINT_JSON, "tx", NULL, tx_status);
-		close_json_object();
-	} else {

netlink/pause.c static int pause_autoneg_cb(const struct nlmsghdr *nlhdr, void *data) · line 83

Quoted 10 lines
netlink/pause.c +4 -3
-		printf("RX negotiated: %s\nTX negotiated: %s\n",
-		       rx_status ? "on" : "off", tx_status ? "on" : "off");
-	}
+	open_json_object("negotiated");
+	show_bool_val("rx", "RX negotiated: %s\n", &rx_status);
+	show_bool_val("tx", "TX negotiated: %s\n", &tx_status);
+	close_json_object();
 
 	return MNL_CB_OK;
 }

This looks good!

Thanks,
Jake

author maintainer
#6 lore raw

PATCH 3/5 separate FLAGS out in -h

In reply to Jakub Kicinski

Help output is quite crowded already with every command being prefixed by --debug and --json options, and we're about to add a third one.

Add an indirection.

---
 ethtool.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
ethtool.c +6 -3
index 23ecfcfd069c..ae5310e9e306 100644
@@ -5947,10 +5947,10 @@ static int show_usage(struct cmd_context *ctx __maybe_unused)
 	fprintf(stdout, PACKAGE " version " VERSION "\n");
 	fprintf(stdout,
 		"Usage:\n"
-		"        ethtool [ --debug MASK ][ --json ] DEVNAME\t"
+		"        ethtool [ FLAGS ] DEVNAME\t"
 		"Display standard information about device\n");
 	for (i = 0; args[i].opts; i++) {
-		fputs("        ethtool [ --debug MASK ][ --json ] ", stdout);
+		fputs("        ethtool [ FLAGS ] ", stdout);
 		fprintf(stdout, "%s %s\t%s\n",
 			args[i].opts,
 			args[i].no_dev ? "\t" : "DEVNAME",
@@ -5959,7 +5959,10 @@ static int show_usage(struct cmd_context *ctx __maybe_unused)
 			fputs(args[i].xhelp, stdout);
 	}
 	nl_monitor_usage();
-	fprintf(stdout, "Not all options support JSON output\n");
+	fprintf(stdout, "\n");
+	fprintf(stdout, "FLAGS:\n");
+	fprintf(stdout, "	--debug MASK	turn on debugging messages\n");
+	fprintf(stdout, "	--json		enable JSON output format (not supported by all commands)\n");
 
 	return 0;
 }
Signature · 1 line
2.26.2
Reviewed-by on v1 3/5
#7 lore raw

Re: [PATCH ethtool-next 3/5] separate FLAGS out in -h

Quoted 7 lines · Jakub Kicinski

Help output is quite crowded already with every command being prefixed by --debug and --json options, and we're about to add a third one.

Add an indirection.

Seems reasonable to me.

Quoted 22 lines
---
 ethtool.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
ethtool.c +2 -2
index 23ecfcfd069c..ae5310e9e306 100644
@@ -5947,10 +5947,10 @@ static int show_usage(struct cmd_context *ctx __maybe_unused)
 	fprintf(stdout, PACKAGE " version " VERSION "\n");
 	fprintf(stdout,
 		"Usage:\n"
-		"        ethtool [ --debug MASK ][ --json ] DEVNAME\t"
+		"        ethtool [ FLAGS ] DEVNAME\t"
 		"Display standard information about device\n");
 	for (i = 0; args[i].opts; i++) {
-		fputs("        ethtool [ --debug MASK ][ --json ] ", stdout);
+		fputs("        ethtool [ FLAGS ] ", stdout);
 		fprintf(stdout, "%s %s\t%s\n",
 			args[i].opts,
 			args[i].no_dev ? "\t" : "DEVNAME",

ethtool.c static int show_usage(struct cmd_context *ctx __maybe_unused) · line 5966

Quoted 9 lines
ethtool.c +4 -1
@@ -5959,7 +5959,10 @@ static int show_usage(struct cmd_context *ctx __maybe_unused)
 			fputs(args[i].xhelp, stdout);
 	}
 	nl_monitor_usage();
-	fprintf(stdout, "Not all options support JSON output\n");
+	fprintf(stdout, "\n");
+	fprintf(stdout, "FLAGS:\n");
+	fprintf(stdout, "	--debug MASK	turn on debugging messages\n");
+	fprintf(stdout, "	--json		enable JSON output format (not supported by all commands)\n");
 

Not really related to this patch, but what is the behavior of a command when --json is used but it doesn't support it?

Thanks,
Jake

Quoted 3 lines
ethtool.c +0 -0
 	return 0;
 }
author maintainer
#8 lore raw

PATCH 4/5 add support for stats in subcommands

In reply to Jakub Kicinski

Add new parameters (-I | --include-statistics) which will control requesting statistic dumps from the kernel.

---
 ethtool.8.in | 7 +++++++
 ethtool.c    | 8 ++++++++
 internal.h   | 1 +
 3 files changed, 16 insertions(+)
ethtool.8.in +7 -0
index 42c4767db33e..00cf7870376c 100644
@@ -140,6 +140,9 @@ ethtool \- query or control network driver and hardware settings
 .B ethtool [--json]
 .I args
 .HP
+.B ethtool [-I | --include-statistics]
+.I args
+.HP
 .B ethtool \-\-monitor
 [
 .I command
@@ -499,6 +502,10 @@ Output results in JavaScript Object Notation (JSON). Only a subset of
 options support this. Those which do not will continue to output
 plain text in the presence of this option.
 .TP
+.B \-I \-\-include\-statistics
+Include command-related statistics in the output. This option allows
+displaying relevant device statistics for selected get commands.
+.TP
 .B \-a \-\-show\-pause
 Queries the specified Ethernet device for pause parameter information.
 .TP
ethtool.c +8 -0
index ae5310e9e306..309539579cc9 100644
@@ -5963,6 +5963,7 @@ static int show_usage(struct cmd_context *ctx __maybe_unused)
 	fprintf(stdout, "FLAGS:\n");
 	fprintf(stdout, "	--debug MASK	turn on debugging messages\n");
 	fprintf(stdout, "	--json		enable JSON output format (not supported by all commands)\n");
+	fprintf(stdout, "	-I|--include-statistics		request device statistics related to the command (not supported by all commands)\n");
 
 	return 0;
 }
@@ -6223,6 +6224,13 @@ int main(int argc, char **argp)
 			argc -= 1;
 			continue;
 		}
+		if (*argp && (!strcmp(*argp, "--include-statistics") ||
+			      !strcmp(*argp, "-I"))) {
+			ctx.show_stats = true;
+			argp += 1;
+			argc -= 1;
+			continue;
+		}
 		break;
 	}
 	if (*argp && !strcmp(*argp, "--monitor")) {
internal.h +1 -0
index d096a28abfa2..1c0652d28793 100644
@@ -225,6 +225,7 @@ struct cmd_context {
 	char **argp;		/* arguments to the sub-command */
 	unsigned long debug;	/* debugging mask */
 	bool json;		/* Output JSON, if supported */
+	bool show_stats;	/* include command-specific stats */
 #ifdef ETHTOOL_ENABLE_NETLINK
 	struct nl_context *nlctx;	/* netlink context (opaque) */
 #endif
Signature · 1 line
2.26.2
author maintainer
#9 lore raw

PATCH 5/5 pause: add support for dumping statistics

In reply to Jakub Kicinski

Add support for requesting pause frame stats from the kernel.

 # ./ethtool -I -a eth0
Pause parameters for eth0:
Autonegotiate:	on
RX:		on
TX:		on
Statistics:
  tx_pause_frames: 1
  rx_pause_frames: 1
 # ./ethtool -I --json -a eth0
[ {
        "ifname": "eth0",
        "autonegotiate": true,
        "rx": true,
        "tx": true,
        "statistics": {
            "tx_pause_frames": 1,
            "rx_pause_frames": 1
        }
    } ]
---
 netlink/pause.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 65 insertions(+), 1 deletion(-)
netlink/pause.c +65 -1
index 30ecdccb15eb..f9dec9fe887a 100644
@@ -5,6 +5,7 @@
  */
 
 #include <errno.h>
+#include <inttypes.h>
 #include <string.h>
 #include <stdio.h>
 
@@ -110,6 +111,62 @@ static int show_pause_autoneg_status(struct nl_context *nlctx)
 	return ret;
 }
 
+static int show_pause_stats(const struct nlattr *nest)
+{
+	const struct nlattr *tb[ETHTOOL_A_PAUSE_STAT_MAX + 1] = {};
+	DECLARE_ATTR_TB_INFO(tb);
+	static const struct {
+		unsigned int attr;
+		char *name;
+	} stats[] = {
+		{ ETHTOOL_A_PAUSE_STAT_TX_FRAMES, "tx_pause_frames" },
+		{ ETHTOOL_A_PAUSE_STAT_RX_FRAMES, "rx_pause_frames" },
+	};
+	bool header = false;
+	unsigned int i;
+	size_t n;
+	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[32];
+
+		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_U64)) {
+			fprintf(stderr, "malformed netlink message (statistic)\n");
+			goto err_close_stats;
+		}
+
+		n = snprintf(fmt, sizeof(fmt), "  %s: %%" PRId64 "\n",
+			     stats[i].name);
+		if (n >= sizeof(fmt)) {
+			fprintf(stderr, "internal error - malformed label\n");
+			goto err_close_stats;
+		}
+
+		print_u64(PRINT_ANY, stats[i].name, fmt,
+			  mnl_attr_get_u64(tb[stats[i].attr]));
+	}
+	close_json_object();
+
+	return 0;
+
+err_close_stats:
+	close_json_object();
+	return -1;
+}
+
 int pause_reply_cb(const struct nlmsghdr *nlhdr, void *data)
 {
 	const struct nlattr *tb[ETHTOOL_A_PAUSE_MAX + 1] = {};
@@ -147,6 +204,11 @@ int pause_reply_cb(const struct nlmsghdr *nlhdr, void *data)
 		if (ret < 0)
 			goto err_close_dev;
 	}
+	if (tb[ETHTOOL_A_PAUSE_STATS]) {
+		ret = show_pause_stats(tb[ETHTOOL_A_PAUSE_STATS]);
+		if (ret < 0)
+			goto err_close_dev;
+	}
 	if (!silent)
 		print_nl();
 
@@ -163,6 +225,7 @@ int nl_gpause(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_PAUSE_GET, true))
@@ -173,8 +236,9 @@ int nl_gpause(struct cmd_context *ctx)
 		return 1;
 	}
 
+	flags = nlctx->ctx->show_stats ? ETHTOOL_FLAG_STATS : 0;
 	ret = nlsock_prep_get_request(nlsk, ETHTOOL_MSG_PAUSE_GET,
-				      ETHTOOL_A_PAUSE_HEADER, 0);
+				      ETHTOOL_A_PAUSE_HEADER, flags);
 	if (ret < 0)
 		return ret;
 
Signature · 1 line
2.26.2
#10 lore raw

Re: [PATCH ethtool-next 5/5] pause: add support for dumping statistics

Quoted 73 lines · Jakub Kicinski

Add support for requesting pause frame stats from the kernel.

 # ./ethtool -I -a eth0
Pause parameters for eth0:
Autonegotiate:	on
RX:		on
TX:		on
Statistics:
  tx_pause_frames: 1
  rx_pause_frames: 1
 # ./ethtool -I --json -a eth0
[ {
        "ifname": "eth0",
        "autonegotiate": true,
        "rx": true,
        "tx": true,
        "statistics": {
            "tx_pause_frames": 1,
            "rx_pause_frames": 1
        }
    } ]
---
 netlink/pause.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 65 insertions(+), 1 deletion(-)
netlink/pause.c +30 -0
index 30ecdccb15eb..f9dec9fe887a 100644
@@ -5,6 +5,7 @@
  */
 
 #include <errno.h>
+#include <inttypes.h>
 #include <string.h>
 #include <stdio.h>
 
@@ -110,6 +111,62 @@ static int show_pause_autoneg_status(struct nl_context *nlctx)
 	return ret;
 }
 
+static int show_pause_stats(const struct nlattr *nest)
+{
+	const struct nlattr *tb[ETHTOOL_A_PAUSE_STAT_MAX + 1] = {};
+	DECLARE_ATTR_TB_INFO(tb);
+	static const struct {
+		unsigned int attr;
+		char *name;
+	} stats[] = {
+		{ ETHTOOL_A_PAUSE_STAT_TX_FRAMES, "tx_pause_frames" },
+		{ ETHTOOL_A_PAUSE_STAT_RX_FRAMES, "rx_pause_frames" },
+	};
+	bool header = false;
+	unsigned int i;
+	size_t n;
+	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[32];
+
+		if (!tb[stats[i].attr])
+			continue;
+
+		if (!header && !is_json_context()) {
+			printf("Statistics:\n");

netlink/pause.c static int show_pause_autoneg_status(struct nl_context *nlctx) · line 152

Quoted 10 lines
netlink/pause.c +10 -0
+			header = true;
+		}
+
+		if (mnl_attr_validate(tb[stats[i].attr], MNL_TYPE_U64)) {
+			fprintf(stderr, "malformed netlink message (statistic)\n");
+			goto err_close_stats;
+		}
+
+		n = snprintf(fmt, sizeof(fmt), "  %s: %%" PRId64 "\n",
+			     stats[i].name);

The stats are unsigned so the format should be PRIu64 here.

Quoted 41 lines
netlink/pause.c +23 -0
+		if (n >= sizeof(fmt)) {
+			fprintf(stderr, "internal error - malformed label\n");
+			goto err_close_stats;
+		}
+
+		print_u64(PRINT_ANY, stats[i].name, fmt,
+			  mnl_attr_get_u64(tb[stats[i].attr]));
+	}
+	close_json_object();
+
+	return 0;
+
+err_close_stats:
+	close_json_object();
+	return -1;
+}
+
 int pause_reply_cb(const struct nlmsghdr *nlhdr, void *data)
 {
 	const struct nlattr *tb[ETHTOOL_A_PAUSE_MAX + 1] = {};
@@ -147,6 +204,11 @@ int pause_reply_cb(const struct nlmsghdr *nlhdr, void *data)
 		if (ret < 0)
 			goto err_close_dev;
 	}
+	if (tb[ETHTOOL_A_PAUSE_STATS]) {
+		ret = show_pause_stats(tb[ETHTOOL_A_PAUSE_STATS]);
+		if (ret < 0)
+			goto err_close_dev;
+	}
 	if (!silent)
 		print_nl();
 
@@ -163,6 +225,7 @@ int nl_gpause(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_PAUSE_GET, true))

netlink/pause.c int nl_gpause(struct cmd_context *ctx) · line 244

Quoted 10 lines
netlink/pause.c +2 -1
@@ -173,8 +236,9 @@ int nl_gpause(struct cmd_context *ctx)
 		return 1;
 	}
 
+	flags = nlctx->ctx->show_stats ? ETHTOOL_FLAG_STATS : 0;
 	ret = nlsock_prep_get_request(nlsk, ETHTOOL_MSG_PAUSE_GET,
-				      ETHTOOL_A_PAUSE_HEADER, 0);
+				      ETHTOOL_A_PAUSE_HEADER, flags);
 	if (ret < 0)
 		return ret;
 

When the stats are supported by kernel but not provided by a device, the request will succeed and usual output without stats will be shown. However, when stats are requested on a pre-5.10 kernel not recognizing ETHTOOL_FLAG_STATS, the request will fail:

mike@lion:~/work/git/ethtool> ./ethtool --debug 0x10 -I -a eth0 netlink error: unrecognized request flags
netlink error: Operation not supported
offending message and attribute:
ETHTOOL_MSG_PAUSE_GET
ETHTOOL_A_PAUSE_HEADER
ETHTOOL_A_HEADER_DEV_NAME = "eth0"
===> ETHTOOL_A_HEADER_FLAGS = 0x00000004

We should probably repeat the request with flags=0 in this case but that would require keeping the offset of ETHTOOL_A_HEADER_FLAGS attribute and checking for -EOPNOTSUPP with this offset in nlsock_process_ack().

Michal

author maintainer
#11 lore raw

Re: [PATCH ethtool-next 5/5] pause: add support for dumping statistics

Quoted 9 lines · On Thu, 24 Sep 2020 00:45:10 +0200 Michal Kubecek wrote:

netlink/pause.c static int show_pause_autoneg_status(struct nl_context *nlctx) · line 152

Quoted 7 lines
netlink/pause.c +7 -0
+		if (mnl_attr_validate(tb[stats[i].attr], MNL_TYPE_U64)) {
+			fprintf(stderr, "malformed netlink message (statistic)\n");
+			goto err_close_stats;
+		}
+
+		n = snprintf(fmt, sizeof(fmt), "  %s: %%" PRId64 "\n",
+			     stats[i].name);  

The stats are unsigned so the format should be PRIu64 here.

Good catch.

Quoted 26 lines

netlink/pause.c int nl_gpause(struct cmd_context *ctx) · line 244

Quoted 11 lines
netlink/pause.c +2 -1
@@ -173,8 +236,9 @@ int nl_gpause(struct cmd_context *ctx)
 		return 1;
 	}
 
+	flags = nlctx->ctx->show_stats ? ETHTOOL_FLAG_STATS : 0;
 	ret = nlsock_prep_get_request(nlsk, ETHTOOL_MSG_PAUSE_GET,
-				      ETHTOOL_A_PAUSE_HEADER, 0);
+				      ETHTOOL_A_PAUSE_HEADER, flags);
 	if (ret < 0)
 		return ret;
 

When the stats are supported by kernel but not provided by a device, the request will succeed and usual output without stats will be shown. However, when stats are requested on a pre-5.10 kernel not recognizing ETHTOOL_FLAG_STATS, the request will fail:

mike@lion:~/work/git/ethtool> ./ethtool --debug 0x10 -I -a eth0 netlink error: unrecognized request flags
netlink error: Operation not supported
offending message and attribute:
ETHTOOL_MSG_PAUSE_GET
ETHTOOL_A_PAUSE_HEADER
ETHTOOL_A_HEADER_DEV_NAME = "eth0"
===> ETHTOOL_A_HEADER_FLAGS = 0x00000004

Quoted 3 lines

We should probably repeat the request with flags=0 in this case but that would require keeping the offset of ETHTOOL_A_HEADER_FLAGS attribute and checking for -EOPNOTSUPP with this offset in nlsock_process_ack().

Makes sense, will do.