Skip to contentSkip to conversation

ethtool: fix netlink bitmasks when sent as NOMASK

ethtool Jacob Keller 4 participants 12 messages Jul 27, 2020

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

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.

The dump_link_modes and bitset_get_bit functions are designed so they can print either the values or the mask. For a nomask bitmap, this doesn't make sense. There is no mask.

Modify dump_link_modes to check ETHTOOL_A_BITSET_NOMASK. For compact bitmaps, always check and print the ETHTOOL_A_BITSET_VALUE bits, regardless of the request to display the mask or the value. For full size bitmaps, the set of provided bits indicates the valid values, without using ETHTOOL_A_BITSET_VALUE fields. Thus, do not skip printing bits without this attribute if nomask is set. This essentially means that dump_link_modes will treat a NOMASK bitset as having a mask equivalent to all of its set bits.

For bitset_get_bit, also check for ETHTOOL_A_BITSET_NOMASK. For compact bitmaps, always use ETHTOOL_A_BITSET_BIT_VALUE as in dump_link_modes. For full bitmaps, if nomask is set, then always return true of the bit is in the set, rather than only if it provides an ETHTOOL_A_BITSET_BIT_VALUE. This will then correctly report the set bits.

This fixes display of link partner advertised fields when using the netlink API.

---
 netlink/bitset.c   | 9 ++++++---
 netlink/settings.c | 8 +++++---
 2 files changed, 11 insertions(+), 6 deletions(-)
netlink/bitset.c +6 -3
index 130bcdb5b52c..ba5d3ea77ff7 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,8 +58,10 @@ bool bitset_get_bit(const struct nlattr *bitset, bool mask, unsigned int idx,
 	if (ret < 0)
 		goto err;
 
-	bits = mask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
-		      bitset_tb[ETHTOOL_A_BITSET_VALUE];
+	nomask = bitset_tb[ETHTOOL_A_BITSET_NOMASK];
+
+	bits = mask && !nomask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
+		                 bitset_tb[ETHTOOL_A_BITSET_VALUE];
 	if (bits) {
 		const uint32_t *bitmap =
 			(const uint32_t *)mnl_attr_get_payload(bits);
@@ -87,7 +90,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 +5 -3
index 35ba2f5dd6d5..29557653336e 100644
@@ -280,9 +280,11 @@ 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);
+	nomask = bitset_tb[ETHTOOL_A_BITSET_NOMASK];
 	bits = bitset_tb[ETHTOOL_A_BITSET_BITS];
 	if (ret < 0)
 		goto err_nonl;
@@ -297,8 +299,8 @@ int dump_link_modes(struct nl_context *nlctx, const struct nlattr *bitset,
 			goto err_nonl;
 		lm_strings = global_stringset(ETH_SS_LINK_MODES,
 					      nlctx->ethnl2_socket);
-		bits = mask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
-			      bitset_tb[ETHTOOL_A_BITSET_VALUE];
+		bits = mask && !nomask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
+			                 bitset_tb[ETHTOOL_A_BITSET_VALUE];
 		ret = -EFAULT;
 		if (!bits || !bitset_tb[ETHTOOL_A_BITSET_SIZE])
 			goto err_nonl;
@@ -354,7 +356,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]);
Signature · 1 line
2.26.2
#1 lore raw
Quoted 26 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.

The dump_link_modes and bitset_get_bit functions are designed so they can print either the values or the mask. For a nomask bitmap, this doesn't make sense. There is no mask.

Modify dump_link_modes to check ETHTOOL_A_BITSET_NOMASK. For compact bitmaps, always check and print the ETHTOOL_A_BITSET_VALUE bits, regardless of the request to display the mask or the value. For full size bitmaps, the set of provided bits indicates the valid values, without using ETHTOOL_A_BITSET_VALUE fields. Thus, do not skip printing bits without this attribute if nomask is set. This essentially means that dump_link_modes will treat a NOMASK bitset as having a mask equivalent to all of its set bits.

For bitset_get_bit, also check for ETHTOOL_A_BITSET_NOMASK. For compact bitmaps, always use ETHTOOL_A_BITSET_BIT_VALUE as in dump_link_modes. For full bitmaps, if nomask is set, then always return true of the bit is in the set, rather than only if it provides an ETHTOOL_A_BITSET_BIT_VALUE. This will then correctly report the set bits.

