Add JSON support for SFP EEPROM dump
Subsystems (2)
cover Add JSON support for SFP EEPROM dump
This patch adds JSON output support to ethtool's -m option, useful for presenting transceiver module option in a web interface.
An example frontend can be found here, for OpenWrt's LuCI:
https://gitlab.com/traversetech/muvirt-feed/-/tree/master/sfp-diagnostics-luci
You can find samples of the output, plus a screenshot here:
10GBASE-LR SFP:
https://gitlab.com/-/snippets/2364354
1/10GBASE-LR dual mode SFP
https://gist.github.com/mcbridematt/7921c795c4835afa807d8f83cc132401
I have tried to make the JSON format easy to consume by including both the original field values and full text descriptions, so any consumer of the JSON output doesn't need to replicate ethtool's rendering logic.
I'm open to any suggestions on how the structure of the JSON output could be improved.
Mathew McBride (2):
ethtool: add JSON output to --module-info
ethtool: remove restriction on ioctl commands having JSON output
ethtool.c | 8 +- sff-common.c | 213 +++++++++++++++-------- sff-common.h | 17 +- sfpdiag.c | 64 ++++++- sfpid.c | 478 +++++++++++++++++++++++++++++++-------------------- 5 files changed, 522 insertions(+), 258 deletions(-)
Signature · 1 line
2.30.1
PATCH 1/2 ethtool: add JSON output to --module-info
This provides JSON output support for 'ethtool -m' / --module-info
To make presenting the module data as simple as possible, both the raw bytes/codes and formatted descriptions are provided where available.
A sample output (edited and formatted for brevity) is shown below:
$ ethtool --json -m eth8
[
{
"identifier": 3,
"identifier_description": "SFP",
"vendor_name": "UBNT",
"vendor_oui": "00:00:00",
"vendor_pn": "UF-MM-10G",
"vendor_sn": "FT17072604079",
"date_code": "170727__",
"extended_identifier": 4,
"extended_identifier_description": "GBIC/SFP defined by 2-wire interface ID",
"connector": 7,
"connector_description": "LC",
"transceiver_codes": [
16,
0,
0,
0,
64,
64,
12,
85,
0
],
"transceiver_types": [
"10G Ethernet: 10G Base-SR"
],
"encoding": 6,
"encoding_description": "64B/66B",
"bitrate_nominal": 10300,
"rate_identifier": 0,
"rate_identifier_description": "unspecified",
"laser_wavelength": 850,
"vendor_rev": "",
"option_byte1": 0,
"option_byte2": 26,
"option_descriptions": [
"RX_LOS implemented",
"TX_FAULT implemented",
"TX_DISABLE implemented"
],
"br_margin_max": 0,
"br_margin_min": 0,
"optical_diagnostics_supported": true,
"bias_current": 11.516,
"tx_power": 0.4872,
"module_temp": 47.1719,
"module_voltage": 3.2784
}
]--- ethtool.c | 5 + sff-common.c | 213 +++++++++++++++-------- sff-common.h | 17 +- sfpdiag.c | 64 ++++++- sfpid.c | 478 +++++++++++++++++++++++++++++++-------------------- 5 files changed, 522 insertions(+), 255 deletions(-)
ethtool.c +5 -0
@@ -4897,6 +4897,8 @@ static int do_getmodule(struct cmd_context *ctx) (eeprom->len != modinfo.eeprom_len)) { geeprom_dump_hex =1 ; } else if (!geeprom_dump_hex) { +new_json_obj (ctx->json); +open_json_object (NULL );switch (modinfo.type) {#ifdef ETHTOOL_ENABLE_PRETTY_DUMP case ETH_MODULE_SFF_8079 :
@@ -4916,6 +4918,8 @@ static int do_getmodule(struct cmd_context *ctx) geeprom_dump_hex =1 ;break ; } +close_json_object (); +delete_json_obj (); }if (geeprom_dump_hex) dump_hex(stdout, eeprom->data,
@@ -5925,6 +5929,7 @@ static const struct option args[] = { .opts = "-m|--dump-module-eeprom|--module-info", .func = do_getmodule, .nlfunc = nl_getmodule, + .json = true, .help ="Query/Decode Module EEPROM information and optical diagnostics if available" , .xhelp = " [ raw on|off ]\n"" [ hex on|off ]\n"
sff-common.c +145 -68
369 lines not on this page · raw message
sff-common.h +15 -2
@@ -134,19 +134,31 @@printf ("\t%-41s : %.4f mW / %.2f dBm\n" , (string), \ (double )((var) /10000. ), \ convert_mw_to_dbm((double)((var) / 10000.))) +#define PRINT_xX_PWR_JSON (key, var) \ + print_float(PRINT_JSON, key, "%.2f", \ + (double)((var) / 10000.)) +#define BIAS_DIV (value) ((double)(value / 500.))#define PRINT_BIAS (string, bias_cur) \ printf("\t%-41s : %.3f mA\n", (string), \ - (double)(bias_cur / 500.)) + BIAS_DIV(bias_cur)) + +#define PRINT_BIAS_JSON (key, bias_cur) \ + print_float(PRINT_JSON, key, "%.3f", BIAS_DIV(bias_cur))#define PRINT_TEMP (string, temp) \ printf("\t%-41s : %.2f degrees C / %.2f degrees F\n", \ (string), (double)(temp / 256.), \ (double)(temp / 256. * 1.8 + 32.)) - +#define PRINT_TEMP_JSON (key, temp) \ + print_float(PRINT_JSON, key, "%.2f", \ + (double)(temp / 256.))#define PRINT_VCC (string, sfp_voltage) \ printf("\t%-41s : %.4f V\n", (string), \ (double)(sfp_voltage / 10000.)) +#define PRINT_VCC_JSON (key, sfp_voltage) \ + print_float(PRINT_JSON, key, "%.4f", \ + (double)(sfp_voltage / 10000.))# define PRINT_xX_THRESH_PWR (string, var, index) \ PRINT_xX_PWR(string, (var)[(index)])
@@ -199,6 +211,7 @@ void sff_show_value_with_unit(const __u8 *id, unsigned int reg,void sff_show_ascii (const __u8 *id,unsigned int first_reg,unsigned int last_reg,const char *name);void sff_show_thresholds (struct sff_diags sd); +void sff_show_thresholds_json (struct sff_diags sd);void sff8024_show_oui (const __u8 *id,int id_offset);void sff8024_show_identifier (const __u8 *id,int id_offset);
sfpdiag.c +63 -1
89 lines not on this page · raw message
sfpid.c +294 -184
711 lines not on this page · raw message
Signature · 1 line
2.30.1
Re: [PATCH ethtool 1/2] ethtool: add JSON output to --module-info
Quoted 59 lines · Mathew McBride
This provides JSON output support for 'ethtool -m' / --module-info
To make presenting the module data as simple as possible, both the raw bytes/codes and formatted descriptions are provided where available.
A sample output (edited and formatted for brevity) is shown below:
$ ethtool --json -m eth8 [ { "identifier": 3, "identifier_description": "SFP", "vendor_name": "UBNT", "vendor_oui": "00:00:00", "vendor_pn": "UF-MM-10G", "vendor_sn": "FT17072604079", "date_code": "170727__", "extended_identifier": 4, "extended_identifier_description": "GBIC/SFP defined by 2-wire interface ID", "connector": 7, "connector_description": "LC", "transceiver_codes": [ 16, 0, 0, 0, 64, 64, 12, 85, 0 ], "transceiver_types": [ "10G Ethernet: 10G Base-SR" ], "encoding": 6, "encoding_description": "64B/66B", "bitrate_nominal": 10300, "rate_identifier": 0, "rate_identifier_description": "unspecified", "laser_wavelength": 850, "vendor_rev": "", "option_byte1": 0, "option_byte2": 26, "option_descriptions": [ "RX_LOS implemented", "TX_FAULT implemented", "TX_DISABLE implemented" ], "br_margin_max": 0, "br_margin_min": 0, "optical_diagnostics_supported": true, "bias_current": 11.516, "tx_power": 0.4872, "module_temp": 47.1719, "module_voltage": 3.2784 } ]
Quoted 1 line
Signed-off-by Mathew McBride <[email protected]>
IIUC this patch implements the JSON output only for some modules, in particular those handled by sff8079_show_all_nl(). Similar to my comment on patch 2/2, I believe it's wrong to show JSON output for some modules and silently fall back to plain text for others. Unless you can provide JSON output for all modules, the command should fail when --json is used but JSON output is unavailable.
Michal
PATCH 2/2 ethtool: remove restriction on ioctl commands having JSON output
In reply to Mathew McBrideThe module-info/dump eeprom (-m/--module-info) is one such command which is now able to produce JSON.
--- ethtool.c | 3 --- 1 file changed, 3 deletions(-)
ethtool.c +0 -3
@@ -6409,9 +6409,6 @@ int main(int argc, char **argp) ctx.argp = argp;netlink_run_handler (&ctx, args[k].nlchk, args[k].nlfunc, !args[k].func); -if (ctx.json)/* no IOCTL command supports JSON output */ -exit_bad_args (); - ret =ioctl_init (&ctx, args[k].no_dev);if (ret)return ret;
Signature · 1 line
2.30.1
Re: [PATCH ethtool 2/2] ethtool: remove restriction on ioctl commands having JSON output
Quoted 4 lines · Mathew McBride
The module-info/dump eeprom (-m/--module-info) is one such command which is now able to produce JSON.
Signed-off-by Mathew McBride <[email protected]>
I'm sorry for the delay, this came while I was on vacation so I needed to think a bit more about JSON output in ioctl path, then it somehow fell through the cracks.
Quoted 9 lines
--- ethtool.c | 3 --- 1 file changed, 3 deletions(-)ethtool.c +0
-0
ethtool.c int main(int argc, char **argp) · line 6414
Quoted 9 lines
ethtool.c +0
-3@@ -6409,9 +6409,6 @@ int main(int argc, char **argp) ctx.argp = argp;netlink_run_handler (&ctx, args[k].nlchk, args[k].nlfunc, !args[k].func); -if (ctx.json)/* no IOCTL command supports JSON output */ -exit_bad_args (); - ret =ioctl_init (&ctx, args[k].no_dev);if (ret)return ret;
I don't think this is a good idea. This way you effectively revert commit 9a935085ec1c ("ethtool: return error if command does not support --json") in ioctl path so that --json will be silently ignored there for all commands which do not support JSON output, i.e. for all except one.
One way around would be adding an extra flag to mark commands supporting JSON output in ioctl path but IMHO it's not likely that there are going to be many more. It would be probably easier keep the check as is.
Michal