features: add --json support
Subsystems (2)
PATCH v2 1/1 features: add --json support
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(-)
ethtool.c +1 -0
@@ -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"
netlink/features.c +28 -7
@@ -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 */
Signature · 1 line
2.30.2
Jakub Kicinski
maintainernetlink/features.c int dump_features(const struct nlattr *const *tb, · line 168
Quoted 3 lines · On Mon, 24 Jan 2022 10:06:35 +0200 Denys Fedoryshchenko wrote:
netlink/features.c +3
-0+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?
Quoted 11 lines · Jakub Kicinski
Quoted 4 lines · 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.