ethtool(1) cable test support
Subsystems (3)
v5 cover ethtool(1) cable test support
Add the user space side of the ethtool cable test.
The TDR output is most useful when fed to some other tool which can visualize the data. So add JSON support, by borrowing code from iproute2.
v2:
man page fixes.
v3:
More man page fixes.
Use json_print from iproute2.
v4:
checkpatch cleanup
ethtool --cable-test dev
Place breakout into cable_test_context
Remove Pair: Pair output
v5:
Add missing pair in help text
Allow --cable-test|--cable-test-tdr with --monitor
Fix rounding when converting from floating point meters to centimeters
Andrew Lunn (6):
Add cable test support
Add cable test TDR support
json_writer: Import the iproute2 helper code for JSON output Add --json command line argument parsing ethtool.8.in: Document the cable test commands ethtool.8.in: Add --json option
Makefile.am | 5 +- ethtool.8.in | 53 ++++ ethtool.c | 46 +++- internal.h | 4 + json_writer.c | 389 +++++++++++++++++++++++++++ json_writer.h | 76 ++++++ netlink/cable_test.c | 624 +++++++++++++++++++++++++++++++++++++++++++ netlink/extapi.h | 4 + netlink/monitor.c | 8 + netlink/netlink.h | 5 +- netlink/parser.c | 41 +++ netlink/parser.h | 4 + 12 files changed, 1245 insertions(+), 14 deletions(-) create mode 100644 json_writer.c create mode 100644 json_writer.h create mode 100644 netlink/cable_test.c
Signature · 1 line
2.26.2
PATCH v5 1/6 Add cable test support
Add support for starting a cable test, and report the results.
This code does not follow the usual patterns because of the way the kernel reports the results of the cable test. It can take a number of seconds for cable testing to occur. So the action request messages is immediately acknowledges, and the test is then performed asynchronous. Once the test has completed, the results are returned as a notification.
This means the command starts as usual. It then monitors multicast messages until it receives the results.
--- Makefile.am | 2 +- ethtool.c | 5 + netlink/cable_test.c | 266 +++++++++++++++++++++++++++++++++++++++++++ netlink/extapi.h | 2 + netlink/monitor.c | 8 ++ netlink/netlink.h | 2 + 6 files changed, 284 insertions(+), 1 deletion(-) create mode 100644 netlink/cable_test.c
Makefile.am +1 -1
@@ -35,7 +35,7 @@ ethtool_SOURCES += \ netlink/channels.c netlink/coalesce.c netlink/pause.c \ netlink/eee.c netlink/tsinfo.c \ netlink/desc-ethtool.c netlink/desc-genlctrl.c \ - netlink/desc-rtnl.c \ + netlink/desc-rtnl.c netlink/cable_test.c \ uapi/linux/ethtool_netlink.h \ uapi/linux/netlink.h uapi/linux/genetlink.h \ uapi/linux/rtnetlink.h uapi/linux/if_link.h
ethtool.c +5 -0
@@ -5482,6 +5482,11 @@ static const struct option args[] = { .xhelp = "The supported sub commands include --show-coalesce, --coalesce" " [queue_mask %x] SUB_COMMAND\n", }, + { + .opts = "--cable-test", + .nlfunc = nl_cable_test, + .help = "Perform a cable test", + }, { .opts = "-h|--help", .no_dev = true,
netlink/cable_test.c new file +266 -0
266 lines not on this page · raw message
netlink/extapi.h +2 -0
@@ -35,6 +35,7 @@ int nl_spause(struct cmd_context *ctx);int nl_geee (struct cmd_context *ctx);int nl_seee (struct cmd_context *ctx);int nl_tsinfo (struct cmd_context *ctx); +int nl_cable_test (struct cmd_context *ctx);int nl_monitor (struct cmd_context *ctx);void nl_monitor_usage (void );
@@ -74,6 +75,7 @@ static inline void nl_monitor_usage(void)#define nl_geee NULL#define nl_seee NULL#define nl_tsinfo NULL +#define nl_cable_test NULL#endif /* ETHTOOL_ENABLE_NETLINK */
netlink/monitor.c +8 -0
@@ -59,6 +59,10 @@ static struct { .cmd = ETHTOOL_MSG_EEE_NTF, .cb = eee_reply_cb, }, + { + .cmd = ETHTOOL_MSG_CABLE_TEST_NTF, + .cb = cable_test_ntf_cb, + }, };static void clear_filter (struct nl_context *nlctx)
@@ -158,6 +162,10 @@ static struct monitor_option monitor_opts[] = { .pattern = "--show-eee|--set-eee", .cmd = ETHTOOL_MSG_EEE_NTF, }, + { + .pattern = "--cable-test", + .cmd = ETHTOOL_MSG_CABLE_TEST_NTF, + }, };static bool pattern_match (const char *s,const char *pattern)
netlink/netlink.h +2 -0
@@ -78,6 +78,8 @@ int channels_reply_cb(const struct nlmsghdr *nlhdr, void *data);int coalesce_reply_cb (const struct nlmsghdr *nlhdr,void *data);int pause_reply_cb (const struct nlmsghdr *nlhdr,void *data);int eee_reply_cb (const struct nlmsghdr *nlhdr,void *data); +int cable_test_reply_cb (const struct nlmsghdr *nlhdr,void *data); +int cable_test_ntf_cb (const struct nlmsghdr *nlhdr,void *data);/* dump helpers */
Signature · 1 line
2.27.0
PATCH v5 2/6 Add cable test TDR support
In reply to Andrew LunnAdd support for accessing the cable test time domain reflectromatry data. Add a new command --cable-test-tdr, and support for dumping the data which is returned.
--- ethtool.c | 9 ++ netlink/cable_test.c | 296 +++++++++++++++++++++++++++++++++++++++++++ netlink/extapi.h | 2 + netlink/monitor.c | 8 ++ netlink/netlink.h | 2 + netlink/parser.c | 42 ++++++ netlink/parser.h | 4 + 7 files changed, 363 insertions(+)
ethtool.c +9 -0
@@ -5487,6 +5487,15 @@ static const struct option args[] = { .nlfunc = nl_cable_test, .help = "Perform a cable test", }, + { + .opts = "--cable-test-tdr", + .nlfunc = nl_cable_test_tdr, + .help = "Print cable test time domain reflectrometery data", + .xhelp =" [ first N ]\n" +" [ last N ]\n" +" [ step N ]\n" +" [ pair N ]\n" + }, { .opts = "-h|--help", .no_dev = true,
netlink/cable_test.c +296 -0
305 lines not on this page · raw message
netlink/extapi.h +2 -0
@@ -36,6 +36,7 @@ int nl_geee(struct cmd_context *ctx);int nl_seee (struct cmd_context *ctx);int nl_tsinfo (struct cmd_context *ctx);int nl_cable_test (struct cmd_context *ctx); +int nl_cable_test_tdr (struct cmd_context *ctx);int nl_monitor (struct cmd_context *ctx);void nl_monitor_usage (void );
@@ -76,6 +77,7 @@ static inline void nl_monitor_usage(void)#define nl_seee NULL#define nl_tsinfo NULL#define nl_cable_test NULL +#define nl_cable_test_tdr NULL#endif /* ETHTOOL_ENABLE_NETLINK */
netlink/monitor.c +8 -0
@@ -63,6 +63,10 @@ static struct { .cmd = ETHTOOL_MSG_CABLE_TEST_NTF, .cb = cable_test_ntf_cb, }, + { + .cmd = ETHTOOL_MSG_CABLE_TEST_TDR_NTF, + .cb = cable_test_tdr_ntf_cb, + }, };static void clear_filter (struct nl_context *nlctx)
@@ -166,6 +170,10 @@ static struct monitor_option monitor_opts[] = { .pattern = "--cable-test", .cmd = ETHTOOL_MSG_CABLE_TEST_NTF, }, + { + .pattern = "--cable-test-tdr", + .cmd = ETHTOOL_MSG_CABLE_TEST_TDR_NTF, + }, };static bool pattern_match (const char *s,const char *pattern)
netlink/netlink.h +2 -0
@@ -80,6 +80,8 @@ int pause_reply_cb(const struct nlmsghdr *nlhdr, void *data);int eee_reply_cb (const struct nlmsghdr *nlhdr,void *data);int cable_test_reply_cb (const struct nlmsghdr *nlhdr,void *data);int cable_test_ntf_cb (const struct nlmsghdr *nlhdr,void *data); +int cable_test_tdr_reply_cb (const struct nlmsghdr *nlhdr,void *data); +int cable_test_tdr_ntf_cb (const struct nlmsghdr *nlhdr,void *data);/* dump helpers */
netlink/parser.c +42 -0
54 lines not on this page · raw message
netlink/parser.h +4 -0
@@ -111,6 +111,10 @@ int nl_parse_direct_u32(struct nl_context *nlctx, uint16_t type,int nl_parse_direct_u8 (struct nl_context *nlctx,uint16_t type,const void *data,struct nl_msg_buff *msgbuff,void *dest); +/* NLA_U32 represented as float number of meters, converted to cm. */ +int nl_parse_direct_m2cm (struct nl_context *nlctx,uint16_t type, +const void *data,struct nl_msg_buff *msgbuff, +void *dest);/* NLA_U8 represented as on | off */ int nl_parse_u8bool (struct nl_context *nlctx,uint16_t type,const void *data,struct nl_msg_buff *msgbuff,void *dest);
Signature · 1 line
2.27.0
Re: [ethtool v5 2/6] Add cable test TDR support
Review
Quoted 13 lines · Andrew Lunn
(quoted diff) +13
-0+/* Receive the broadcasted messages until we get the cable test + * results + */ +static int nl_cable_test_tdr_process_results(struct cmd_context *ctx) +{ + struct nl_context *nlctx = ctx->nlctx; + struct nl_socket *nlsk = nlctx->ethnl_socket; + struct cable_test_context ctctx; + int err; + + nlctx->is_monitor = true; + nlsk->port = 0; + nlsk->seq = 0;
Andrew, is this missing the following patch?
netlink/cable_test.c +2 -0
@@ -225,6 +225,7 @@ static int nl_cable_test_process_results(struct cmd_context *ctx) nlctx->is_monitor = true; nlsk->port =0 ; nlsk->seq =0 ; + nlctx->filter_devname = ctx->devname; ctctx.breakout = false; nlctx->cmd_private = &ctctx;
@@ -496,6 +497,7 @@ static int nl_cable_test_tdr_process_results(struct cmd_context *ctx) nlctx->is_monitor = true; nlsk->port =0 ; nlsk->seq =0 ; + nlctx->filter_devname = ctx->devname; ctctx.breakout = false; nlctx->cmd_private = &ctctx;
I don't have hardware with cable test support so wondered if you could test it. I think that without this patch you would see problems with two simultaneous cable tests. The first one to finish will terminate both ethtool processes because the code is processing all cable tests notifications regardless of the device for which the test was issued.
Context: I'm using a similar scheme for transceiver module firmware update in order to support simultaneous update of several modules.
Quoted 13 lines
(quoted diff) +13
-0+ + ctctx.breakout = false; + nlctx->cmd_private = &ctctx; + + while (!ctctx.breakout) { + err = nlsock_process_reply(nlsk, nl_cable_test_tdr_results_cb, + nlctx); + if (err) + return err; + } + + return err; +}
Andrew Lunn
Reviewed-byRe: [ethtool v5 2/6] Add cable test TDR support
Quoted 23 lines
Andrew, is this missing the following patch?
netlink/cable_test.c +2
-0@@ -225,6 +225,7 @@ static int nl_cable_test_process_results(struct cmd_context *ctx) nlctx->is_monitor = true; nlsk->port =0 ; nlsk->seq =0 ; + nlctx->filter_devname = ctx->devname; ctctx.breakout = false; nlctx->cmd_private = &ctctx;@@ -496,6 +497,7 @@ static int nl_cable_test_tdr_process_results(struct cmd_context *ctx) nlctx->is_monitor = true; nlsk->port =0 ; nlsk->seq =0 ; + nlctx->filter_devname = ctx->devname; ctctx.breakout = false; nlctx->cmd_private = &ctctx;
Quoted 2 lines
I don't have hardware with cable test support so wondered if you could test it.
Hi Ido
I've tested this. No obvious regressions. My broken cable is still broken.
Quoted 4 lines
I think that without this patch you would see problems with two simultaneous cable tests. The first one to finish will terminate both ethtool processes because the code is processing all cable tests notifications regardless of the device for which the test was issued.
I did not test such a setup. But you are correct, the message from the kernel is broadcast, and any waiting process will consume it, even if it is for a different device.
Andrew
PATCH v5 3/6 json_writer/json_print: Import the iproute2 helper code for JSON output
In reply to Andrew LunnIn general, Linux network tools use JSON for machine readable output. See for example -json for iproute2 and devlink. In order to support JSON output from ethtool, import the iproute2 helper code.
--- Makefile.am | 3 +- json_print.c | 228 +++++++++++++++++++++++++++++ json_print.h | 67 +++++++++ json_writer.c | 391 ++++++++++++++++++++++++++++++++++++++++++++++++++ json_writer.h | 76 ++++++++++ 5 files changed, 764 insertions(+), 1 deletion(-) create mode 100644 json_print.c create mode 100644 json_print.h create mode 100644 json_writer.c create mode 100644 json_writer.h
Makefile.am +2 -1
@@ -7,7 +7,8 @@ EXTRA_DIST = LICENSE ethtool.8 ethtool.spec.in aclocal.m4 ChangeLog autogen.sh sbin_PROGRAMS = ethtool ethtool_SOURCES = ethtool.c uapi/linux/ethtool.h internal.h \ - uapi/linux/net_tstamp.h rxclass.c common.c common.h + uapi/linux/net_tstamp.h rxclass.c common.c common.h \ + json_writer.c json_writer.h json_print.c json_print.h if ETHTOOL_ENABLE_PRETTY_DUMP ethtool_SOURCES += \ amd8111e.c de2104x.c dsa.c e100.c e1000.c et131x.c igb.c \
json_print.c new file +228 -0
228 lines not on this page · raw message
json_print.h new file +67 -0
67 lines not on this page · raw message
json_writer.c new file +391 -0
391 lines not on this page · raw message
json_writer.h new file +76 -0
76 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH v5 4/6 Add --json command line argument parsing
In reply to Andrew LunnAllow --json to be passed as an option to select JSON output. The option is handled in the same way as --debug, setting a variable in the command context, which can then later be used per option to select JSON outputters.
--- ethtool.c | 33 ++++++++++------ internal.h | 4 ++ netlink/cable_test.c | 92 ++++++++++++++++++++++++++++++-------------- 3 files changed, 90 insertions(+), 39 deletions(-)
ethtool.c +22 -11
@@ -5519,10 +5519,10 @@ static int show_usage(struct cmd_context *ctx maybe_unused)fprintf (stdout,PACKAGE " version " VERSION "\n" );fprintf (stdout,"Usage:\n" -" ethtool [ --debug MASK ] DEVNAME\t" +" ethtool [ --debug MASK ][ --json ] DEVNAME\t" "Display standard information about device\n" );for (i =0 ; args[i].opts; i++) { -fputs (" ethtool [ --debug MASK ] " , stdout); +fputs (" ethtool [ --debug MASK ][ --json ] " , stdout); fprintf(stdout, "%s %s\t%s\n", args[i].opts, args[i].no_dev ? "\t" : "DEVNAME",
@@ -5531,6 +5531,7 @@ static int show_usage(struct cmd_context *ctx maybe_unused)fputs (args[i].xhelp, stdout); }nl_monitor_usage (); +fprintf (stdout,"Not all options support JSON output\n" );return 0 ; }
@@ -5769,17 +5770,27 @@ int main(int argc, char **argp) argp++; argc--; -if (*argp && !strcmp (*argp,"--debug" )) { -char *eptr; +while (true) { +if (*argp && !strcmp (*argp,"--debug" )) { +char *eptr; -if (argc <2 ) -exit_bad_args (); - ctx.debug =strtoul (argp[1 ], &eptr,0 ); -if (!argp[1 ][0 ] || *eptr) -exit_bad_args (); +if (argc <2 ) +exit_bad_args (); + ctx.debug =strtoul (argp[1 ], &eptr,0 ); +if (!argp[1 ][0 ] || *eptr) +exit_bad_args (); - argp +=2 ; - argc -=2 ; + argp +=2 ; + argc -=2 ; +continue ; + } +if (*argp && !strcmp (*argp,"--json" )) { + ctx.json = true; + argp +=1 ; + argc -=1 ; +continue ; + } +break ; }if (*argp && !strcmp (*argp,"--monitor" )) { ctx.argp = ++argp;
internal.h +4 -0
@@ -23,6 +23,9 @@#include <sys/ioctl.h> #include <net/if.h> +#include "json_writer.h" +#include "json_print.h" +#define maybe_unused __attribute__((__unused__))/* internal for netlink interface */
@@ -221,6 +224,7 @@ struct cmd_context {int argc;/* number of arguments to the sub-command */ char **argp;/* arguments to the sub-command */ unsigned long debug;/* debugging mask */ +bool json;/* Output JSON, if supported */ #ifdef ETHTOOL_ENABLE_NETLINK struct nl_context *nlctx;/* netlink context (opaque) */ #endif
netlink/cable_test.c +64 -28
201 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH v5 5/6 ethtool.8.in: Document the cable test commands
In reply to Andrew LunnExtend the man page with --cable-test and --cable-test-tdr commands.
---
v2: Fixup some grammar/English errors.
v3: More grammar/English errors fixed.
--- ethtool.8.in | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)
ethtool.8.in +45 -0
@@ -434,6 +434,16 @@ ethtool \- query or control network driver and hardware settings .I sub_command .RB ... . +.HP +.B ethtool \-\-cable\-test +.I devname +.HP +.B ethtool \-\-cable\-test\-tdr +.I devname +.BN first N +.BN last N +.BN step N +.BN pair N . .\" Adjust lines (i.e. full justification) and hyphenate. .ad
@@ -1277,6 +1287,41 @@ Sub command to apply. The supported sub commands include --show-coalesce and --coalesce. .RE .TP +q.B \-\-cable\-test +Perform a cable test and report the results. What results are returned depends +on the capabilities of the network interface. Typically open pairs and shorted +pairs can be reported, along with pairs being O.K. When a fault is detected +the approximate distance to the fault may be reported. +.TP +.B \-\-cable\-test\-tdr +Perform a cable test and report the raw Time Domain Reflectometer +data. A pulse is sent down a cable pair and the amplitude of the +reflection, for a given distance, is reported. A break in the cable +returns a big reflection. Minor damage to the cable returns a small +reflection. If the cable is shorted, the amplitude of the reflection +can be negative. By default, data is returned for lengths between 0 +and 150m at 1m steps, for all pairs. However parameters can be passed +to restrict the collection of data. It should be noted, that the +interface will round the distances to whatever granularity is actually +implemented. This is often 0.8 of a meter. The results should include +the actual rounded first and last distance and step size. +.RS 4 +.TP +.B first \ N +Distance along the cable, in meters, where the first measurement +should be made. +.TP +.B last \ N +Distance along the cable, in meters, where the last measurement should +be made. +.TP +.B step \ N +Distance, in meters, between each measurement. +.TP +.B pair \ N +Which pair should be measured. Typically a cable has 4 pairs. 0 = Pair A, 1 = Pair B, ... +.RE +.TP .B \-\-monitor Listens to netlink notification and displays them. .RS 4
Signature · 1 line
2.27.0
PATCH v5 6/6 ethtool.8.in: Add --json option
In reply to Andrew LunnDocument the --json option, which the --cable-test and --cable-test-tdr options support.
--- ethtool.8.in | 8 ++++++++ 1 file changed, 8 insertions(+)
ethtool.8.in +8 -0
@@ -137,6 +137,9 @@ ethtool \- query or control network driver and hardware settings .BN --debug .I args .HP +.B ethtool [--json] +.I args +.HP .B ethtool \-\-monitor [ .I command
@@ -476,6 +479,11 @@ lB l. 0x01 Parser information .TE .TP +.BI \-\-json +Output results in JavaScript Object Notation (JSON). Only a subset of +options support this. Those which do not will continue to output +plain text in the presence of this option. +.TP .B \-a \-\-show\-pause Queries the specified Ethernet device for pause parameter information. .TP
Signature · 1 line
2.27.0
Quoted 19 lines · Andrew Lunn
Add the user space side of the ethtool cable test.
The TDR output is most useful when fed to some other tool which can visualize the data. So add JSON support, by borrowing code from iproute2.
v2:
man page fixes.v3:
More man page fixes.
Usejson_printfrom iproute2.v4:
checkpatch cleanup
ethtool --cable-test dev
Place breakout intocable_test_context
Remove Pair: Pair output
Quoted 4 lines
v5:
Add missing pair in help text
Allow --cable-test|--cable-test-tdr with --monitor
Fix rounding when converting from floating point meters to centimeters
Series applied, thank you.
Michal