Skip to contentSkip to conversation

JSON output support for Netlink implementation of --show-coalesce option

PATCH Superseded Maxim Georgiev 3 participants 6 messages Dec 15, 2022 – Dec 20, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 JSON output support for Netlink implementation of --show-coalesce option

Add --json support for Netlink implementation of --show-coalesce option No changes for non-JSON output for this featire.

Example output without --json:
[ethtool-git]$ sudo ./ethtool --show-coalesce enp9s0u2u1u2 Coalesce parameters for enp9s0u2u1u2:
Adaptive RX: n/a TX: n/a
stats-block-usecs: n/a
sample-interval: n/a
pkt-rate-low: n/a
pkt-rate-high: n/a

rx-usecs: 15000
rx-frames: n/a
rx-usecs-irq: n/a
rx-frames-irq: n/a

tx-usecs: 0
tx-frames: n/a
tx-usecs-irq: n/a
tx-frames-irq: n/a

rx-usecs-low: n/a
rx-frame-low: n/a
tx-usecs-low: n/a
tx-frame-low: n/a

rx-usecs-high: n/a
rx-frame-high: n/a
tx-usecs-high: n/a
tx-frame-high: n/a

CQE mode RX: n/a TX: n/a

Same output with --json:
[ethtool-git]$ sudo ./ethtool --json --show-coalesce enp9s0u2u1u2

[ {
        "ifname": "enp9s0u2u1u2",
        "rx-usecs": 15000,
        "tx-usecs": 0
    } ]

---
Changes in v2:
- Moved the patch to ethtool-next branch
- Eliminated ':' in JSON key names
- Replaced the last 'putchar('\n')' call left in coalesce_reply_cb() with show_cr()

 ethtool.c          |  1 +
 netlink/channels.c | 18 +++++-----
 netlink/coalesce.c | 88 ++++++++++++++++++++++++++++++----------------
 netlink/netlink.h  | 24 ++++++++++---
 netlink/rings.c    | 21 +++++------
 5 files changed, 98 insertions(+), 54 deletions(-)
ethtool.c +1 -0
index 3207e49..3b8412c 100644
@@ -5694,6 +5694,7 @@ static const struct option args[] = {
 	},
 	{
 		.opts	= "-c|--show-coalesce",
+		.json	= true,
 		.func	= do_gcoalesce,
 		.nlfunc	= nl_gcoalesce,
 		.help	= "Show coalesce options"
netlink/channels.c +10 -8
index 894c74b..5cae227 100644
@@ -37,15 +37,17 @@ int channels_reply_cb(const struct nlmsghdr *nlhdr, void *data)
 		putchar('\n');
 	printf("Channel parameters for %s:\n", nlctx->devname);
 	printf("Pre-set maximums:\n");
-	show_u32(tb[ETHTOOL_A_CHANNELS_RX_MAX], "RX:\t\t");
-	show_u32(tb[ETHTOOL_A_CHANNELS_TX_MAX], "TX:\t\t");
-	show_u32(tb[ETHTOOL_A_CHANNELS_OTHER_MAX], "Other:\t\t");
-	show_u32(tb[ETHTOOL_A_CHANNELS_COMBINED_MAX], "Combined:\t");
+	show_u32("rx-max", "RX:\t\t", tb[ETHTOOL_A_CHANNELS_RX_MAX]);
+	show_u32("tx-max", "TX:\t\t", tb[ETHTOOL_A_CHANNELS_TX_MAX]);
+	show_u32("other-max", "Other:\t\t", tb[ETHTOOL_A_CHANNELS_OTHER_MAX]);
+	show_u32("combined-max", "Combined:\t",
+		 tb[ETHTOOL_A_CHANNELS_COMBINED_MAX]);
 	printf("Current hardware settings:\n");
-	show_u32(tb[ETHTOOL_A_CHANNELS_RX_COUNT], "RX:\t\t");
-	show_u32(tb[ETHTOOL_A_CHANNELS_TX_COUNT], "TX:\t\t");
-	show_u32(tb[ETHTOOL_A_CHANNELS_OTHER_COUNT], "Other:\t\t");
-	show_u32(tb[ETHTOOL_A_CHANNELS_COMBINED_COUNT], "Combined:\t");
+	show_u32("rx", "RX:\t\t", tb[ETHTOOL_A_CHANNELS_RX_COUNT]);
+	show_u32("tx", "TX:\t\t", tb[ETHTOOL_A_CHANNELS_TX_COUNT]);
+	show_u32("other", "Other:\t\t", tb[ETHTOOL_A_CHANNELS_OTHER_COUNT]);
+	show_u32("combined", "Combined:\t",
+		 tb[ETHTOOL_A_CHANNELS_COMBINED_COUNT]);
 
 	return MNL_CB_OK;
 }
