netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH ethtool-next v2] features: add --json support
@ 2022-01-24  8:06 Denys Fedoryshchenko
  2022-01-24 19:02 ` Jakub Kicinski
  0 siblings, 1 reply; 3+ messages in thread
From: Denys Fedoryshchenko @ 2022-01-24  8:06 UTC (permalink / raw)
  To: Michal Kubecek; +Cc: netdev

Adding json to make ethtool more machine friendly.
No change in normal text output:

Features for enx0c37961ce55a:
rx-checksumming: off [fixed]
tx-checksumming: off
	tx-checksum-ipv4: off [fixed]
	tx-checksum-ip-generic: off [fixed]
	tx-checksum-ipv6: off [fixed]
	tx-checksum-fcoe-crc: off [fixed]
	tx-checksum-sctp: off [fixed]
scatter-gather: off
	tx-scatter-gather: off [fixed]
	tx-scatter-gather-fraglist: off [fixed]
tcp-segmentation-offload: off
	tx-tcp-segmentation: off [fixed]
	tx-tcp-ecn-segmentation: off [fixed]
	tx-tcp-mangleid-segmentation: off [fixed]
	tx-tcp6-segmentation: off [fixed]
generic-segmentation-offload: off [requested on]

...skip similar lines...

JSON output:
[ {
        "ifname": "enx0c37961ce55a",
        "rx-checksumming": {
            "active": false,
            "fixed": true,
            "requested": false
        },
        "tx-checksumming": {
            "active": false
        },
        "tx-checksum-ipv4": {
            "active": false,
            "fixed": true,
            "requested": false
        },
        "tx-checksum-ip-generic": {
            "active": false,
            "fixed": true,
            "requested": false
        },
        "tx-checksum-ipv6": {
            "active": false,
            "fixed": true,
            "requested": false
        },
        "tx-checksum-fcoe-crc": {
            "active": false,
            "fixed": true,
            "requested": false
        },
        "tx-checksum-sctp": {
            "active": false,
            "fixed": true,
            "requested": false
        },
        "scatter-gather": {
            "active": false
        },
        "tx-scatter-gather": {
            "active": false,
            "fixed": true,
            "requested": false
        },

...skip similar lines...

v2:
 - formatting fixes
 - show each feature as object with available attributes

---
 ethtool.c          |  1 +
 netlink/features.c | 35 ++++++++++++++++++++++++++++-------
 2 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/ethtool.c b/ethtool.c
index 5d718a2..28ecf69 100644
--- a/ethtool.c
+++ b/ethtool.c
@@ -5734,6 +5734,7 @@ static const struct option args[] = {
 	},
 	{
 		.opts	= "-k|--show-features|--show-offload",
+		.json	= true,
 		.func	= do_gfeatures,
 		.nlfunc	= nl_gfeatures,
 		.help	= "Get state of protocol offload and other features"
diff --git a/netlink/features.c b/netlink/features.c
index 2a0899e..a7b66d8 100644
--- a/netlink/features.c
+++ b/netlink/features.c
@@ -82,8 +82,17 @@ static void dump_feature(const struct feature_results *results,
 		 feature_on(results->wanted, idx))
 		suffix = feature_on(results->wanted, idx) ?
 			" [requested on]" : " [requested off]";
-	printf("%s%s: %s%s\n", prefix, name,
-	       feature_on(results->active, idx) ? "on" : "off", suffix);
+	if (is_json_context()) {
+		open_json_object(name);
+		print_bool(PRINT_JSON, "active", NULL, feature_on(results->active, idx));
+		print_bool(PRINT_JSON, "fixed", NULL,
+			   (!feature_on(results->hw, idx) || feature_on(results->nochange, idx)));
+		print_bool(PRINT_JSON, "requested", NULL, feature_on(results->wanted, idx));
+		close_json_object();
+	} else {
+		printf("%s%s: %s%s\n", prefix, name,
+		       feature_on(results->active, idx) ? "on" : "off", suffix);
+	}
 }
 
 /* this assumes pattern contains no more than one asterisk */
@@ -153,9 +162,14 @@ int dump_features(const struct nlattr *const *tb,
 					feature_on(results.active, j);
 			}
 		}
-		if (n_match != 1)
-			printf("%s: %s\n", off_flag_def[i].long_name,
-			       flag_value ? "on" : "off");
+		if (n_match != 1) {
+			if (is_json_context()) {
+				print_bool(PRINT_JSON, off_flag_def[i].long_name, NULL, flag_value);
+			} else {
+				printf("%s: %s\n", off_flag_def[i].long_name,
+				       flag_value ? "on" : "off");
+			}
+		}
 		if (n_match == 0)
 			continue;
 		for (j = 0; j < results.count; j++) {
@@ -210,8 +224,10 @@ int features_reply_cb(const struct nlmsghdr *nlhdr, void *data)
 
 	if (silent)
 		putchar('\n');
-	printf("Features for %s:\n", nlctx->devname);
+	open_json_object(NULL);
+	print_string(PRINT_ANY, "ifname", "Features for %s:\n", nlctx->devname);
 	ret = dump_features(tb, feature_names);
+	close_json_object();
 	return (silent || !ret) ? MNL_CB_OK : MNL_CB_ERROR;
 }
 
@@ -234,7 +250,12 @@ int nl_gfeatures(struct cmd_context *ctx)
 				      ETHTOOL_FLAG_COMPACT_BITSETS);
 	if (ret < 0)
 		return ret;
-	return nlsock_send_get_request(nlsk, features_reply_cb);
+
+	new_json_obj(ctx->json);
+	ret = nlsock_send_get_request(nlsk, features_reply_cb);
+	delete_json_obj();
+
+	return ret;
 }
 
 /* FEATURES_SET */
-- 
2.30.2




^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH ethtool-next v2] features: add --json support
  2022-01-24  8:06 [PATCH ethtool-next v2] features: add --json support Denys Fedoryshchenko
@ 2022-01-24 19:02 ` Jakub Kicinski
  2022-01-25 12:58   ` Denys Fedoryshchenko
  0 siblings, 1 reply; 3+ messages in thread
From: Jakub Kicinski @ 2022-01-24 19:02 UTC (permalink / raw)
  To: Denys Fedoryshchenko; +Cc: Michal Kubecek, netdev

On Mon, 24 Jan 2022 10:06:35 +0200 Denys Fedoryshchenko wrote:
> +			if (is_json_context()) {
> +				print_bool(PRINT_JSON, off_flag_def[i].long_name, NULL, flag_value);
> +			} else {

Would it make sense to report "fixed" and "requested" as nil for the
special features? I'm not a high level language expert but otherwise
generic code handling features will have to test for presence of those
keys before accessing them, no?

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH ethtool-next v2] features: add --json support
  2022-01-24 19:02 ` Jakub Kicinski