Quoted 5 lines

This fixes display of link partner advertised fields when using the netlink API.

Andrew, could you kindly test this in your setup? I believe it will fully resolve the issue.

Michal, I think this is complete based on the docs in ethtool-netlink.rst and some tests. Any further insight?

Thanks,
Jake

Quoted 76 lines
---
 netlink/bitset.c   | 9 ++++++---
 netlink/settings.c | 8 +++++---
 2 files changed, 11 insertions(+), 6 deletions(-)
netlink/bitset.c +6 -3
index 130bcdb5b52c..ba5d3ea77ff7 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,8 +58,10 @@ bool bitset_get_bit(const struct nlattr *bitset, bool mask, unsigned int idx,
 	if (ret < 0)
 		goto err;
 
-	bits = mask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
-		      bitset_tb[ETHTOOL_A_BITSET_VALUE];
+	nomask = bitset_tb[ETHTOOL_A_BITSET_NOMASK];
+
+	bits = mask && !nomask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
+		                 bitset_tb[ETHTOOL_A_BITSET_VALUE];
 	if (bits) {
 		const uint32_t *bitmap =
 			(const uint32_t *)mnl_attr_get_payload(bits);
@@ -87,7 +90,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 +5 -3
index 35ba2f5dd6d5..29557653336e 100644
@@ -280,9 +280,11 @@ 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);
+	nomask = bitset_tb[ETHTOOL_A_BITSET_NOMASK];
 	bits = bitset_tb[ETHTOOL_A_BITSET_BITS];
 	if (ret < 0)
 		goto err_nonl;
@@ -297,8 +299,8 @@ int dump_link_modes(struct nl_context *nlctx, const struct nlattr *bitset,
 			goto err_nonl;
 		lm_strings = global_stringset(ETH_SS_LINK_MODES,
 					      nlctx->ethnl2_socket);
-		bits = mask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
-			      bitset_tb[ETHTOOL_A_BITSET_VALUE];
+		bits = mask && !nomask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
+			                 bitset_tb[ETHTOOL_A_BITSET_VALUE];
 		ret = -EFAULT;
 		if (!bits || !bitset_tb[ETHTOOL_A_BITSET_SIZE])
 			goto err_nonl;
@@ -354,7 +356,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]);
#2 lore raw
Quoted 118 lines · Jacob Keller
Quoted 26 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.

The dump_link_modes and bitset_get_bit functions are designed so they can print either the values or the mask. For a nomask bitmap, this doesn't make sense. There is no mask.

Modify dump_link_modes to check ETHTOOL_A_BITSET_NOMASK. For compact bitmaps, always check and print the ETHTOOL_A_BITSET_VALUE bits, regardless of the request to display the mask or the value. For full size bitmaps, the set of provided bits indicates the valid values, without using ETHTOOL_A_BITSET_VALUE fields. Thus, do not skip printing bits without this attribute if nomask is set. This essentially means that dump_link_modes will treat a NOMASK bitset as having a mask equivalent to all of its set bits.

For bitset_get_bit, also check for ETHTOOL_A_BITSET_NOMASK. For compact bitmaps, always use ETHTOOL_A_BITSET_BIT_VALUE as in dump_link_modes. For full bitmaps, if nomask is set, then always return true of the bit is in the set, rather than only if it provides an ETHTOOL_A_BITSET_BIT_VALUE. This will then correctly report the set bits.

Quoted 5 lines

This fixes display of link partner advertised fields when using the netlink API.

Andrew, could you kindly test this in your setup? I believe it will fully resolve the issue.

Michal, I think this is complete based on the docs in ethtool-netlink.rst and some tests. Any further insight?

Thanks,
Jake

Quoted 74 lines
---
 netlink/bitset.c   | 9 ++++++---
 netlink/settings.c | 8 +++++---
 2 files changed, 11 insertions(+), 6 deletions(-)