netlink/coalesce.c +57 -31

107 lines not on this page · raw message

netlink/netlink.h +19 -5
index f43c1bf..3240fca 100644
@@ -100,12 +100,20 @@ int dump_link_modes(struct nl_context *nlctx, const struct nlattr *bitset,
 		    const char *between, const char *after,
 		    const char *if_none);
 
-static inline void show_u32(const struct nlattr *attr, const char *label)
+static inline void show_u32(const char *key,
+			    const char *fmt,
+			    const struct nlattr *attr)
 {
-	if (attr)
-		printf("%s%u\n", label, mnl_attr_get_u32(attr));
-	else
-		printf("%sn/a\n", label);
+	if (is_json_context()) {
+		if (attr)
+			print_uint(PRINT_JSON, key, NULL,
+				   mnl_attr_get_u32(attr));
+	} else {
+		if (attr)
+			printf("%s%u\n", fmt, mnl_attr_get_u32(attr));
+		else
+			printf("%sn/a\n", fmt);
+	}
 }
 
 static inline const char *u8_to_bool(const uint8_t *val)
@@ -132,6 +140,12 @@ static inline void show_bool(const char *key, const char *fmt,
 	show_bool_val(key, fmt, attr ? mnl_attr_get_payload(attr) : NULL);
 }
 
+static inline void show_cr(void)
+{
+	if (!is_json_context())
+		putchar('\n');
+}
+
 /* misc */
 
 static inline void copy_devname(char *dst, const char *src)
netlink/rings.c +11 -10
index 6284035..5996d5a 100644
@@ -38,17 +38,18 @@ int rings_reply_cb(const struct nlmsghdr *nlhdr, void *data)
 		putchar('\n');
 	printf("Ring parameters for %s:\n", nlctx->devname);
 	printf("Pre-set maximums:\n");
-	show_u32(tb[ETHTOOL_A_RINGS_RX_MAX], "RX:\t\t");
-	show_u32(tb[ETHTOOL_A_RINGS_RX_MINI_MAX], "RX Mini:\t");
-	show_u32(tb[ETHTOOL_A_RINGS_RX_JUMBO_MAX], "RX Jumbo:\t");
-	show_u32(tb[ETHTOOL_A_RINGS_TX_MAX], "TX:\t\t");
+	show_u32("rx-max", "RX:\t\t", tb[ETHTOOL_A_RINGS_RX_MAX]);
+	show_u32("rx-mini-max", "RX Mini:\t", tb[ETHTOOL_A_RINGS_RX_MINI_MAX]);
+	show_u32("rx-jumbo-max", "RX Jumbo:\t",
+		 tb[ETHTOOL_A_RINGS_RX_JUMBO_MAX]);
+	show_u32("tx-max", "TX:\t\t", tb[ETHTOOL_A_RINGS_TX_MAX]);
 	printf("Current hardware settings:\n");
-	show_u32(tb[ETHTOOL_A_RINGS_RX], "RX:\t\t");
-	show_u32(tb[ETHTOOL_A_RINGS_RX_MINI], "RX Mini:\t");
-	show_u32(tb[ETHTOOL_A_RINGS_RX_JUMBO], "RX Jumbo:\t");
-	show_u32(tb[ETHTOOL_A_RINGS_TX], "TX:\t\t");
-	show_u32(tb[ETHTOOL_A_RINGS_RX_BUF_LEN], "RX Buf Len:\t\t");
-	show_u32(tb[ETHTOOL_A_RINGS_CQE_SIZE], "CQE Size:\t\t");
+	show_u32("rx", "RX:\t\t", tb[ETHTOOL_A_RINGS_RX]);
+	show_u32("rx-mini", "RX Mini:\t", tb[ETHTOOL_A_RINGS_RX_MINI]);
+	show_u32("rx-jumbo", "RX Jumbo:\t", tb[ETHTOOL_A_RINGS_RX_JUMBO]);
+	show_u32("tx", "TX:\t\t", tb[ETHTOOL_A_RINGS_TX]);
+	show_u32("rx-buf-len", "RX Buf Len:\t", tb[ETHTOOL_A_RINGS_RX_BUF_LEN]);
+	show_u32("cqe-size", "CQE Size:\t", tb[ETHTOOL_A_RINGS_CQE_SIZE]);
 	show_bool("tx-push", "TX Push:\t%s\n", tb[ETHTOOL_A_RINGS_TX_PUSH]);
 
 	tcp_hds = tb[ETHTOOL_A_RINGS_TCP_DATA_SPLIT] ?
