Skip to contentSkip to conversation

netlink: improve compatibility with ioctl interface

PATCH New Michal Kubecek 1 participant 3 messages Nov 9, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover netlink: improve compatibility with ioctl interface

Restore special behavior of "ethtool -s <dev> autoneg on" if no advertised modes, speed and duplex are requested: ioctl code enables all link modes supported by the device. This is most important for network devices which report no advertised modes when autonegotiation is disabled.

First patch cleans up the parser interface; it allows nl_sset() to inspect the composed message and append an attribute to it if needed.

Ido Schimmel (1):
ethtool: Improve compatibility between netlink and ioctl interfaces

Michal Kubecek (1):
netlink: do not send messages and process replies in nl_parser()

 netlink/cable_test.c |   2 +-
 netlink/channels.c   |   2 +-
 netlink/coalesce.c   |   2 +-
 netlink/eee.c        |   2 +-
 netlink/parser.c     |  43 ++++++++++-----
 netlink/parser.h     |   3 +-
 netlink/pause.c      |   2 +-
 netlink/rings.c      |   2 +-
 netlink/settings.c   | 127 +++++++++++++++++++++++++++++++++++++++++--
 9 files changed, 158 insertions(+), 27 deletions(-)
Signature · 1 line
2.29.2
#1 lore raw

PATCH 1/2 netlink: do not send messages and process replies in nl_parser()

When called with group_style = PARSER_GROUP_MSG, nl_parser() not only parses the command line and composes the messages but also sends them to kernel and processes the replies. This is inconsistent with other modes and also impractical as it takes the control over the process from caller where it belongs.

Modify nl_parser() to pass composed messages back to caller (which is only nl_sset() at the moment) and let it send requests and process replies. This will be needed for an upcoming backward compatibility patch which will need to inspect and possibly modify one of the composed messages.

---
 netlink/cable_test.c |  2 +-
 netlink/channels.c   |  2 +-
 netlink/coalesce.c   |  2 +-
 netlink/eee.c        |  2 +-
 netlink/parser.c     | 43 ++++++++++++++++++++++++++++---------------
 netlink/parser.h     |  3 ++-
 netlink/pause.c      |  2 +-
 netlink/rings.c      |  2 +-
 netlink/settings.c   | 35 ++++++++++++++++++++++++++++++-----
 9 files changed, 66 insertions(+), 27 deletions(-)
netlink/cable_test.c +1 -1
index 8a7145324610..17139f7d297d 100644
@@ -574,7 +574,7 @@ int nl_cable_test_tdr(struct cmd_context *ctx)
 			       ctx->devname, 0))
 		return -EMSGSIZE;
 
-	ret = nl_parser(nlctx, tdr_params, NULL, PARSER_GROUP_NEST);
+	ret = nl_parser(nlctx, tdr_params, NULL, PARSER_GROUP_NEST, NULL);
 	if (ret < 0)
 		return ret;
 
netlink/channels.c +1 -1
index c6002ceeb121..894c74bcc11a 100644
@@ -126,7 +126,7 @@ int nl_schannels(struct cmd_context *ctx)
 			       ctx->devname, 0))
 		return -EMSGSIZE;
 
-	ret = nl_parser(nlctx, schannels_params, NULL, PARSER_GROUP_NONE);
+	ret = nl_parser(nlctx, schannels_params, NULL, PARSER_GROUP_NONE, NULL);
 	if (ret < 0)
 		return 1;
 
netlink/coalesce.c +1 -1
index 07a92d04b7a1..75922a91c2e7 100644
@@ -254,7 +254,7 @@ int nl_scoalesce(struct cmd_context *ctx)
 			       ctx->devname, 0))
 		return -EMSGSIZE;
 
-	ret = nl_parser(nlctx, scoalesce_params, NULL, PARSER_GROUP_NONE);
+	ret = nl_parser(nlctx, scoalesce_params, NULL, PARSER_GROUP_NONE, NULL);
 	if (ret < 0)
 		return 1;
 
netlink/eee.c +1 -1
index d3135b2094a4..04d8f0bbe3fc 100644
@@ -174,7 +174,7 @@ int nl_seee(struct cmd_context *ctx)
 			       ctx->devname, 0))
 		return -EMSGSIZE;
 
-	ret = nl_parser(nlctx, seee_params, NULL, PARSER_GROUP_NONE);
+	ret = nl_parser(nlctx, seee_params, NULL, PARSER_GROUP_NONE, NULL);
 	if (ret < 0)
 		return 1;
 
netlink/parser.c +28 -15

