Skip to contentSkip to conversation

ethtool: fix netlink bitmasks when sent as NOMASK

PATCH In review Jacob Keller 2 participants 4 messages Jul 27, 2020 – Jul 30, 2020

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

PATCH v2 2/2 ethtool: use "Not reported" when no FEC modes are provided

In reply to Jacob Keller

When displaying the FEC link modes advertised by the peer, we used the string "No" to indicate when nothing was provided. This does not match the IOCTL output which indicates "Not reported". It also doesn't match the local advertised FEC modes, which also used the "Not reported" string.

This is especially confusing for FEC, because the FEC bits include a "None" bit which indicates that FEC is definitely not supported. Avoid this confusion and match both the local advertised settings display and the old IOCTL output by using "Not reported" when FEC settings aren't reported.

---
 netlink/settings.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
netlink/settings.c +1 -1
index 66b0d4892cdd..726259d83702 100644
@@ -481,7 +481,7 @@ static int dump_peer_modes(struct nl_context *nlctx, const struct nlattr *attr)
 
 	ret = dump_link_modes(nlctx, attr, false, LM_CLASS_FEC,
 			      "Link partner advertised FEC modes: ",
-			      " ", "\n", "No");
+			      " ", "\n", "Not reported");
 	return ret;
 }
 
Signature · 1 line
2.26.2
#0 lore raw

PATCH v2 1/2 ethtool: fix netlink bitmasks when sent as NOMASK

The ethtool netlink API can send bitsets without an associated bitmask. These do not get displayed properly, because the dump_link_modes, and bitset_get_bit to not check whether the provided bitset is a NOMASK bitset. This results in the inability to display peer advertised link modes.

Both the dump_link_modes and bitset_git_bit functions do not check ETHTOOL_A_BITSET_NOMASK, and thus do not properly handle bitsets which do not have a provided mask.

For compact bitmaps, things work more or less ok, as long as mask was provided as "false". This is because it will always use the ETHTOOL_A_BITSET_BIT_VALUE section when mask is false. A NOMASK compact bitmap will provide this.

Unfortunately, if the bitset is not sent in the compact format, these functions do not behave correctly. When NOMASK is set, then the ETHTOOL_A_BITSET_BIT_VALUE is not provided. Instead, the application is supposed to treat it as a list of all the valid values.

Fix these functions so that they behave properly with NOMASK bitsets in the non-compact form. Additionally, make these functions report an error if requesting to operate with "mask" set on a NOMASK bitmap. This ensures that we catch issues in the case where ethtool is trying to print the mask of a bitset that has no mask. Doing so highlights a small bug in the FEC settings where we accidentally set mask to true. Fix this also.

---
 netlink/bitset.c   | 12 +++++++++++-
 netlink/settings.c | 16 +++++++++++++---
 2 files changed, 24 insertions(+), 4 deletions(-)
