ethtool: Improve compatibility between netlink and ioctl interfaces
Subsystems (2)
RFC PATCH 1/1 ethtool: Improve compatibility between netlink and ioctl interfaces
From: Ido Schimmel <[email protected]>
With the ioctl interface, when autoneg is enabled, but without specifying speed, duplex or link modes, the advertised link modes are set to the supported link modes by the ethtool user space utility.
This does not happen when using the netlink interface. Fix this incompatibility problem by having ethtool query the supported link modes from the kernel and advertise all of them when only "autoneg on" is specified.
Before:
# ethtool -s eth0 advertise 0xC autoneg on
# ethtool -s eth0 autoneg on
# ethtool eth0
Settings for eth0:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Supported pause frame use: No
Supports auto-negotiation: Yes
Supported FEC modes: Not reported
Advertised link modes: 100baseT/Half 100baseT/Full
Advertised pause frame use: No
Advertised auto-negotiation: Yes
Advertised FEC modes: Not reported
Speed: 1000Mb/s
Duplex: Full
Auto-negotiation: on
Port: Twisted Pair
PHYAD: 0
Transceiver: internal
MDI-X: off (auto)
Supports Wake-on: umbg
Wake-on: d
Current message level: 0x00000007 (7)
drv probe link
Link detected: yesAfter:
# ethtool -s eth0 advertise 0xC autoneg on
# ethtool -s eth0 autoneg on
# ethtool eth0
Settings for eth0:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Supported pause frame use: No
Supports auto-negotiation: Yes
Supported FEC modes: Not reported
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Advertised pause frame use: No
Advertised auto-negotiation: Yes
Advertised FEC modes: Not reported
Speed: 1000Mb/s
Duplex: Full
Auto-negotiation: on
Port: Twisted Pair
PHYAD: 0
Transceiver: internal
MDI-X: on (auto)
Supports Wake-on: umbg
Wake-on: d
Current message level: 0x00000007 (7)
drv probe link
Link detected: yes---
Michal / Jakub, let me know if you see a better way. Sending as RFC since I want to run it through regression first.
--- netlink/settings.c | 115 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+)
netlink/settings.c +115 -0
@@ -1110,6 +1110,113 @@ static const struct param_parser sset_params[] = { {} }; +static bool sset_is_autoneg_only (const struct nl_context *nlctx) +{ +return nlctx->argc ==2 && !strcmp (nlctx->argp[0 ],"autoneg" ) && + !strcmp (nlctx->argp[1 ],"on" ); +} + +static int linkmodes_reply_adver_all_cb (const struct nlmsghdr *nlhdr, +void *data) +{ +const struct nlattr *bitset_tb[ETHTOOL_A_BITSET_MAX +1 ] = {}; +const struct nlattr *tb[ETHTOOL_A_LINKMODES_MAX +1 ] = {}; +DECLARE_ATTR_TB_INFO (bitset_tb); +struct nl_context *nlctx = data; +struct nl_msg_buff *msgbuff; +DECLARE_ATTR_TB_INFO (tb); +struct nl_socket *nlsk; +struct nlattr *nest; +int ret; + + ret =mnl_attr_parse (nlhdr,GENL_HDRLEN , attr_cb, &tb_info); +if (ret <0 ) +return ret; +if (!tb[ETHTOOL_A_LINKMODES_OURS ]) +return -EINVAL ; + + ret =mnl_attr_parse_nested (tb[ETHTOOL_A_LINKMODES_OURS ], attr_cb, + &bitset_tb_info); +if (ret <0 ) +return ret; +if (!bitset_tb[ETHTOOL_A_BITSET_SIZE ] || + !bitset_tb[ETHTOOL_A_BITSET_VALUE ] || + !bitset_tb[ETHTOOL_A_BITSET_MASK ]) +return -EINVAL ; + + ret =netlink_init_ethnl2_socket (nlctx); +if (ret <0 ) +return ret; + + nlsk = nlctx->ethnl2_socket; + msgbuff = &nlsk->msgbuff; + + ret =msg_init (nlctx, msgbuff,ETHTOOL_MSG_LINKMODES_SET , +NLM_F_REQUEST |NLM_F_ACK ); +if (ret <0 ) +return ret; +if (ethnla_fill_header (msgbuff,ETHTOOL_A_LINKMODES_HEADER , + nlctx->devname,0 )) +return -EMSGSIZE ; + +if (ethnla_put_u8 (msgbuff,ETHTOOL_A_LINKMODES_AUTONEG ,AUTONEG_ENABLE )) +return -EMSGSIZE ; + +/* Use the size and mask from the reply and set the value to the mask, +* so that all supported link modes will be advertised. +*/ + ret = -EMSGSIZE ; + nest =ethnla_nest_start (msgbuff,ETHTOOL_A_LINKMODES_OURS ); +if (!nest) +return -EMSGSIZE ; + +if (ethnla_put_u32 (msgbuff,ETHTOOL_A_BITSET_SIZE , +mnl_attr_get_u32 (bitset_tb[ETHTOOL_A_BITSET_SIZE ]))) + goto err; + +if (ethnla_put (msgbuff,ETHTOOL_A_BITSET_VALUE , +mnl_attr_get_payload_len (bitset_tb[ETHTOOL_A_BITSET_MASK ]), +mnl_attr_get_payload (bitset_tb[ETHTOOL_A_BITSET_MASK ]))) + goto err; + +if (ethnla_put (msgbuff,ETHTOOL_A_BITSET_MASK , +mnl_attr_get_payload_len (bitset_tb[ETHTOOL_A_BITSET_MASK ]), +mnl_attr_get_payload (bitset_tb[ETHTOOL_A_BITSET_MASK ]))) + goto err; + +ethnla_nest_end (msgbuff, nest); + + ret =nlsock_sendmsg (nlsk,NULL ); +if (ret <0 ) +return ret; + ret =nlsock_process_reply (nlsk, nomsg_reply_cb, nlctx); +if (ret <0 ) +return ret; + +return MNL_CB_OK ; + +err: +ethnla_nest_cancel (msgbuff, nest); +return ret; +} + +static int sset_adver_all (struct nl_context *nlctx) +{ +struct nl_socket *nlsk = nlctx->ethnl_socket; +int ret; + +if (netlink_cmd_check (nlctx->ctx,ETHTOOL_MSG_LINKMODES_GET , false) || +netlink_cmd_check (nlctx->ctx,ETHTOOL_MSG_LINKMODES_SET , false)) +return -EOPNOTSUPP ; + + ret =nlsock_prep_get_request (nlsk,ETHTOOL_MSG_LINKMODES_GET , +ETHTOOL_A_LINKMODES_HEADER , +ETHTOOL_FLAG_COMPACT_BITSETS ); +if (ret <0 ) +return ret; +return nlsock_send_get_request (nlsk, linkmodes_reply_adver_all_cb); +} +int nl_sset (struct cmd_context *ctx) {struct nl_context *nlctx = ctx->nlctx;
@@ -1120,6 +1227,14 @@ int nl_sset(struct cmd_context *ctx) nlctx->argc = ctx->argc; nlctx->devname = ctx->devname; +/* For compatibility reasons with ioctl-based ethtool, when "autoneg +* on" is specified without "advertise", "speed" and "duplex", we need +* to query the supported link modes from the kernel and advertise all +* of them. +*/ +if (sset_is_autoneg_only (nlctx)) +return sset_adver_all (nlctx); + ret =nl_parser (nlctx, sset_params,NULL ,PARSER_GROUP_MSG );if (ret <0 )return 1 ;
Signature · 1 line
2.26.2
Quoted 86 lines · Ido Schimmel
From: Ido Schimmel <[email protected]>
With the ioctl interface, when autoneg is enabled, but without specifying speed, duplex or link modes, the advertised link modes are set to the supported link modes by the ethtool user space utility.
This does not happen when using the netlink interface. Fix this incompatibility problem by having ethtool query the supported link modes from the kernel and advertise all of them when only "autoneg on" is specified.
Before:
# ethtool -s eth0 advertise 0xC autoneg on # ethtool -s eth0 autoneg on # ethtool eth0 Settings for eth0: Supported ports: [ TP ] Supported link modes: 10baseT/Half 10baseT/Full 100baseT/Half 100baseT/Full 1000baseT/Full Supported pause frame use: No Supports auto-negotiation: Yes Supported FEC modes: Not reported Advertised link modes: 100baseT/Half 100baseT/Full Advertised pause frame use: No Advertised auto-negotiation: Yes Advertised FEC modes: Not reported Speed: 1000Mb/s Duplex: Full Auto-negotiation: on Port: Twisted Pair PHYAD: 0 Transceiver: internal MDI-X: off (auto) Supports Wake-on: umbg Wake-on: d Current message level: 0x00000007 (7) drv probe link Link detected: yesAfter:
# ethtool -s eth0 advertise 0xC autoneg on # ethtool -s eth0 autoneg on # ethtool eth0 Settings for eth0: Supported ports: [ TP ] Supported link modes: 10baseT/Half 10baseT/Full 100baseT/Half 100baseT/Full 1000baseT/Full Supported pause frame use: No Supports auto-negotiation: Yes Supported FEC modes: Not reported Advertised link modes: 10baseT/Half 10baseT/Full 100baseT/Half 100baseT/Full 1000baseT/Full Advertised pause frame use: No Advertised auto-negotiation: Yes Advertised FEC modes: Not reported Speed: 1000Mb/s Duplex: Full Auto-negotiation: on Port: Twisted Pair PHYAD: 0 Transceiver: internal MDI-X: on (auto) Supports Wake-on: umbg Wake-on: d Current message level: 0x00000007 (7) drv probe link Link detected: yesSigned-off-by Ido Schimmel <[email protected]>---
Michal / Jakub, let me know if you see a better way. Sending as RFC since I want to run it through regression first.--- netlink/settings.c | 115 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+)netlink/settings.c +0
-0
netlink/settings.c static const struct param_parser sset_params[] = { · line 1117
Quoted 8 lines
netlink/settings.c +5
-0@@ -1110,6 +1110,113 @@ static const struct param_parser sset_params[] = { {} }; +static bool sset_is_autoneg_only (const struct nl_context *nlctx) +{ +return nlctx->argc ==2 && !strcmp (nlctx->argp[0 ],"autoneg" ) && + !strcmp (nlctx->argp[1 ],"on" ); +}
This would only return true if there is only "autoneg on" on command line; the ioctl parser fills all supported modes whenever none of "speed", "duplex" and "advertise" is present, even if there are other parameters not related to advertised modes selection (e.g. "wol").
Doing this properly from command line would require us to duplicate the parser here which would be very inconvenient and impractical w.r.t. future extensions.
What would probably make more sense would be modifying nl_parse() not to send the messages itself in PARSER_GROUP_MSG case but return them back to caller (like it does in other cases when there is only one message). Then we could check the ETHTOOL_MSG_LINKMODES_SET message (if there is one) for presence of ETHTOOL_A_LINKMODES_AUTONEG, ETHTOOL_A_LINKMODES_OURS, ETHTOOL_A_LINKMODES_SPEED and ETHTOOL_A_LINKMODES_DUPLEX (and value of ETHTOOL_A_LINKMODES_AUTONEG) and add ETHTOOL_A_LINKMODES_OURS if needed.
I'll prepare a proof of concept of such nl_parse() rework. Fortunately this is the only subcommand using PARSER_GROUP_MSG so that there are no other callers that would need adjusting.
netlink/settings.c static const struct param_parser sset_params[] = { · line 1119
Quoted 1 line
netlink/settings.c +1
-0+static int linkmodes_reply_adver_all_cb(const struct nlmsghdr *nlhdr,
^^^^^ advert?
Quoted 58 lines
netlink/settings.c +58
-0+ void *data) +{ +const struct nlattr *bitset_tb[ETHTOOL_A_BITSET_MAX +1 ] = {}; +const struct nlattr *tb[ETHTOOL_A_LINKMODES_MAX +1 ] = {}; +DECLARE_ATTR_TB_INFO (bitset_tb); +struct nl_context *nlctx = data; +struct nl_msg_buff *msgbuff; +DECLARE_ATTR_TB_INFO (tb); +struct nl_socket *nlsk; +struct nlattr *nest; +int ret; + + ret =mnl_attr_parse (nlhdr,GENL_HDRLEN , attr_cb, &tb_info); +if (ret <0 ) +return ret; +if (!tb[ETHTOOL_A_LINKMODES_OURS ]) +return -EINVAL ; + + ret =mnl_attr_parse_nested (tb[ETHTOOL_A_LINKMODES_OURS ], attr_cb, + &bitset_tb_info); +if (ret <0 ) +return ret; +if (!bitset_tb[ETHTOOL_A_BITSET_SIZE ] || + !bitset_tb[ETHTOOL_A_BITSET_VALUE ] || + !bitset_tb[ETHTOOL_A_BITSET_MASK ]) +return -EINVAL ; + + ret =netlink_init_ethnl2_socket (nlctx); +if (ret <0 ) +return ret; + + nlsk = nlctx->ethnl2_socket; + msgbuff = &nlsk->msgbuff; + + ret =msg_init (nlctx, msgbuff,ETHTOOL_MSG_LINKMODES_SET , +NLM_F_REQUEST |NLM_F_ACK ); +if (ret <0 ) +return ret; +if (ethnla_fill_header (msgbuff,ETHTOOL_A_LINKMODES_HEADER , + nlctx->devname,0 )) +return -EMSGSIZE ; + +if (ethnla_put_u8 (msgbuff,ETHTOOL_A_LINKMODES_AUTONEG ,AUTONEG_ENABLE )) +return -EMSGSIZE ; + +/* Use the size and mask from the reply and set the value to the mask, +* so that all supported link modes will be advertised. +*/ + ret = -EMSGSIZE ; + nest =ethnla_nest_start (msgbuff,ETHTOOL_A_LINKMODES_OURS ); +if (!nest) +return -EMSGSIZE ; + +if (ethnla_put_u32 (msgbuff,ETHTOOL_A_BITSET_SIZE , +mnl_attr_get_u32 (bitset_tb[ETHTOOL_A_BITSET_SIZE ]))) + goto err; + + if (ethnla_put(msgbuff, ETHTOOL_A_BITSET_VALUE,
netlink/settings.c static const struct param_parser sset_params[] = { · line 1187
Quoted 10 lines
netlink/settings.c +10
-0+mnl_attr_get_payload_len (bitset_tb[ETHTOOL_A_BITSET_MASK ]), + mnl_attr_get_payload(bitset_tb[ETHTOOL_A_BITSET_MASK]))) + goto err; + +if (ethnla_put (msgbuff,ETHTOOL_A_BITSET_MASK , +mnl_attr_get_payload_len (bitset_tb[ETHTOOL_A_BITSET_MASK ]), +mnl_attr_get_payload (bitset_tb[ETHTOOL_A_BITSET_MASK ]))) + goto err; + +ethnla_nest_end (msgbuff, nest);
To fully replicate ioctl code behaviour, we should only set the bits corresponding to "real" link modes, not "special" ones (e.g. ETHTOOL_LINK_MODE_TP_BIT).
Quoted 6 lines
netlink/settings.c +6
-0+ + ret =nlsock_sendmsg (nlsk,NULL ); +if (ret <0 ) +return ret; + ret =nlsock_process_reply (nlsk, nomsg_reply_cb, nlctx); +if (ret <0 )
netlink/settings.c static const struct param_parser sset_params[] = { · line 1203
Quoted 10 lines
netlink/settings.c +10
-0+return ret; + +return MNL_CB_OK ; + +err: +ethnla_nest_cancel (msgbuff, nest); +return ret; +} + +static int sset_adver_all (struct nl_context *nlctx)
^^^^^ advert?
Michal
Quoted 37 lines
netlink/settings.c +24
-0+{ +struct nl_socket *nlsk = nlctx->ethnl_socket; +int ret; + +if (netlink_cmd_check (nlctx->ctx,ETHTOOL_MSG_LINKMODES_GET , false) || +netlink_cmd_check (nlctx->ctx,ETHTOOL_MSG_LINKMODES_SET , false)) +return -EOPNOTSUPP ; + + ret =nlsock_prep_get_request (nlsk,ETHTOOL_MSG_LINKMODES_GET , +ETHTOOL_A_LINKMODES_HEADER , +ETHTOOL_FLAG_COMPACT_BITSETS ); +if (ret <0 ) +return ret; +return nlsock_send_get_request (nlsk, linkmodes_reply_adver_all_cb); +} +int nl_sset (struct cmd_context *ctx) {struct nl_context *nlctx = ctx->nlctx;@@ -1120,6 +1227,14 @@ int nl_sset(struct cmd_context *ctx) nlctx->argc = ctx->argc; nlctx->devname = ctx->devname; +/* For compatibility reasons with ioctl-based ethtool, when "autoneg +* on" is specified without "advertise", "speed" and "duplex", we need +* to query the supported link modes from the kernel and advertise all +* of them. +*/ +if (sset_is_autoneg_only (nlctx)) +return sset_adver_all (nlctx); + ret =nl_parser (nlctx, sset_params,NULL ,PARSER_GROUP_MSG );if (ret <0 )return 1 ;Signature · 1 line
2.26.2
Quoted 69 lines · Michal Kubecek
Quoted 1 line · Ido Schimmel
+static int linkmodes_reply_adver_all_cb(const struct nlmsghdr *nlhdr,
^^^^^ advert?Quoted 64 lines
netlink/settings.c +64
-0+ void *data) +{ +const struct nlattr *bitset_tb[ETHTOOL_A_BITSET_MAX +1 ] = {}; +const struct nlattr *tb[ETHTOOL_A_LINKMODES_MAX +1 ] = {}; +DECLARE_ATTR_TB_INFO (bitset_tb); +struct nl_context *nlctx = data; +struct nl_msg_buff *msgbuff; +DECLARE_ATTR_TB_INFO (tb); +struct nl_socket *nlsk; +struct nlattr *nest; +int ret; + + ret =mnl_attr_parse (nlhdr,GENL_HDRLEN , attr_cb, &tb_info); +if (ret <0 ) +return ret; +if (!tb[ETHTOOL_A_LINKMODES_OURS ]) +return -EINVAL ; + + ret =mnl_attr_parse_nested (tb[ETHTOOL_A_LINKMODES_OURS ], attr_cb, + &bitset_tb_info); +if (ret <0 ) +return ret; +if (!bitset_tb[ETHTOOL_A_BITSET_SIZE ] || + !bitset_tb[ETHTOOL_A_BITSET_VALUE ] || + !bitset_tb[ETHTOOL_A_BITSET_MASK ]) +return -EINVAL ; + + ret =netlink_init_ethnl2_socket (nlctx); +if (ret <0 ) +return ret; + + nlsk = nlctx->ethnl2_socket; + msgbuff = &nlsk->msgbuff; + + ret =msg_init (nlctx, msgbuff,ETHTOOL_MSG_LINKMODES_SET , +NLM_F_REQUEST |NLM_F_ACK ); +if (ret <0 ) +return ret; +if (ethnla_fill_header (msgbuff,ETHTOOL_A_LINKMODES_HEADER , + nlctx->devname,0 )) +return -EMSGSIZE ; + +if (ethnla_put_u8 (msgbuff,ETHTOOL_A_LINKMODES_AUTONEG ,AUTONEG_ENABLE )) +return -EMSGSIZE ; + +/* Use the size and mask from the reply and set the value to the mask, +* so that all supported link modes will be advertised. +*/ + ret = -EMSGSIZE ; + nest =ethnla_nest_start (msgbuff,ETHTOOL_A_LINKMODES_OURS ); +if (!nest) +return -EMSGSIZE ; + +if (ethnla_put_u32 (msgbuff,ETHTOOL_A_BITSET_SIZE , +mnl_attr_get_u32 (bitset_tb[ETHTOOL_A_BITSET_SIZE ]))) + goto err; + +if (ethnla_put (msgbuff,ETHTOOL_A_BITSET_VALUE , +mnl_attr_get_payload_len (bitset_tb[ETHTOOL_A_BITSET_MASK ]), +mnl_attr_get_payload (bitset_tb[ETHTOOL_A_BITSET_MASK ]))) + goto err; + + if (ethnla_put(msgbuff, ETHTOOL_A_BITSET_MASK, + mnl_attr_get_payload_len(bitset_tb[ETHTOOL_A_BITSET_MASK]),
Quoted 8 lines
netlink/settings.c static const struct param_parser sset_params[] = { · line 1187
Quoted 4 lines
netlink/settings.c +4
-0+ mnl_attr_get_payload(bitset_tb[ETHTOOL_A_BITSET_MASK]))) + goto err; + +ethnla_nest_end (msgbuff, nest);To fully replicate ioctl code behaviour, we should only set the bits corresponding to "real" link modes, not "special" ones (e.g.
ETHTOOL_LINK_MODE_TP_BIT).
Michal,
I have the changes you requested here:
https://github.com/idosch/ethtool/commit/b34d15839f2662808c566c04eda726113e20ee59
Do you want to integrate it with your nl_parse() rework or should I?
Thanks
Quoted 5 lines · Ido Schimmel
I have the changes you requested here:
https://github.com/idosch/ethtool/commit/b34d15839f2662808c566c04eda726113e20ee59Do you want to integrate it with your
nl_parse()rework or should I?
I pushed the combined series to
https://git.kernel.org/pub/scm/linux/kernel/git/mkubecek/ethtool.git
as branch mk/master/advertise-all. I only ran few quick tests so far, it's not submission ready yet.
First two patches are unrelated fixes found while testing, I'm going to submit and push them separately. Third patch reworks nl_parser() handling of multiple request messages as indicated in my previous mail. Fourth patch is the ioctl compatibility fix.
Michal
Quoted 11 lines · Michal Kubecek
Quoted 5 lines · Ido Schimmel
I have the changes you requested here:
https://github.com/idosch/ethtool/commit/b34d15839f2662808c566c04eda726113e20ee59Do you want to integrate it with your
nl_parse()rework or should I?I pushed the combined series to
https://git.kernel.org/pub/scm/linux/kernel/git/mkubecek/ethtool.git
Quoted 7 lines
as branch mk/master/advertise-all. I only ran few quick tests so far, it's not submission ready yet.
First two patches are unrelated fixes found while testing, I'm going to submit and push them separately. Third patch reworks
nl_parser()handling of multiple request messages as indicated in my previous mail. Fourth patch is the ioctl compatibility fix.
Great, thank you. I pushed the patches to our regression. Will go over the results tomorrow and let you know.
Quoted 15 lines · Ido Schimmel
Quoted 14 lines · Michal Kubecek
Quoted 5 lines · Ido Schimmel
I have the changes you requested here:
https://github.com/idosch/ethtool/commit/b34d15839f2662808c566c04eda726113e20ee59Do you want to integrate it with your
nl_parse()rework or should I?I pushed the combined series to
https://git.kernel.org/pub/scm/linux/kernel/git/mkubecek/ethtool.git
as branch mk/master/advertise-all. I only ran few quick tests so far, it's not submission ready yet.
Quoted 7 lines
Quoted 4 lines
First two patches are unrelated fixes found while testing, I'm going to submit and push them separately. Third patch reworks
nl_parser()handling of multiple request messages as indicated in my previous mail. Fourth patch is the ioctl compatibility fix.Great, thank you. I pushed the patches to our regression. Will go over the results tomorrow and let you know.
Looks OK. Thanks again.