107 lines not on this page · raw message

netlink/parser.h +2 -1
index fd55bc768d42..28f26ccc2a1c 100644
@@ -143,6 +143,7 @@ int nl_parse_char_bitset(struct nl_context *nlctx, uint16_t type,
 
 /* main entry point called to parse the command line */
 int nl_parser(struct nl_context *nlctx, const struct param_parser *params,
-	      void *dest, enum parser_group_style group_style);
+	      void *dest, enum parser_group_style group_style,
+	      struct nl_msg_buff **msgbuffs);
 
 #endif /* ETHTOOL_NETLINK_PARSER_H__ */
netlink/pause.c +1 -1
index 9bc9a301821f..867d0da71f72 100644
@@ -293,7 +293,7 @@ int nl_spause(struct cmd_context *ctx)
 			       ctx->devname, 0))
 		return -EMSGSIZE;
 
-	ret = nl_parser(nlctx, spause_params, NULL, PARSER_GROUP_NONE);
+	ret = nl_parser(nlctx, spause_params, NULL, PARSER_GROUP_NONE, NULL);
 	if (ret < 0)
 		return 1;
 
netlink/rings.c +1 -1
index 4061520212d5..b8c458fce25f 100644
@@ -126,7 +126,7 @@ int nl_sring(struct cmd_context *ctx)
 			       ctx->devname, 0))
 		return -EMSGSIZE;
 
-	ret = nl_parser(nlctx, sring_params, NULL, PARSER_GROUP_NONE);
+	ret = nl_parser(nlctx, sring_params, NULL, PARSER_GROUP_NONE, NULL);
 	if (ret < 0)
 		return 1;
 
netlink/settings.c +30 -5
index 41a2e5af1945..dc9280c114b5 100644
@@ -1110,9 +1110,16 @@ static const struct param_parser sset_params[] = {
 	{}
 };
 
+/* Maximum number of request messages sent to kernel; must be equal to the
+ * number of different .group values in sset_params[] array.
+ */
+#define SSET_MAX_MSGS 4
+
 int nl_sset(struct cmd_context *ctx)
 {
+	struct nl_msg_buff *msgbuffs[SSET_MAX_MSGS] = {};
 	struct nl_context *nlctx = ctx->nlctx;
+	unsigned int i;
 	int ret;
 
 	nlctx->cmd = "-s";
@@ -1120,11 +1127,29 @@ int nl_sset(struct cmd_context *ctx)
 	nlctx->argc = ctx->argc;
 	nlctx->devname = ctx->devname;
 
-	ret = nl_parser(nlctx, sset_params, NULL, PARSER_GROUP_MSG);
-	if (ret < 0)
-		return 1;
+	ret = nl_parser(nlctx, sset_params, NULL, PARSER_GROUP_MSG, msgbuffs);
+	if (ret < 0) {
+		ret = 1;
+		goto out_free;
+	}
+
+	for (i = 0; i < SSET_MAX_MSGS && msgbuffs[i]; i++) {
+		struct nl_socket *nlsk = nlctx->ethnl_socket;
 
-	if (ret == 0)
-		return 0;
+		ret = nlsock_sendmsg(nlsk, msgbuffs[i]);
+		if (ret < 0)
+			goto out_free;
+		ret = nlsock_process_reply(nlsk, nomsg_reply_cb, NULL);
+		if (ret < 0)
+			goto out_free;
+	}
+
+out_free:
+	for (i = 0; i < SSET_MAX_MSGS && msgbuffs[i]; i++) {
+		msgbuff_done(msgbuffs[i]);
+		free(msgbuffs[i]);
+	}
+	if (ret >= 0)
+		return ret;
 	return nlctx->exit_code ?: 75;
 }
Signature · 1 line
2.29.2
#2 lore raw

PATCH 2/2 ethtool: Improve compatibility between netlink and ioctl interfaces

In reply to Michal Kubecek

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 the "real" ones when only "autoneg on" is specified.

Before:

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: yes

After:

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

---
 netlink/settings.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 92 insertions(+)
