From: Denys Fedoryshchenko <denys.f@collabora.com>
To: netdev@vger.kernel.org
Cc: Michal Kubecek <mkubecek@suse.cz>
Subject: [PATCH ethtool-next] features: add --json support
Date: Sun, 16 Jan 2022 13:31:00 +0200 [thread overview]
Message-ID: <266fbaa6b59bbbee6f216f46f3641fdcc3032bd0.camel@collabora.com> (raw)
Normal text output remain as before, adding json to make ethtool
more machine friendly.
Signed-off-by: Denys Fedoryshchenko <denys.f@collabora.com>
---
ethtool.c | 1 +
netlink/features.c | 36 +++++++++++++++++++++++++++++-------
2 files changed, 30 insertions(+), 7 deletions(-)
diff --git a/ethtool.c b/ethtool.c
index 064bc69..c4905f0 100644
--- a/ethtool.c
+++ b/ethtool.c
@@ -5729,6 +5729,7 @@ static const struct option args[] = {
.opts = "-k|--show-features|--show-offload",
.func = do_gfeatures,
.nlfunc = nl_gfeatures,
+ .json = true,
.help = "Get state of protocol offload and other features"
},
{
diff --git a/netlink/features.c b/netlink/features.c
index 2a0899e..cbe3edc 100644
--- a/netlink/features.c
+++ b/netlink/features.c
@@ -77,13 +77,23 @@ static void dump_feature(const struct feature_results *results,
}
if (!feature_on(results->hw, idx) || feature_on(results->nochange, idx))
- suffix = " [fixed]";
+ suffix = "[fixed]";
else if (feature_on(results->active, idx) !=
feature_on(results->wanted, idx))
suffix = feature_on(results->wanted, idx) ?
- " [requested on]" : " [requested off]";
- printf("%s%s: %s%s\n", prefix, name,
+ "[requested on]" : "[requested off]";
+
+ if (is_json_context()) {
+ char *name_suffix = malloc(strlen(name)+strlen("-opt")+1);
+
+ sprintf(name_suffix, "%s-opt", name);
+ print_bool(PRINT_JSON, name, NULL, feature_on(results->active, idx));
+ print_string(PRINT_JSON, name_suffix, NULL, suffix);
+ free(name_suffix);
+ } 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 +163,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,
+ 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 +225,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;
}
@@ -232,9 +249,14 @@ int nl_gfeatures(struct cmd_context *ctx)
ret = nlsock_prep_get_request(nlsk, ETHTOOL_MSG_FEATURES_GET,
ETHTOOL_A_FEATURES_HEADER,
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.32.0
next reply other threads:[~2022-01-16 11:31 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-16 11:31 Denys Fedoryshchenko [this message]
2022-01-18 20:01 ` [PATCH ethtool-next] features: add --json support Jakub Kicinski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=266fbaa6b59bbbee6f216f46f3641fdcc3032bd0.camel@collabora.com \
--to=denys.f@collabora.com \
--cc=mkubecek@suse.cz \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).