@ 2022-01-25 12:58   ` Denys Fedoryshchenko
  0 siblings, 0 replies; 3+ messages in thread
From: Denys Fedoryshchenko @ 2022-01-25 12:58 UTC (permalink / raw)
  To: Jakub Kicinski; +Cc: Michal Kubecek, netdev

On Mon, 2022-01-24 at 11:02 -0800, Jakub Kicinski wrote:
> On Mon, 24 Jan 2022 10:06:35 +0200 Denys Fedoryshchenko wrote:
> > +                       if (is_json_context()) {
> > +                               print_bool(PRINT_JSON,
> > off_flag_def[i].long_name, NULL, flag_value);
> > +                       } else {
> 
> Would it make sense to report "fixed" and "requested" as nil for the
> special features? I'm not a high level language expert but otherwise
> generic code handling features will have to test for presence of
> those
> keys before accessing them, no?
I thought about this question for a while, some people prefer to check
for key existence, some prefer to have key, but set null value, and
even all my friends were divided in opinion. 
I think you are right, for a stable schema, I'd better add null values
for missing attributes. I will prepare v3.



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-01-25 13:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-24  8:06 [PATCH ethtool-next v2] features: add --json support Denys Fedoryshchenko
2022-01-24 19:02 ` Jakub Kicinski
2022-01-25 12:58   ` Denys Fedoryshchenko

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