bonding: comparing a u8 with -1 is always false
Subsystems (5)
PATCH 1/1 bonding: comparing a u8 with -1 is always false
slave->duplex is a u8 type so the in bond_info_show_slave() when we check "if (slave->duplex = -1)", it's always false.
drivers/net/bonding/bond_main.c +2 -2
@@ -560,8 +560,8 @@ static int bond_update_speed_duplex(struct slave *slave)u32 slave_speed;int res; - slave->speed =-1 ; - slave->duplex =-1 ; + slave->speed =SPEED_UNKNOWN ; + slave->duplex =DUPLEX_UNKNOWN ; res =__ethtool_get_settings (slave_dev, &ecmd);if (res <0 )
drivers/net/bonding/bond_procfs.c +2 -2
@@ -158,12 +158,12 @@ static void bond_info_show_slave(struct seq_file *seq,seq_printf (seq,"\nSlave Interface: %s\n" , slave->dev->name);seq_printf (seq,"MII Status: %s\n" , (slave->link =BOND_LINK_UP ) ?"up" :"down" ); -if (slave->speed =-1 ) +if (slave->speed =SPEED_UNKNOWN )seq_printf (seq,"Speed: %s\n" ,"Unknown" );else seq_printf (seq,"Speed: %d Mbps\n" , slave->speed); -if (slave->duplex =-1 ) +if (slave->duplex =DUPLEX_UNKNOWN )seq_printf (seq,"Duplex: %s\n" ,"Unknown" );else seq_printf (seq,"Duplex: %s\n" , slave->duplex ?"full" :"half" );
include/linux/ethtool.h +2 -0
@@ -1097,10 +1097,12 @@ struct ethtool_ops {#define SPEED_1000 1000#define SPEED_2500 2500#define SPEED_10000 10000 +#define SPEED_UNKNOWN -1/* Duplex, half or full. */ #define DUPLEX_HALF 0x00#define DUPLEX_FULL 0x01 +#define DUPLEX_UNKNOWN 0xff/* Which connector port. */ #define PORT_TP 0x00
Quoted 25 lines · Dan Carpenter
slave->duplexis a u8 type so the inbond_info_show_slave()when we check "if (slave->duplex= -1)", it's always false.Signed-off-by Dan Carpenter <[email protected]>drivers/net/bonding/bond_main.c +2
-2@@ -560,8 +560,8 @@ static int bond_update_speed_duplex(struct slave *slave)u32 slave_speed;int res; - slave->speed =-1 ; - slave->duplex =-1 ; + slave->speed =SPEED_UNKNOWN ; + slave->duplex =DUPLEX_UNKNOWN ; res =__ethtool_get_settings (slave_dev, &ecmd);if (res <0 )drivers/net/bonding/bond_procfs.c +0
-0
drivers/net/bonding/bond_procfs.c static void bond_info_show_slave(struct seq_file *seq, · line 164
Quoted 8 lines
drivers/net/bonding/bond_procfs.c +1
-1@@ -158,12 +158,12 @@ static void bond_info_show_slave(struct seq_file *seq,seq_printf (seq,"\nSlave Interface: %s\n" , slave->dev->name);seq_printf (seq,"MII Status: %s\n" , (slave->link =BOND_LINK_UP ) ?"up" :"down" ); -if (slave->speed =-1 ) +if (slave->speed =SPEED_UNKNOWN )seq_printf (seq,"Speed: %s\n" ,"Unknown" );else seq_printf (seq,"Speed: %d Mbps\n" , slave->speed);
Since you #define SPEED_UNKNOWN to -1 (below), how does this
actually change anything? Did you mean 0xffff (because struct ethtool_cmd's speed is a u16)?
Running on a moderately recent net-next (without the very recent
change to bond_update_speed_duplex), I see that bonding indeed doesn't get the speed or duplex correct after a cable pull:
Slave Interface: eth2
MII Status: down
Speed: 100 Mbps
Duplex: full
so perhaps a rational (unsigned-friendly) SPEED_UNKNOWN andDUPLEX_UNKNOWN are needed, but I'm not sure how this #define actually would change any behavior in the bonding case.
drivers/net/bonding/bond_procfs.c static void bond_info_show_slave(struct seq_file *seq, · line 169
Quoted 5 lines
drivers/net/bonding/bond_procfs.c +1
-1-if (slave->duplex =-1 ) +if (slave->duplex =DUPLEX_UNKNOWN )seq_printf (seq,"Duplex: %s\n" ,"Unknown" );else seq_printf (seq,"Duplex: %s\n" , slave->duplex ?"full" :"half" );
This one might "work," but it seems to depend on the fact that
the integral conversion of -1 to an 8 bit unsigned type will be 255 (0xff). I believe that's true (according to the ISO C copy I have handy), but I'm not sure that kind of implicit assumption should be built into the code. At least not without some explanation.
-J
Quoted 7 lines
include/linux/ethtool.h +0
-0@@ -1097,10 +1097,12 @@ struct ethtool_ops {#define SPEED_1000 1000#define SPEED_2500 2500
include/linux/ethtool.h struct ethtool_ops { · line 1108
Quoted 10 lines
include/linux/ethtool.h +2
-0#define SPEED_10000 10000 +#define SPEED_UNKNOWN -1/* Duplex, half or full. */ #define DUPLEX_HALF 0x00#define DUPLEX_FULL 0x01 +#define DUPLEX_UNKNOWN 0xff/* Which connector port. */ #define PORT_TP 0x00
---
-Jay Vosburgh, IBM Linux Technology Center, [email protected]
Quoted 30 lines · Jay Vosburgh
Quoted 29 lines · Dan Carpenter
slave->duplexis a u8 type so the inbond_info_show_slave()when we check "if (slave->duplex= -1)", it's always false.Signed-off-by Dan Carpenter <[email protected]>drivers/net/bonding/bond_main.c +2
-2@@ -560,8 +560,8 @@ static int bond_update_speed_duplex(struct slave *slave)u32 slave_speed;int res; - slave->speed =-1 ; - slave->duplex =-1 ; + slave->speed =SPEED_UNKNOWN ; + slave->duplex =DUPLEX_UNKNOWN ; res =__ethtool_get_settings (slave_dev, &ecmd);if (res <0 )drivers/net/bonding/bond_procfs.c +0
-1@@ -158,12 +158,12 @@ static void bond_info_show_slave(struct seq_file *seq,seq_printf (seq,"\nSlave Interface: %s\n" , slave->dev->name);seq_printf (seq,"MII Status: %s\n" , (slave->link =BOND_LINK_UP ) ?"up" :"down" ); -if (slave->speed =-1 )
Quoted 8 lines
drivers/net/bonding/bond_procfs.c static void bond_info_show_slave(struct seq_file *seq, · line 164
Quoted 4 lines
drivers/net/bonding/bond_procfs.c +1
-0+if (slave->speed =SPEED_UNKNOWN )seq_printf (seq,"Speed: %s\n" ,"Unknown" );else seq_printf (seq,"Speed: %d Mbps\n" , slave->speed);Since you #define
SPEED_UNKNOWNto -1 (below), how does this
actually change anything? Did you mean0xffff(because struct ethtool_cmd's speed is a u16)?
The speed in ethtool_cmd is 32 bits divided between two fields.
Quoted 4 lines
Running on a moderately recent net-next (without the very recent
change tobond_update_speed_duplex), I see that bonding indeed doesn't get the speed or duplex correct after a cable pull:
Quoted 8 lines
Slave Interface: eth2
MII Status: down
Speed: 100 Mbps
Duplex: fullso perhaps a rational (unsigned-friendly)
SPEED_UNKNOWNandDUPLEX_UNKNOWNare needed, but I'm not sure how this #define actually would change any behavior in the bonding case.
Agree that they should be defined somewhere. The ethtool utility recognises speed values of 0, (u16)(-1) and (u32)(-1) as 'unknown'. Personally I think 0 makes more sense than (u32)(-1) but it doesn't matter much.
Quoted 11 lines
drivers/net/bonding/bond_procfs.c static void bond_info_show_slave(struct seq_file *seq, · line 169
Quoted 5 lines
drivers/net/bonding/bond_procfs.c +1
-1-if (slave->duplex =-1 ) +if (slave->duplex =DUPLEX_UNKNOWN )seq_printf (seq,"Duplex: %s\n" ,"Unknown" );else seq_printf (seq,"Duplex: %s\n" , slave->duplex ?"full" :"half" );This one might "work," but it seems to depend on the fact that
the integral conversion of -1 to an 8 bit unsigned type will be 255 (0xff). I believe that's true (according to the ISO C copy I have handy), but I'm not sure that kind of implicit assumption should be built into the code. At least not without some explanation.
[...]
It's true and does not need explanation. Quite why anyone expected a negative value to survive conversion to u8 and back to int, now that deserves explanation...
Ben.
Signature · 3 lines
Ben Hutchings, Staff Engineer, Solarflare Not speaking for my employer; that's the marketing department's job. They asked us to note that Solarflare product names are trademarked.
Quoted 5 lines · Jay Vosburgh
Since you #define
SPEED_UNKNOWNto -1 (below), how does this
actually change anything? Did you mean0xffff(because struct ethtool_cmd's speed is a u16)?
Sorry I could have explained this better in the changelog. The slave->speed is stored in a u32 and the -1 works fine as is. Obviously, as you point out the define doesn't change anything. I just changed it so it would look symetric with DUPLEX_UNKNOWN.
But I think you missed that I defined #define DUPLEX_UNKNOWN 0xff. Before it we used a -1 for both and that didn't work.
I can resend this with a note about the SPEED_UNKNOWN cleanup if you'd like. I'll do that tomorrow or Sunday.
regards,
dan carpenter
David Miller
maintainerFrom: Dan Carpenter <[email protected]>
Date: Fri, 4 Nov 2011 23:53:51 +0300
Quoted 12 lines · Jay Vosburgh
Quoted 5 lines
Since you #define
SPEED_UNKNOWNto -1 (below), how does this
actually change anything? Did you mean0xffff(because struct ethtool_cmd's speed is a u16)?Sorry I could have explained this better in the changelog. The
slave->speedis stored in a u32 and the -1 works fine as is. Obviously, as you point out the define doesn't change anything. I just changed it so it would look symetric withDUPLEX_UNKNOWN.
Quoted 5 lines
But I think you missed that I defined #define
DUPLEX_UNKNOWN0xff. Before it we used a -1 for both and that didn't work.I can resend this with a note about the
SPEED_UNKNOWNcleanup if you'd like. I'll do that tomorrow or Sunday.
That won't be necessary, I'll apply your patch as-is.
Thanks Dan.