netlink/bitset.c +11 -1
index 130bcdb5b52c..10ce8e9def9a 100644
@@ -50,6 +50,7 @@ bool bitset_get_bit(const struct nlattr *bitset, bool mask, unsigned int idx,
 	DECLARE_ATTR_TB_INFO(bitset_tb);
 	const struct nlattr *bits;
 	const struct nlattr *bit;
+	bool nomask;
 	int ret;
 
 	*retptr = 0;
@@ -57,6 +58,15 @@ bool bitset_get_bit(const struct nlattr *bitset, bool mask, unsigned int idx,
 	if (ret < 0)
 		goto err;
 
+	nomask = bitset_tb[ETHTOOL_A_BITSET_NOMASK];
+	if (mask && nomask) {
+		/* Trying to determine if a bit is set in the mask of a "no
+		 * mask" bitset doesn't make sense.
+		 */
+		ret = -EFAULT;
+		goto err;
+	}
+
 	bits = mask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
 		      bitset_tb[ETHTOOL_A_BITSET_VALUE];
 	if (bits) {
@@ -87,7 +97,7 @@ bool bitset_get_bit(const struct nlattr *bitset, bool mask, unsigned int idx,
 
 		my_idx = mnl_attr_get_u32(tb[ETHTOOL_A_BITSET_BIT_INDEX]);
 		if (my_idx == idx)
-			return mask || tb[ETHTOOL_A_BITSET_BIT_VALUE];
+			return mask || nomask || tb[ETHTOOL_A_BITSET_BIT_VALUE];
 	}
 
 	return false;
netlink/settings.c +13 -3
index 35ba2f5dd6d5..66b0d4892cdd 100644
@@ -280,12 +280,22 @@ int dump_link_modes(struct nl_context *nlctx, const struct nlattr *bitset,
 	const struct nlattr *bit;
 	bool first = true;
 	int prev = -2;
+	bool nomask;
 	int ret;
 
 	ret = mnl_attr_parse_nested(bitset, attr_cb, &bitset_tb_info);
-	bits = bitset_tb[ETHTOOL_A_BITSET_BITS];
 	if (ret < 0)
 		goto err_nonl;
+
+	nomask = bitset_tb[ETHTOOL_A_BITSET_NOMASK];
+	/* Trying to print the mask of a "no mask" bitset doesn't make sense */
+	if (mask && nomask) {
+		ret = -EFAULT;
+		goto err_nonl;
+	}
+
+	bits = bitset_tb[ETHTOOL_A_BITSET_BITS];
+
 	if (!bits) {
 		const struct stringset *lm_strings;
 		unsigned int count;
@@ -354,7 +364,7 @@ int dump_link_modes(struct nl_context *nlctx, const struct nlattr *bitset,
 		if (!tb[ETHTOOL_A_BITSET_BIT_INDEX] ||
 		    !tb[ETHTOOL_A_BITSET_BIT_NAME])
 			goto err;
-		if (!mask && !tb[ETHTOOL_A_BITSET_BIT_VALUE])
+		if (!mask && !nomask && !tb[ETHTOOL_A_BITSET_BIT_VALUE])
 			continue;
 
 		idx = mnl_attr_get_u32(tb[ETHTOOL_A_BITSET_BIT_INDEX]);
@@ -469,7 +479,7 @@ static int dump_peer_modes(struct nl_context *nlctx, const struct nlattr *attr)
 	printf("\tLink partner advertised auto-negotiation: %s\n",
 	       autoneg ? "Yes" : "No");
 
-	ret = dump_link_modes(nlctx, attr, true, LM_CLASS_FEC,
+	ret = dump_link_modes(nlctx, attr, false, LM_CLASS_FEC,
 			      "Link partner advertised FEC modes: ",
 			      " ", "\n", "No");
 	return ret;
Signature · 1 line
2.26.2
#3 lore raw
Quoted 28 lines · Jacob Keller

The ethtool netlink API can send bitsets without an associated bitmask. These do not get displayed properly, because the dump_link_modes, and bitset_get_bit to not check whether the provided bitset is a NOMASK bitset. This results in the inability to display peer advertised link modes.

Both the dump_link_modes and bitset_git_bit functions do not check ETHTOOL_A_BITSET_NOMASK, and thus do not properly handle bitsets which do not have a provided mask.

For compact bitmaps, things work more or less ok, as long as mask was provided as "false". This is because it will always use the ETHTOOL_A_BITSET_BIT_VALUE section when mask is false. A NOMASK compact bitmap will provide this.

Unfortunately, if the bitset is not sent in the compact format, these functions do not behave correctly. When NOMASK is set, then the ETHTOOL_A_BITSET_BIT_VALUE is not provided. Instead, the application is supposed to treat it as a list of all the valid values.

Fix these functions so that they behave properly with NOMASK bitsets in the non-compact form. Additionally, make these functions report an error if requesting to operate with "mask" set on a NOMASK bitmap. This ensures that we catch issues in the case where ethtool is trying to print the mask of a bitset that has no mask. Doing so highlights a small bug in the FEC settings where we accidentally set mask to true. Fix this also.

Quoted 2 lines

Applied with

Fixes 490503bdbd67 ("netlink: add bitset helpers")
Fixes 10cc3ea337d1 ("netlink: partial netlink handler for gset (no option)")

Michal

#2 lore raw

Re: [ethtool v2 2/2] ethtool: use "Not reported" when no FEC modes are provided

In reply to Jacob Keller
Quoted 6 lines · Jacob Keller

When displaying the FEC link modes advertised by the peer, we used the string "No" to indicate when nothing was provided. This does not match the IOCTL output which indicates "Not reported". It also doesn't match the local advertised FEC modes, which also used the "Not reported" string.

Quoted 8 lines

This is especially confusing for FEC, because the FEC bits include a "None" bit which indicates that FEC is definitely not supported. Avoid this confusion and match both the local advertised settings display and the old IOCTL output by using "Not reported" when FEC settings aren't reported.

Applied with

Fixes 10cc3ea337d1 ("netlink: partial netlink handler for gset (no option)")

Michal

Quoted 20 lines
---
 netlink/settings.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
netlink/settings.c +1 -1
index 66b0d4892cdd..726259d83702 100644
@@ -481,7 +481,7 @@ static int dump_peer_modes(struct nl_context *nlctx, const struct nlattr *attr)
 
 	ret = dump_link_modes(nlctx, attr, false, LM_CLASS_FEC,
 			      "Link partner advertised FEC modes: ",
-			      " ", "\n", "No");
+			      " ", "\n", "Not reported");
 	return ret;
 }
 
Signature · 1 line
2.26.2