Signature · 1 line
2.38.1
maintainer
#1 lore raw
Quoted 2 lines · On Wed, 14 Dec 2022 20:56:51 -0700 Maxim Georgiev wrote:

Add --json support for Netlink implementation of --show-coalesce option No changes for non-JSON output for this featire.

s/featire/feature/

I'd add another sentence here, something like:

Note that all callers of show_u32() have to be updated but only coalesce is converted fully.

To make it clear that the interspersed printf()s don't matter.

One small nit pick below:

netlink/coalesce.c int nl_gcoalesce(struct cmd_context *ctx) · line 122

Quoted 12 lines
netlink/coalesce.c +6 -1
@@ -92,7 +113,12 @@ int nl_gcoalesce(struct cmd_context *ctx)
 				      ETHTOOL_A_COALESCE_HEADER, 0);
 	if (ret < 0)
 		return ret;
-	return nlsock_send_get_request(nlsk, coalesce_reply_cb);
+
+	new_json_obj(ctx->json);
+	ret = nlsock_send_get_request(nlsk, coalesce_reply_cb);
+	delete_json_obj();
+	return ret;
+
 }

Empty line befre closing bracket unnecessary.

#2 lore raw
Quoted 16 lines · Jakub Kicinski
Quoted 2 lines · On Wed, 14 Dec 2022 20:56:51 -0700 Maxim Georgiev wrote:

Add --json support for Netlink implementation of --show-coalesce option No changes for non-JSON output for this featire.

s/featire/feature/

I'd add another sentence here, something like:

Note that all callers of show_u32() have to be updated but only coalesce is converted fully.

To make it clear that the interspersed printf()s don't matter.

One small nit pick below:

Quoted 14 lines

netlink/coalesce.c int nl_gcoalesce(struct cmd_context *ctx) · line 122

Quoted 12 lines
netlink/coalesce.c +6 -1
@@ -92,7 +113,12 @@ int nl_gcoalesce(struct cmd_context *ctx)
                                    ETHTOOL_A_COALESCE_HEADER, 0);
      if (ret < 0)
              return ret;
-     return nlsock_send_get_request(nlsk, coalesce_reply_cb);
+
+     new_json_obj(ctx->json);
+     ret = nlsock_send_get_request(nlsk, coalesce_reply_cb);
+     delete_json_obj();
+     return ret;
+
 }

Empty line befre closing bracket unnecessary.

Thank you for catching it!
I'll submit the updated patch version shortly.

#3 lore raw

PATCH v3 1/1 JSON output support for Netlink implementation of --show-coalesce option

In reply to Jakub Kicinski

Add --json support for Netlink implementation of --show-coalesce option No changes for non-JSON output for this feature.

Example output without --json:
[ethtool-git]$ sudo ./ethtool --show-coalesce enp9s0u2u1u2 Coalesce parameters for enp9s0u2u1u2:
Adaptive RX: n/a TX: n/a
stats-block-usecs: n/a
sample-interval: n/a
pkt-rate-low: n/a
pkt-rate-high: n/a

rx-usecs: 15000
rx-frames: n/a
rx-usecs-irq: n/a
rx-frames-irq: n/a

tx-usecs: 0
tx-frames: n/a
tx-usecs-irq: n/a
tx-frames-irq: n/a

rx-usecs-low: n/a
rx-frame-low: n/a
tx-usecs-low: n/a
tx-frame-low: n/a

rx-usecs-high: n/a
rx-frame-high: n/a
tx-usecs-high: n/a
tx-frame-high: n/a

CQE mode RX: n/a TX: n/a

Same output with --json:
[ethtool-git]$ sudo ./ethtool --json --show-coalesce enp9s0u2u1u2

[ {
        "ifname": "enp9s0u2u1u2",
        "rx-usecs": 15000,
        "tx-usecs": 0
    } ]

Note that since show_u32() signature was changed, all show_u32() call sites had to be updated. So far only coalesce.c of all the updated files supports JSON output. It's OK for now to mix show_u32() calls with printf() calls in rings.c and channels.c since they can't print JSON yet. Something to fix in future.

---
Changes in V3:
- Fixed a typo in the patch description (featire->feature)
- Removed the empty line at the end of nl_gcoalesce()
- Added a section to the description commenting on the mix of JSON-complient show_u32() and printfs in rings.c and channels.c
Changes in v2:
- Moved the patch to ethtool-next branch
- Eliminated ':' in JSON key names
- Replaced the last 'putchar('\n')' call left
in coalesce_reply_cb() with show_cr()

