[PATCH ethtool-next v3] features: add --json support
From: Denys Fedoryshchenko <hidden>
Date: 2022-01-26 07:42:28
Subsystem:
the rest · Maintainer:
Linus Torvalds
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,
"fixed": null,
"requested": null
},
"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,
"fixed": null,
"requested": null
},
"tx-scatter-gather": {
"active": false,
"fixed": true,
"requested": false
},
...skip similar lines...
v2:
- formatting fixes
- show each feature as object with available attributes
v3:
- report "fixed" and "requested" as nil for the special features
Signed-off-by: Denys Fedoryshchenko <redacted>
---
ethtool.c | 1 +
netlink/features.c | 39 ++++++++++++++++++++++++++++++++-------
2 files changed, 33 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..a4dae8f 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,18 @@ 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()) { + open_json_object(off_flag_def[i].long_name); + print_bool(PRINT_JSON, "active", NULL, flag_value); + print_null(PRINT_JSON, "fixed", NULL, NULL); + print_null(PRINT_JSON, "requested", NULL, NULL); + close_json_object(); + } 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 +228,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 +254,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