netlink/settings.c +92 -0
index dc9280c114b5..90c28b1bc424 100644
@@ -1115,6 +1115,93 @@ static const struct param_parser sset_params[] = {
  */
 #define SSET_MAX_MSGS 4
 
+static int linkmodes_reply_advert_all_cb(const struct nlmsghdr *nlhdr,
+					 void *data)
+{
+	const struct nlattr *tb[ETHTOOL_A_LINKMODES_MAX + 1] = {};
+	DECLARE_ATTR_TB_INFO(tb);
+	struct nl_msg_buff *req_msgbuff = data;
+	const struct nlattr *ours_attr;
+	struct nlattr *req_bitset;
+	uint32_t *supported_modes;
+	unsigned int modes_count;
+	unsigned int i;
+	int ret;
+
+	ret = mnl_attr_parse(nlhdr, GENL_HDRLEN, attr_cb, &tb_info);
+	if (ret < 0)
+		return MNL_CB_ERROR;
+	ours_attr = tb[ETHTOOL_A_LINKMODES_OURS];
+	if (!ours_attr)
+		return MNL_CB_ERROR;
+	modes_count = bitset_get_count(tb[ETHTOOL_A_LINKMODES_OURS], &ret);
+	if (ret < 0)
+		return MNL_CB_ERROR;
+	supported_modes = get_compact_bitset_mask(tb[ETHTOOL_A_LINKMODES_OURS]);
+	if (!supported_modes)
+		return MNL_CB_ERROR;
+
+	/* keep only "real" link modes */
+	for (i = 0; i < modes_count; i++)
+		if (!lm_class_match(i, LM_CLASS_REAL))
+			supported_modes[i / 32] &= ~((uint32_t)1 << (i % 32));
+
+	req_bitset = ethnla_nest_start(req_msgbuff, ETHTOOL_A_LINKMODES_OURS);
+	if (!req_bitset)
+		return MNL_CB_ERROR;
+
+	if (ethnla_put_u32(req_msgbuff, ETHTOOL_A_BITSET_SIZE, modes_count) ||
+	    ethnla_put(req_msgbuff, ETHTOOL_A_BITSET_VALUE,
+		       DIV_ROUND_UP(modes_count, 32) * sizeof(uint32_t),
+		       supported_modes) ||
+	    ethnla_put(req_msgbuff, ETHTOOL_A_BITSET_MASK,
+		       DIV_ROUND_UP(modes_count, 32) * sizeof(uint32_t),
+		       supported_modes)) {
+		ethnla_nest_cancel(req_msgbuff, req_bitset);
+		return MNL_CB_ERROR;
+	}
+
+	ethnla_nest_end(req_msgbuff, req_bitset);
+	return MNL_CB_OK;
+}
+
+/* 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 the "real" ones.
+ */
+static int nl_sset_compat_linkmodes(struct nl_context *nlctx,
+				    struct nl_msg_buff *msgbuff)
+{
+	const struct nlattr *tb[ETHTOOL_A_LINKMODES_MAX + 1] = {};
+	DECLARE_ATTR_TB_INFO(tb);
+	struct nl_socket *nlsk = nlctx->ethnl_socket;
+	int ret;
+
+	ret = mnl_attr_parse(msgbuff->nlhdr, GENL_HDRLEN, attr_cb, &tb_info);
+	if (ret < 0)
+		return ret;
+	if (!tb[ETHTOOL_A_LINKMODES_AUTONEG] || tb[ETHTOOL_A_LINKMODES_OURS] ||
+	    tb[ETHTOOL_A_LINKMODES_SPEED] || tb[ETHTOOL_A_LINKMODES_DUPLEX])
+		return 0;
+	if (!mnl_attr_get_u8(tb[ETHTOOL_A_LINKMODES_AUTONEG]))
+		return 0;
+
+	/* all conditions satisfied, create ETHTOOL_A_LINKMODES_OURS */
+	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;
+	ret = nlsock_sendmsg(nlsk, NULL);
+	if (ret < 0)
+		return ret;
+	return nlsock_process_reply(nlsk, linkmodes_reply_advert_all_cb,
+				    msgbuff);
+}
+
 int nl_sset(struct cmd_context *ctx)
 {
 	struct nl_msg_buff *msgbuffs[SSET_MAX_MSGS] = {};
@@ -1136,6 +1223,11 @@ int nl_sset(struct cmd_context *ctx)
 	for (i = 0; i < SSET_MAX_MSGS && msgbuffs[i]; i++) {
 		struct nl_socket *nlsk = nlctx->ethnl_socket;
 
+		if (msgbuffs[i]->genlhdr->cmd == ETHTOOL_MSG_LINKMODES_SET) {
+			ret = nl_sset_compat_linkmodes(nlctx, msgbuffs[i]);
+			if (ret < 0)
+				goto out_free;
+		}
 		ret = nlsock_sendmsg(nlsk, msgbuffs[i]);
 		if (ret < 0)
 			goto out_free;
Signature · 1 line
2.29.2