ethtool.c | 1 +

 netlink/channels.c | 18 +++++-----
 netlink/coalesce.c | 87 +++++++++++++++++++++++++++++-----------------
 netlink/netlink.h  | 24 ++++++++++---
 netlink/rings.c    | 21 +++++------
 5 files changed, 97 insertions(+), 54 deletions(-)
ethtool.c +1 -0
index 3207e49..3b8412c 100644
@@ -5694,6 +5694,7 @@ static const struct option args[] = {
 	},
 	{
 		.opts	= "-c|--show-coalesce",
+		.json	= true,
 		.func	= do_gcoalesce,
 		.nlfunc	= nl_gcoalesce,
 		.help	= "Show coalesce options"
netlink/channels.c +10 -8
index 894c74b..5cae227 100644
@@ -37,15 +37,17 @@ int channels_reply_cb(const struct nlmsghdr *nlhdr, void *data)
 		putchar('\n');
 	printf("Channel parameters for %s:\n", nlctx->devname);
 	printf("Pre-set maximums:\n");
-	show_u32(tb[ETHTOOL_A_CHANNELS_RX_MAX], "RX:\t\t");
-	show_u32(tb[ETHTOOL_A_CHANNELS_TX_MAX], "TX:\t\t");
-	show_u32(tb[ETHTOOL_A_CHANNELS_OTHER_MAX], "Other:\t\t");
-	show_u32(tb[ETHTOOL_A_CHANNELS_COMBINED_MAX], "Combined:\t");
+	show_u32("rx-max", "RX:\t\t", tb[ETHTOOL_A_CHANNELS_RX_MAX]);
+	show_u32("tx-max", "TX:\t\t", tb[ETHTOOL_A_CHANNELS_TX_MAX]);
+	show_u32("other-max", "Other:\t\t", tb[ETHTOOL_A_CHANNELS_OTHER_MAX]);
+	show_u32("combined-max", "Combined:\t",
+		 tb[ETHTOOL_A_CHANNELS_COMBINED_MAX]);
 	printf("Current hardware settings:\n");
-	show_u32(tb[ETHTOOL_A_CHANNELS_RX_COUNT], "RX:\t\t");
-	show_u32(tb[ETHTOOL_A_CHANNELS_TX_COUNT], "TX:\t\t");
-	show_u32(tb[ETHTOOL_A_CHANNELS_OTHER_COUNT], "Other:\t\t");
-	show_u32(tb[ETHTOOL_A_CHANNELS_COMBINED_COUNT], "Combined:\t");
+	show_u32("rx", "RX:\t\t", tb[ETHTOOL_A_CHANNELS_RX_COUNT]);
+	show_u32("tx", "TX:\t\t", tb[ETHTOOL_A_CHANNELS_TX_COUNT]);
+	show_u32("other", "Other:\t\t", tb[ETHTOOL_A_CHANNELS_OTHER_COUNT]);
+	show_u32("combined", "Combined:\t",
+		 tb[ETHTOOL_A_CHANNELS_COMBINED_COUNT]);
 
 	return MNL_CB_OK;
 }
netlink/coalesce.c +56 -31

106 lines not on this page · raw message

netlink/netlink.h +19 -5
index f43c1bf..3240fca 100644
@@ -100,12 +100,20 @@ int dump_link_modes(struct nl_context *nlctx, const struct nlattr *bitset,
 		    const char *between, const char *after,
 		    const char *if_none);
 
-static inline void show_u32(const struct nlattr *attr, const char *label)
+static inline void show_u32(const char *key,
+			    const char *fmt,
+			    const struct nlattr *attr)
 {
-	if (attr)
-		printf("%s%u\n", label, mnl_attr_get_u32(attr));
-	else
-		printf("%sn/a\n", label);
+	if (is_json_context()) {
+		if (attr)
+			print_uint(PRINT_JSON, key, NULL,
+				   mnl_attr_get_u32(attr));
+	} else {
+		if (attr)
+			printf("%s%u\n", fmt, mnl_attr_get_u32(attr));
+		else
+			printf("%sn/a\n", fmt);
+	}
 }
 
 static inline const char *u8_to_bool(const uint8_t *val)
@@ -132,6 +140,12 @@ static inline void show_bool(const char *key, const char *fmt,
 	show_bool_val(key, fmt, attr ? mnl_attr_get_payload(attr) : NULL);
 }
 