netlink/bitset.c +6 -3
index 130bcdb5b52c..ba5d3ea77ff7 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,8 +58,10 @@ bool bitset_get_bit(const struct nlattr *bitset, bool mask, unsigned int idx,
 	if (ret < 0)
 		goto err;
 
-	bits = mask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
-		      bitset_tb[ETHTOOL_A_BITSET_VALUE];
+	nomask = bitset_tb[ETHTOOL_A_BITSET_NOMASK];
+
+	bits = mask && !nomask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
+		                 bitset_tb[ETHTOOL_A_BITSET_VALUE];
 	if (bits) {
 		const uint32_t *bitmap =
 			(const uint32_t *)mnl_attr_get_payload(bits);
@@ -87,7 +90,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 +5 -3
index 35ba2f5dd6d5..29557653336e 100644
@@ -280,9 +280,11 @@ 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);
+	nomask = bitset_tb[ETHTOOL_A_BITSET_NOMASK];
 	bits = bitset_tb[ETHTOOL_A_BITSET_BITS];
 	if (ret < 0)
 		goto err_nonl;
@@ -297,8 +299,8 @@ int dump_link_modes(struct nl_context *nlctx, const struct nlattr *bitset,
 			goto err_nonl;
 		lm_strings = global_stringset(ETH_SS_LINK_MODES,
 					      nlctx->ethnl2_socket);
-		bits = mask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
-			      bitset_tb[ETHTOOL_A_BITSET_VALUE];
+		bits = mask && !nomask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
+			                 bitset_tb[ETHTOOL_A_BITSET_VALUE];
 		ret = -EFAULT;
 		if (!bits || !bitset_tb[ETHTOOL_A_BITSET_SIZE])
 			goto err_nonl;
@@ -354,7 +356,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;
 
Quoted 2 lines

netlink/settings.c int dump_link_modes(struct nl_context *nlctx, const struct nlattr *bitset, · line 362

Quoted 2 lines
netlink/settings.c +0 -0
 		idx = mnl_attr_get_u32(tb[ETHTOOL_A_BITSET_BIT_INDEX]);

I can confirm that your patch works. As you can see below:

Link partner advertised pause frame use: Symmetric Receive-only Link partner advertised auto-negotiation: Yes
Link partner advertised FEC modes: No

You can tag this patch Tested-by me. Thanks!

Regards,
Jamie Gloudon

Tested-by
#3 lore raw
In reply to Jacob Keller
Quoted 26 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.

The dump_link_modes and bitset_get_bit functions are designed so they can print either the values or the mask. For a nomask bitmap, this doesn't make sense. There is no mask.

Modify dump_link_modes to check ETHTOOL_A_BITSET_NOMASK. For compact bitmaps, always check and print the ETHTOOL_A_BITSET_VALUE bits, regardless of the request to display the mask or the value. For full size bitmaps, the set of provided bits indicates the valid values, without using ETHTOOL_A_BITSET_VALUE fields. Thus, do not skip printing bits without this attribute if nomask is set. This essentially means that dump_link_modes will treat a NOMASK bitset as having a mask equivalent to all of its set bits.

For bitset_get_bit, also check for ETHTOOL_A_BITSET_NOMASK. For compact bitmaps, always use ETHTOOL_A_BITSET_BIT_VALUE as in dump_link_modes. For full bitmaps, if nomask is set, then always return true of the bit is in the set, rather than only if it provides an ETHTOOL_A_BITSET_BIT_VALUE. This will then correctly report the set bits.

Quoted 2 lines

This fixes display of link partner advertised fields when using the netlink API.

Hi Jacob

This is close

Netlink
Link partner advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full 1000baseT/Full
Link partner advertised pause frame use: No
Link partner advertised auto-negotiation: Yes
Link partner advertised FEC modes: No

IOCTL
Link partner advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full 1000baseT/Full
Link partner advertised pause frame use: No
Link partner advertised auto-negotiation: Yes
Link partner advertised FEC modes: Not reported

So just the FEC modes differ.

However, i don't think this was part of the original issue, so:

It would be nice to get the FEC modes fixed.

Andrew

#8 lore raw
In reply to Jacob Keller
Quoted 51 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.

The dump_link_modes and bitset_get_bit functions are designed so they can print either the values or the mask. For a nomask bitmap, this doesn't make sense. There is no mask.

Modify dump_link_modes to check ETHTOOL_A_BITSET_NOMASK. For compact bitmaps, always check and print the ETHTOOL_A_BITSET_VALUE bits, regardless of the request to display the mask or the value. For full size bitmaps, the set of provided bits indicates the valid values, without using ETHTOOL_A_BITSET_VALUE fields. Thus, do not skip printing bits without this attribute if nomask is set. This essentially means that dump_link_modes will treat a NOMASK bitset as having a mask equivalent to all of its set bits.

For bitset_get_bit, also check for ETHTOOL_A_BITSET_NOMASK. For compact bitmaps, always use ETHTOOL_A_BITSET_BIT_VALUE as in dump_link_modes. For full bitmaps, if nomask is set, then always return true of the bit is in the set, rather than only if it provides an ETHTOOL_A_BITSET_BIT_VALUE. This will then correctly report the set bits.

This fixes display of link partner advertised fields when using the netlink API.

---
 netlink/bitset.c   | 9 ++++++---
 netlink/settings.c | 8 +++++---
 2 files changed, 11 insertions(+), 6 deletions(-)
netlink/bitset.c +1 -0
index 130bcdb5b52c..ba5d3ea77ff7 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,8 +58,10 @@ bool bitset_get_bit(const struct nlattr *bitset, bool mask, unsigned int idx,
 	if (ret < 0)
 		goto err;

netlink/bitset.c bool bitset_get_bit(const struct nlattr *bitset, bool mask, unsigned int idx, · line 67

Quoted 10 lines
netlink/bitset.c +4 -2
 
-	bits = mask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
-		      bitset_tb[ETHTOOL_A_BITSET_VALUE];
+	nomask = bitset_tb[ETHTOOL_A_BITSET_NOMASK];
+
+	bits = mask && !nomask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
+		                 bitset_tb[ETHTOOL_A_BITSET_VALUE];
 	if (bits) {
 		const uint32_t *bitmap =
 			(const uint32_t *)mnl_attr_get_payload(bits);

I don't like this part: (mask && nomask) is a situation which should never happen as it would mean we are trying to get mask value from a bitmap which does not any. In other words, if we ever see such combination, it is a result of a bug either on ethtool side or on kernel side.

Rather than silently returning something else than asked, we should IMHO report an error. Which is easy in dump_link_modes() but it would require rewriting bitset_get_bit().

Michal

Quoted 48 lines
netlink/bitset.c +1 -1
@@ -87,7 +90,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 +5 -3
index 35ba2f5dd6d5..29557653336e 100644
@@ -280,9 +280,11 @@ 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);
+	nomask = bitset_tb[ETHTOOL_A_BITSET_NOMASK];
 	bits = bitset_tb[ETHTOOL_A_BITSET_BITS];
 	if (ret < 0)
 		goto err_nonl;
@@ -297,8 +299,8 @@ int dump_link_modes(struct nl_context *nlctx, const struct nlattr *bitset,
 			goto err_nonl;
 		lm_strings = global_stringset(ETH_SS_LINK_MODES,
 					      nlctx->ethnl2_socket);
-		bits = mask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
-			      bitset_tb[ETHTOOL_A_BITSET_VALUE];
+		bits = mask && !nomask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
+			                 bitset_tb[ETHTOOL_A_BITSET_VALUE];
 		ret = -EFAULT;
 		if (!bits || !bitset_tb[ETHTOOL_A_BITSET_SIZE])
 			goto err_nonl;
@@ -354,7 +356,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]);
Signature · 1 line
2.26.2
#4 lore raw
In reply to Andrew Lunn
Quoted 52 lines · Andrew Lunn
Quoted 26 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.

The dump_link_modes and bitset_get_bit functions are designed so they can print either the values or the mask. For a nomask bitmap, this doesn't make sense. There is no mask.

Modify dump_link_modes to check ETHTOOL_A_BITSET_NOMASK. For compact bitmaps, always check and print the ETHTOOL_A_BITSET_VALUE bits, regardless of the request to display the mask or the value. For full size bitmaps, the set of provided bits indicates the valid values, without using ETHTOOL_A_BITSET_VALUE fields. Thus, do not skip printing bits without this attribute if nomask is set. This essentially means that dump_link_modes will treat a NOMASK bitset as having a mask equivalent to all of its set bits.

For bitset_get_bit, also check for ETHTOOL_A_BITSET_NOMASK. For compact bitmaps, always use ETHTOOL_A_BITSET_BIT_VALUE as in dump_link_modes. For full bitmaps, if nomask is set, then always return true of the bit is in the set, rather than only if it provides an ETHTOOL_A_BITSET_BIT_VALUE. This will then correctly report the set bits.

Quoted 2 lines

This fixes display of link partner advertised fields when using the netlink API.

Hi Jacob

This is close

Netlink
Link partner advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full 1000baseT/Full
Link partner advertised pause frame use: No
Link partner advertised auto-negotiation: Yes
Link partner advertised FEC modes: No

IOCTL
Link partner advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full 1000baseT/Full
Link partner advertised pause frame use: No
Link partner advertised auto-negotiation: Yes
Link partner advertised FEC modes: Not reported

So just the FEC modes differ.

Quoted 8 lines

However, i don't think this was part of the original issue, so:

It would be nice to get the FEC modes fixed.

Andrew

Yea, it looks like FEC modes is because FEC actually send a "None" flag, as opposed to using an empty set as "none". I can follow-up this fix with a change I think will resolve this as well.

Thanks,
Jake

#5 lore raw
In reply to Andrew Lunn
Quoted 50 lines · Andrew Lunn
Quoted 26 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.

The dump_link_modes and bitset_get_bit functions are designed so they can print either the values or the mask. For a nomask bitmap, this doesn't make sense. There is no mask.

Modify dump_link_modes to check ETHTOOL_A_BITSET_NOMASK. For compact bitmaps, always check and print the ETHTOOL_A_BITSET_VALUE bits, regardless of the request to display the mask or the value. For full size bitmaps, the set of provided bits indicates the valid values, without using ETHTOOL_A_BITSET_VALUE fields. Thus, do not skip printing bits without this attribute if nomask is set. This essentially means that dump_link_modes will treat a NOMASK bitset as having a mask equivalent to all of its set bits.

For bitset_get_bit, also check for ETHTOOL_A_BITSET_NOMASK. For compact bitmaps, always use ETHTOOL_A_BITSET_BIT_VALUE as in dump_link_modes. For full bitmaps, if nomask is set, then always return true of the bit is in the set, rather than only if it provides an ETHTOOL_A_BITSET_BIT_VALUE. This will then correctly report the set bits.

Quoted 2 lines

This fixes display of link partner advertised fields when using the netlink API.

Hi Jacob

This is close

Netlink
Link partner advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full 1000baseT/Full
Link partner advertised pause frame use: No
Link partner advertised auto-negotiation: Yes
Link partner advertised FEC modes: No

IOCTL
Link partner advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full 1000baseT/Full
Link partner advertised pause frame use: No
Link partner advertised auto-negotiation: Yes
Link partner advertised FEC modes: Not reported

Quoted 1 line

So just the FEC modes differ.

This is a different issue, the last call to dump_link_modes() in dump_peer_modes() should be

ret = dump_link_modes(nlctx, attr, false, LM_CLASS_FEC,

(third parameter needs to be false, not true).

Michal

Quoted 8 lines

However, i don't think this was part of the original issue, so:

It would be nice to get the FEC modes fixed.

Andrew

#6 lore raw
Quoted 64 lines

-----Original Message-----
From: Michal Kubecek <[email protected]>
Sent: Monday, July 27, 2020 3:27 PM
To: Andrew Lunn <[email protected]>
Cc: Keller, Jacob E <[email protected]>; [email protected]; Jamie Gloudon <[email protected]>
Subject: Re: [ethtool] ethtool: fix netlink bitmasks when sent as NOMASK

Quoted 7 lines · Andrew Lunn
Quoted 6 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.

Quoted 5 lines
Quoted 5 lines

The dump_link_modes and bitset_get_bit functions are designed so they can print either the values or the mask. For a nomask bitmap, this doesn't make sense. There is no mask.

Modify dump_link_modes to check ETHTOOL_A_BITSET_NOMASK. For

compact

Quoted 38 lines
Quoted 15 lines

bitmaps, always check and print the ETHTOOL_A_BITSET_VALUE bits, regardless of the request to display the mask or the value. For full size bitmaps, the set of provided bits indicates the valid values, without using ETHTOOL_A_BITSET_VALUE fields. Thus, do not skip printing bits without this attribute if nomask is set. This essentially means that dump_link_modes will treat a NOMASK bitset as having a mask equivalent to all of its set bits.

For bitset_get_bit, also check for ETHTOOL_A_BITSET_NOMASK. For compact bitmaps, always use ETHTOOL_A_BITSET_BIT_VALUE as in dump_link_modes. For full bitmaps, if nomask is set, then always return true of the bit is in the set, rather than only if it provides an ETHTOOL_A_BITSET_BIT_VALUE. This will then correctly report the set bits.

Quoted 2 lines

This fixes display of link partner advertised fields when using the netlink API.

Hi Jacob

This is close

Netlink
Link partner advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full 1000baseT/Full
Link partner advertised pause frame use: No
Link partner advertised auto-negotiation: Yes
Link partner advertised FEC modes: No

IOCTL
Link partner advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full 1000baseT/Full
Link partner advertised pause frame use: No
Link partner advertised auto-negotiation: Yes
Link partner advertised FEC modes: Not reported

Quoted 1 line

So just the FEC modes differ.

Quoted 9 lines

This is a different issue, the last call to dump_link_modes() in dump_peer_modes() should be

ret = dump_link_modes(nlctx, attr, false, LM_CLASS_FEC,

(third parameter needs to be false, not true).

Michal

That's part of it, yea, but also it should use the string "Not Reported" instead of "No" I think?

Quoted 8 lines
Quoted 8 lines

However, i don't think this was part of the original issue, so:

It would be nice to get the FEC modes fixed.

Andrew

#9 lore raw
In reply to Michal Kubecek
Quoted 69 lines · Michal Kubecek
Quoted 51 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.

The dump_link_modes and bitset_get_bit functions are designed so they can print either the values or the mask. For a nomask bitmap, this doesn't make sense. There is no mask.

Modify dump_link_modes to check ETHTOOL_A_BITSET_NOMASK. For compact bitmaps, always check and print the ETHTOOL_A_BITSET_VALUE bits, regardless of the request to display the mask or the value. For full size bitmaps, the set of provided bits indicates the valid values, without using ETHTOOL_A_BITSET_VALUE fields. Thus, do not skip printing bits without this attribute if nomask is set. This essentially means that dump_link_modes will treat a NOMASK bitset as having a mask equivalent to all of its set bits.

For bitset_get_bit, also check for ETHTOOL_A_BITSET_NOMASK. For compact bitmaps, always use ETHTOOL_A_BITSET_BIT_VALUE as in dump_link_modes. For full bitmaps, if nomask is set, then always return true of the bit is in the set, rather than only if it provides an ETHTOOL_A_BITSET_BIT_VALUE. This will then correctly report the set bits.

This fixes display of link partner advertised fields when using the netlink API.

---
 netlink/bitset.c   | 9 ++++++---
 netlink/settings.c | 8 +++++---
 2 files changed, 11 insertions(+), 6 deletions(-)
netlink/bitset.c +1 -0
index 130bcdb5b52c..ba5d3ea77ff7 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,8 +58,10 @@ bool bitset_get_bit(const struct nlattr *bitset, bool mask, unsigned int idx,
 	if (ret < 0)
 		goto err;

netlink/bitset.c bool bitset_get_bit(const struct nlattr *bitset, bool mask, unsigned int idx, · line 67

Quoted 10 lines
netlink/bitset.c +4 -2
 
-	bits = mask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
-		      bitset_tb[ETHTOOL_A_BITSET_VALUE];
+	nomask = bitset_tb[ETHTOOL_A_BITSET_NOMASK];
+
+	bits = mask && !nomask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
+		                 bitset_tb[ETHTOOL_A_BITSET_VALUE];
 	if (bits) {
 		const uint32_t *bitmap =
 			(const uint32_t *)mnl_attr_get_payload(bits);

I don't like this part: (mask && nomask) is a situation which should never happen as it would mean we are trying to get mask value from a bitmap which does not any. In other words, if we ever see such combination, it is a result of a bug either on ethtool side or on kernel side.

Quoted 5 lines

Rather than silently returning something else than asked, we should IMHO report an error. Which is easy in dump_link_modes() but it would require rewriting bitset_get_bit().

Michal

The "mask" boolean is an indication that you want to print the mask for a bitmap, rather than its value. I think treating a bitmap without a predefined mask to have its mask be equivalent to its values is reasonable. However, I could see the argument for not wanting this since it is effectively a bug somewhere.

For dump_link_modes, it is trivial. If nomask is set, and mask is requested, bail out of the function. It looks like we can also report an error for the bitset_get_bit too. I'll take a look closer.

Thanks,
Jake

#7 lore raw
In reply to Keller, Jacob E
Quoted 36 lines · Jacob E
Quoted 36 lines

-----Original Message-----
From: Michal Kubecek <[email protected]>
Sent: Monday, July 27, 2020 3:27 PM
To: Andrew Lunn <[email protected]>
Cc: Keller, Jacob E <[email protected]>; [email protected]; Jamie Gloudon <[email protected]>
Subject: Re: [ethtool] ethtool: fix netlink bitmasks when sent as NOMASK

Quoted 20 lines · Andrew Lunn

Hi Jacob

This is close

Netlink
Link partner advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full 1000baseT/Full
Link partner advertised pause frame use: No
Link partner advertised auto-negotiation: Yes
Link partner advertised FEC modes: No

IOCTL
Link partner advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full 1000baseT/Full
Link partner advertised pause frame use: No
Link partner advertised auto-negotiation: Yes
Link partner advertised FEC modes: Not reported

Quoted 1 line

So just the FEC modes differ.

This is a different issue, the last call to dump_link_modes() in dump_peer_modes() should be

ret = dump_link_modes(nlctx, attr, false, LM_CLASS_FEC,

Quoted 7 lines
Quoted 4 lines

(third parameter needs to be false, not true).

Michal

That's part of it, yea, but also it should use the string "Not Reported" instead of "No" I think?

Right, both should be fixed.

Michal

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

However, i don't think this was part of the original issue, so:

It would be nice to get the FEC modes fixed.

Andrew

#10 lore raw
In reply to Jacob Keller
Quoted 74 lines · Jacob Keller
Quoted 73 lines · Michal Kubecek
Quoted 51 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.

The dump_link_modes and bitset_get_bit functions are designed so they can print either the values or the mask. For a nomask bitmap, this doesn't make sense. There is no mask.

Modify dump_link_modes to check ETHTOOL_A_BITSET_NOMASK. For compact bitmaps, always check and print the ETHTOOL_A_BITSET_VALUE bits, regardless of the request to display the mask or the value. For full size bitmaps, the set of provided bits indicates the valid values, without using ETHTOOL_A_BITSET_VALUE fields. Thus, do not skip printing bits without this attribute if nomask is set. This essentially means that dump_link_modes will treat a NOMASK bitset as having a mask equivalent to all of its set bits.

For bitset_get_bit, also check for ETHTOOL_A_BITSET_NOMASK. For compact bitmaps, always use ETHTOOL_A_BITSET_BIT_VALUE as in dump_link_modes. For full bitmaps, if nomask is set, then always return true of the bit is in the set, rather than only if it provides an ETHTOOL_A_BITSET_BIT_VALUE. This will then correctly report the set bits.

This fixes display of link partner advertised fields when using the netlink API.

---
 netlink/bitset.c   | 9 ++++++---
 netlink/settings.c | 8 +++++---
 2 files changed, 11 insertions(+), 6 deletions(-)
netlink/bitset.c +1 -0
index 130bcdb5b52c..ba5d3ea77ff7 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,8 +58,10 @@ bool bitset_get_bit(const struct nlattr *bitset, bool mask, unsigned int idx,
 	if (ret < 0)
 		goto err;

netlink/bitset.c bool bitset_get_bit(const struct nlattr *bitset, bool mask, unsigned int idx, · line 67

Quoted 10 lines
netlink/bitset.c +4 -2
 
-	bits = mask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
-		      bitset_tb[ETHTOOL_A_BITSET_VALUE];
+	nomask = bitset_tb[ETHTOOL_A_BITSET_NOMASK];
+
+	bits = mask && !nomask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
+		                 bitset_tb[ETHTOOL_A_BITSET_VALUE];
 	if (bits) {
 		const uint32_t *bitmap =
 			(const uint32_t *)mnl_attr_get_payload(bits);

I don't like this part: (mask && nomask) is a situation which should never happen as it would mean we are trying to get mask value from a bitmap which does not any. In other words, if we ever see such combination, it is a result of a bug either on ethtool side or on kernel side.

Rather than silently returning something else than asked, we should IMHO report an error. Which is easy in dump_link_modes() but it would require rewriting bitset_get_bit().

Quoted 6 lines
Quoted 1 line

Michal

The "mask" boolean is an indication that you want to print the mask for a bitmap, rather than its value. I think treating a bitmap without a predefined mask to have its mask be equivalent to its values is reasonable.

It depends on the context. In requests, value=0x1,mask=0x1 means "set bit 0 and leave the rest untouched" while nomask bitmap with value=0x1 would mean "set bit 0 and clear the rest".

For kernel replies, it should be documented which variant is expected.

Quoted 2 lines

However, I could see the argument for not wanting this since
it is effectively a bug somewhere.

We actually had this kind of bug in FEC modes handling as we just found.

Michal

Quoted 6 lines

For dump_link_modes, it is trivial. If nomask is set, and mask is requested, bail out of the function. It looks like we can also report an error for the bitset_get_bit too. I'll take a look closer.

Thanks,
Jake

#11 lore raw
Quoted 82 lines · Michal Kubecek
Quoted 74 lines · Jacob Keller
Quoted 73 lines · Michal Kubecek
Quoted 51 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.

The dump_link_modes and bitset_get_bit functions are designed so they can print either the values or the mask. For a nomask bitmap, this doesn't make sense. There is no mask.

Modify dump_link_modes to check ETHTOOL_A_BITSET_NOMASK. For compact bitmaps, always check and print the ETHTOOL_A_BITSET_VALUE bits, regardless of the request to display the mask or the value. For full size bitmaps, the set of provided bits indicates the valid values, without using ETHTOOL_A_BITSET_VALUE fields. Thus, do not skip printing bits without this attribute if nomask is set. This essentially means that dump_link_modes will treat a NOMASK bitset as having a mask equivalent to all of its set bits.

For bitset_get_bit, also check for ETHTOOL_A_BITSET_NOMASK. For compact bitmaps, always use ETHTOOL_A_BITSET_BIT_VALUE as in dump_link_modes. For full bitmaps, if nomask is set, then always return true of the bit is in the set, rather than only if it provides an ETHTOOL_A_BITSET_BIT_VALUE. This will then correctly report the set bits.

This fixes display of link partner advertised fields when using the netlink API.

---
 netlink/bitset.c   | 9 ++++++---
 netlink/settings.c | 8 +++++---
 2 files changed, 11 insertions(+), 6 deletions(-)
netlink/bitset.c +1 -0
index 130bcdb5b52c..ba5d3ea77ff7 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,8 +58,10 @@ bool bitset_get_bit(const struct nlattr *bitset, bool mask, unsigned int idx,
 	if (ret < 0)
 		goto err;

netlink/bitset.c bool bitset_get_bit(const struct nlattr *bitset, bool mask, unsigned int idx, · line 67

Quoted 10 lines
netlink/bitset.c +4 -2
 
-	bits = mask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
-		      bitset_tb[ETHTOOL_A_BITSET_VALUE];
+	nomask = bitset_tb[ETHTOOL_A_BITSET_NOMASK];
+
+	bits = mask && !nomask ? bitset_tb[ETHTOOL_A_BITSET_MASK] :
+		                 bitset_tb[ETHTOOL_A_BITSET_VALUE];
 	if (bits) {
 		const uint32_t *bitmap =
 			(const uint32_t *)mnl_attr_get_payload(bits);

I don't like this part: (mask && nomask) is a situation which should never happen as it would mean we are trying to get mask value from a bitmap which does not any. In other words, if we ever see such combination, it is a result of a bug either on ethtool side or on kernel side.

Rather than silently returning something else than asked, we should IMHO report an error. Which is easy in dump_link_modes() but it would require rewriting bitset_get_bit().

Quoted 6 lines
Quoted 1 line

Michal

The "mask" boolean is an indication that you want to print the mask for a bitmap, rather than its value. I think treating a bitmap without a predefined mask to have its mask be equivalent to its values is reasonable.

Quoted 6 lines

It depends on the context. In requests, value=0x1,mask=0x1 means "set bit 0 and leave the rest untouched" while nomask bitmap with value=0x1 would mean "set bit 0 and clear the rest".

For kernel replies, it should be documented which variant is expected.

Right, I was mostly referring to the reply side of things. I've updated the patch in v2 to explicitly reject trying to print the mask of a NO MASK bitset.

Thanks,
Jake