+static inline void show_cr(void)
+{
+	if (!is_json_context())
+		putchar('\n');
+}
+
 /* misc */
 
 static inline void copy_devname(char *dst, const char *src)
netlink/rings.c +11 -10
index 6284035..5996d5a 100644
@@ -38,17 +38,18 @@ int rings_reply_cb(const struct nlmsghdr *nlhdr, void *data)
 		putchar('\n');
 	printf("Ring parameters for %s:\n", nlctx->devname);
 	printf("Pre-set maximums:\n");
-	show_u32(tb[ETHTOOL_A_RINGS_RX_MAX], "RX:\t\t");
-	show_u32(tb[ETHTOOL_A_RINGS_RX_MINI_MAX], "RX Mini:\t");
-	show_u32(tb[ETHTOOL_A_RINGS_RX_JUMBO_MAX], "RX Jumbo:\t");
-	show_u32(tb[ETHTOOL_A_RINGS_TX_MAX], "TX:\t\t");
+	show_u32("rx-max", "RX:\t\t", tb[ETHTOOL_A_RINGS_RX_MAX]);
+	show_u32("rx-mini-max", "RX Mini:\t", tb[ETHTOOL_A_RINGS_RX_MINI_MAX]);
+	show_u32("rx-jumbo-max", "RX Jumbo:\t",
+		 tb[ETHTOOL_A_RINGS_RX_JUMBO_MAX]);
+	show_u32("tx-max", "TX:\t\t", tb[ETHTOOL_A_RINGS_TX_MAX]);
 	printf("Current hardware settings:\n");
-	show_u32(tb[ETHTOOL_A_RINGS_RX], "RX:\t\t");
-	show_u32(tb[ETHTOOL_A_RINGS_RX_MINI], "RX Mini:\t");
-	show_u32(tb[ETHTOOL_A_RINGS_RX_JUMBO], "RX Jumbo:\t");
-	show_u32(tb[ETHTOOL_A_RINGS_TX], "TX:\t\t");
-	show_u32(tb[ETHTOOL_A_RINGS_RX_BUF_LEN], "RX Buf Len:\t\t");
-	show_u32(tb[ETHTOOL_A_RINGS_CQE_SIZE], "CQE Size:\t\t");
+	show_u32("rx", "RX:\t\t", tb[ETHTOOL_A_RINGS_RX]);
+	show_u32("rx-mini", "RX Mini:\t", tb[ETHTOOL_A_RINGS_RX_MINI]);
+	show_u32("rx-jumbo", "RX Jumbo:\t", tb[ETHTOOL_A_RINGS_RX_JUMBO]);
+	show_u32("tx", "TX:\t\t", tb[ETHTOOL_A_RINGS_TX]);
+	show_u32("rx-buf-len", "RX Buf Len:\t", tb[ETHTOOL_A_RINGS_RX_BUF_LEN]);
+	show_u32("cqe-size", "CQE Size:\t", tb[ETHTOOL_A_RINGS_CQE_SIZE]);
 	show_bool("tx-push", "TX Push:\t%s\n", tb[ETHTOOL_A_RINGS_TX_PUSH]);
 
 	tcp_hds = tb[ETHTOOL_A_RINGS_TCP_DATA_SPLIT] ?
Signature · 1 line
2.38.1
maintainer Reviewed-by on v3 1/1
#4 lore raw
Quoted 2 lines · On Wed, 14 Dec 2022 22:13:47 -0700 Maxim Georgiev wrote:

Add --json support for Netlink implementation of --show-coalesce option No changes for non-JSON output for this feature.

#5 lore raw
In reply to Maxim Georgiev
This patch was applied to ethtool/ethtool.git (master) by Michal Kubecek <[email protected]>:

Hello:

This patch was applied to ethtool/ethtool.git (master) by Michal Kubecek <[email protected]>:

Quoted 12 lines · On Wed, 14 Dec 2022 22:13:47 -0700 you wrote:

Add --json support for Netlink implementation of --show-coalesce option No changes for non-JSON output for this feature.

Example output without --json:
[ethtool-git]$ sudo ./ethtool --show-coalesce enp9s0u2u1u2 Coalesce parameters for enp9s0u2u1u2:
Adaptive RX: n/a TX: n/a
stats-block-usecs: n/a
sample-interval: n/a
pkt-rate-low: n/a
pkt-rate-high: n/a

Quoted 1 line

[...]

Here is the summary with links:
- [ethtool-next,v3,v3] JSON output support for Netlink implementation of --show-coalesce option

    https://git.kernel.org/pub/scm/network/ethtool/ethtool.git/commit/?id=8cee2091ae2a

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html