Skip to contentSkip to conversation

netdev carrier changes is one even after ethernet link up.

DISCUSSION Bhadram Varka 2 participants 7 messages Aug 31, 2017 – Sep 4, 2017 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive

+ netdev

From: Bhadram Varka
Sent: Thursday, August 31, 2017 11:24 AM
To: '[email protected]' <[email protected]>; '[email protected]' <[email protected]>
Cc: linux-netdev <[email protected]>
Subject: netdev carrier changes is one even after ethernet link up.

Hi,

I have observed that carrier_changes is one even in case of the ethernet link is up.
After investigating the code below is my observation -

ethernet_driver_probe()

+--->phy_connect()
|     +--->phy_attach_direct()
|           +---> netif_carrier_off()    : which increments carrier_changes to one.
+--->register_netdevice() : will the carrier_changes becomes zero here ?
+--->netif_carrier_off(): not increment the carrier_changes since __LINK_STATE_NOCARRIER already set.

Quoted 1 line

From ethernet driver open will start the PHY and trigger the phy_state_machine.

Phy_state_machine workqueue calling netif_carrier_on() once the link is UP. netif_carrier_on() increments the carrier_changes by one.

After link is UP if we check the carrier_changes sysfs node - it will be one only.

$ cat /sys/class/net/eth0/carrier_changes
1
 
After reverting the change - https://lkml.org/lkml/2016/1/9/173 (net: phy: turn carrier off on phy attach) then I could see the carrier changes incremented to 2 after Link UP.
$ cat /sys/class/net/eth0/carrier_changes
2

Thanks,
Bhadram.
-----------------------------------------------------------------------------------
This email message is for the sole use of the intended recipient(s) and may contain confidential information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
-----------------------------------------------------------------------------------

#2 lore raw
In reply to a message not in the archive
Quoted 29 lines · Bhadram Varka

Hi,

I have observed that carrier_changes is one even in case of the ethernet link is up.

After investigating the code below is my observation –

ethernet_driver_probe()

+--->phy_connect()

| +--->phy_attach_direct()

| +---> netif_carrier_off() : which increments
carrier_changes to one.

+--->register_netdevice() : will the carrier_changes becomes zero here ?

+--->netif_carrier_off(): not increment the carrier_changes since __LINK_STATE_NOCARRIER already set.

Quoted 6 lines

From ethernet driver open will start the PHY and trigger the phy_state_machine.

Phy_state_machine workqueue calling netif_carrier_on() once the link is UP.

netif_carrier_on() increments the carrier_changes by one.

If the call trace is correct, then there is at least two problems here:

- phy_connect() does start the PHY machine which means that as soon as it detects a link state of any kind (up or down) it can call netif_carrier_off() respectively netif_carrier_on()

- as soon as you call register_netdevice() notifiers run and other parts of the kernel or user-space programs can see an inconsistent link state

I would suggest doing the following sequence instead:

netif_carrier_off()
register_netdevice()
phy_connect()

Which should result in a consistent link state and carrier value.

Quoted 35 lines

After link is UP if we check the carrier_changes sysfs node - it will be one only.

$ cat /sys/class/net/eth0/carrier_changes

1

After reverting the change - https://lkml.org/lkml/2016/1/9/173 (net:
phy: turn carrier off on phy attach) then I could see the carrier changes incremented to 2 after Link UP.

$ cat /sys/class/net/eth0/carrier_changes

2

Thanks,

Bhadram.

------------------------------------------------------------------------
This email message is for the sole use of the intended recipient(s) and may contain confidential information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
------------------------------------------------------------------------

Signature · 1 line
Florian
#3 lore raw

Thanks for responding.

-----Original Message-----
From: Florian Fainelli [mailto:[email protected]]
Sent: Friday, September 01, 2017 5:53 AM
To: Bhadram Varka <[email protected]>; [email protected]
Cc: linux-netdev <[email protected]>
Subject: Re: netdev carrier changes is one even after ethernet link up.

Quoted 29 lines · Bhadram Varka

Hi,

I have observed that carrier_changes is one even in case of the ethernet link is up.

After investigating the code below is my observation –

ethernet_driver_probe()

+--->phy_connect()

| +--->phy_attach_direct()

| +---> netif_carrier_off() : which increments
carrier_changes to one.

+--->register_netdevice() : will the carrier_changes becomes zero here ?

+--->netif_carrier_off(): not increment the carrier_changes since __LINK_STATE_NOCARRIER already set.

Quoted 6 lines

From ethernet driver open will start the PHY and trigger the phy_state_machine.

Phy_state_machine workqueue calling netif_carrier_on() once the link is UP.

netif_carrier_on() increments the carrier_changes by one.

If the call trace is correct, then there is at least two problems here:

- phy_connect() does start the PHY machine which means that as soon as it detects a link state of any kind (up or down) it can call netif_carrier_off() respectively netif_carrier_on()

- as soon as you call register_netdevice() notifiers run and other parts of the kernel or user-space programs can see an inconsistent link state

I would suggest doing the following sequence instead:

netif_carrier_off()
register_netdevice()
phy_connect()

Which should result in a consistent link state and carrier value.

Yes, It will address the issue.

If we did the phy_conect in ndo_open it will make the carrier changes as one. But if we did in probe function then it's not working.

In ethernet driver probe - (below sequence is not working) phy_connect()
register_netdevice()
netif_carrier_off()

working sequence:
In probe():
register_netdevice()
ndo_open:
phy_connect()

Thanks,
Bhadram.

Quoted 28 lines

After link is UP if we check the carrier_changes sysfs node - it will be one only.

$ cat /sys/class/net/eth0/carrier_changes

1

After reverting the change - https://lkml.org/lkml/2016/1/9/173 (net:
phy: turn carrier off on phy attach) then I could see the carrier changes incremented to 2 after Link UP.

$ cat /sys/class/net/eth0/carrier_changes

2

Thanks,

Bhadram.

Quoted 8 lines

----------------------------------------------------------------------
-- This email message is for the sole use of the intended recipient(s) and may contain confidential information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
----------------------------------------------------------------------
--

--
Florian

#4 lore raw
In reply to Florian Fainelli

Thanks for responding. Now responding inline

Quoted 54 lines

-----Original Message-----
From: Florian Fainelli [mailto:[email protected]]
Sent: Friday, September 01, 2017 5:53 AM
To: Bhadram Varka <[email protected]>; [email protected]
Cc: linux-netdev <[email protected]>
Subject: Re: netdev carrier changes is one even after ethernet link up.

Quoted 29 lines · Bhadram Varka

Hi,

I have observed that carrier_changes is one even in case of the ethernet link is up.

After investigating the code below is my observation –

ethernet_driver_probe()

+--->phy_connect()

| +--->phy_attach_direct()

| +---> netif_carrier_off() : which increments
carrier_changes to one.

+--->register_netdevice() : will the carrier_changes becomes zero here ?

+--->netif_carrier_off(): not increment the carrier_changes since __LINK_STATE_NOCARRIER already set.

Quoted 4 lines

From ethernet driver open will start the PHY and trigger the phy_state_machine.

Phy_state_machine workqueue calling netif_carrier_on() once the link is

UP.

Quoted 2 lines

netif_carrier_on() increments the carrier_changes by one.

If the call trace is correct, then there is at least two problems here:

- phy_connect() does start the PHY machine which means that as soon as it detects a link state of any kind (up or down) it can call netif_carrier_off() respectively netif_carrier_on()

- as soon as you call register_netdevice() notifiers run and other parts of the kernel or user-space programs can see an inconsistent link state

Quoted 8 lines

I would suggest doing the following sequence instead:

netif_carrier_off()
register_netdevice()
phy_connect()

Which should result in a consistent link state and carrier value.

Yes, It will address the issue.

If we did the phy_conect in ndo_open it will make the carrier changes as two. But if we did in probe function then it's not working.

In ethernet driver probe - (below sequence is not working) phy_connect()
register_netdevice()
netif_carrier_off()

working sequence:
In probe():
register_netdevice()
ndo_open:
phy_connect()

After reverting - https://lkml.org/lkml/2016/1/9/173 this works if we do phy_connect in probe as well.

Thanks,
Bhadram.

Quoted 40 lines
Quoted 28 lines

After link is UP if we check the carrier_changes sysfs node - it will be one only.

$ cat /sys/class/net/eth0/carrier_changes

1

After reverting the change - https://lkml.org/lkml/2016/1/9/173 (net:
phy: turn carrier off on phy attach) then I could see the carrier changes incremented to 2 after Link UP.

$ cat /sys/class/net/eth0/carrier_changes

2

Thanks,

Bhadram.

Quoted 8 lines

----------------------------------------------------------------------
-- This email message is for the sole use of the intended recipient(s) and may contain confidential information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
----------------------------------------------------------------------
--

--
Florian

#5 lore raw
Quoted 73 lines · Bhadram Varka

Thanks for responding. Now responding inline

Quoted 54 lines

-----Original Message-----
From: Florian Fainelli [mailto:[email protected]]
Sent: Friday, September 01, 2017 5:53 AM
To: Bhadram Varka <[email protected]>; [email protected]
Cc: linux-netdev <[email protected]>
Subject: Re: netdev carrier changes is one even after ethernet link up.

Quoted 29 lines · Bhadram Varka

Hi,

I have observed that carrier_changes is one even in case of the ethernet link is up.

After investigating the code below is my observation –

ethernet_driver_probe()

+--->phy_connect()

| +--->phy_attach_direct()

| +---> netif_carrier_off() : which increments
carrier_changes to one.

+--->register_netdevice() : will the carrier_changes becomes zero here ?

+--->netif_carrier_off(): not increment the carrier_changes since __LINK_STATE_NOCARRIER already set.

Quoted 4 lines

From ethernet driver open will start the PHY and trigger the phy_state_machine.

Phy_state_machine workqueue calling netif_carrier_on() once the link is

UP.

Quoted 2 lines

netif_carrier_on() increments the carrier_changes by one.

If the call trace is correct, then there is at least two problems here:

- phy_connect() does start the PHY machine which means that as soon as it detects a link state of any kind (up or down) it can call netif_carrier_off() respectively netif_carrier_on()

- as soon as you call register_netdevice() notifiers run and other parts of the kernel or user-space programs can see an inconsistent link state

Quoted 8 lines

I would suggest doing the following sequence instead:

netif_carrier_off()
register_netdevice()
phy_connect()

Which should result in a consistent link state and carrier value.

Yes, It will address the issue.

If we did the phy_conect in ndo_open it will make the carrier changes as two. But if we did in probe function then it's not working.

In ethernet driver probe - (below sequence is not working) phy_connect()
register_netdevice()
netif_carrier_off()

Quoted 7 lines

working sequence:
In probe():
register_netdevice()
ndo_open:
phy_connect()

After reverting - https://lkml.org/lkml/2016/1/9/173 this works if we do phy_connect in probe as well.

But as mentioned before you should not be doing the PHY probe in your driver's probe function for different reasons:

- the probe function's responsibility is to initialize the driver and the HW to a state where they both have everything needed but it should be in quiesced state. There is no guarantee that your network device may ever be used after probe unless something calls ndo_open(), you should therefore keep all resources to a minimum: memory allocated, HW powered down etc.

- there is a race condition between the PHY state machine started in phy_connect(), and when register_netdevice() is called and notifiers running which can lead to an inconsistent state for the carrier

So considering that your driver does not do that, I am not sure what you are expecting...

Signature · 1 line
Florian
#6 lore raw

Hi Florian,

Quoted 102 lines

-----Original Message-----
From: Florian Fainelli [mailto:[email protected]]
Sent: Saturday, September 02, 2017 7:25 AM
To: Bhadram Varka <[email protected]>; [email protected]
Cc: linux-netdev <[email protected]>
Subject: Re: netdev carrier changes is one even after ethernet link up.

Quoted 64 lines · Bhadram Varka

Thanks for responding. Now responding inline

Quoted 62 lines

-----Original Message-----
From: Florian Fainelli [mailto:[email protected]]
Sent: Friday, September 01, 2017 5:53 AM
To: Bhadram Varka <[email protected]>; [email protected]
Cc: linux-netdev <[email protected]>
Subject: Re: netdev carrier changes is one even after ethernet link up.

Quoted 29 lines · Bhadram Varka

Hi,

I have observed that carrier_changes is one even in case of the ethernet link is up.

After investigating the code below is my observation -

ethernet_driver_probe()

+--->phy_connect()

| +--->phy_attach_direct()

| +---> netif_carrier_off() : which increments
carrier_changes to one.

+--->register_netdevice() : will the carrier_changes becomes zero here ?

+--->netif_carrier_off(): not increment the carrier_changes since __LINK_STATE_NOCARRIER already set.

Quoted 5 lines

From ethernet driver open will start the PHY and trigger the phy_state_machine.

Phy_state_machine workqueue calling netif_carrier_on() once the link is

UP.

Quoted 2 lines

netif_carrier_on() increments the carrier_changes by one.

If the call trace is correct, then there is at least two problems here:

- phy_connect() does start the PHY machine which means that as soon as it detects a link state of any kind (up or down) it can call netif_carrier_off() respectively netif_carrier_on()

- as soon as you call register_netdevice() notifiers run and other parts of the kernel or user-space programs can see an inconsistent link state

I would suggest doing the following sequence instead:

netif_carrier_off()
register_netdevice()
phy_connect()

Quoted 5 lines
Quoted 2 lines

Which should result in a consistent link state and carrier value.

Yes, It will address the issue.

If we did the phy_conect in ndo_open it will make the carrier changes as

two. But if we did in probe function then it's not working.

Quoted 6 lines

In ethernet driver probe - (below sequence is not working) phy_connect()
register_netdevice()
netif_carrier_off()

Quoted 7 lines

working sequence:
In probe():
register_netdevice()
ndo_open:
phy_connect()

After reverting - https://lkml.org/lkml/2016/1/9/173 this works if we do

phy_connect in probe as well.

But as mentioned before you should not be doing the PHY probe in your driver's probe function for different reasons:

- the probe function's responsibility is to initialize the driver and the HW to a state where they both have everything needed but it should be in quiesced state. There is no guarantee that your network device may ever be used after probe unless something calls ndo_open(), you should therefore keep all resources to a minimum: memory allocated, HW powered down etc.

Quoted 4 lines

- there is a race condition between the PHY state machine started in phy_connect(), and when register_netdevice() is called and notifiers running which can lead to an inconsistent state for the carrier

Agreed. But I could see lot of drivers performing the phy_connect() in driver_probe() function itself. Also observed that these drivers are performing netif_carrier_off() after register_netdevice().

My point is that we do we need to do netif_carrier_off() in phy_connect since we already doing the same after register_netdevice() ?

Thanks!
-----------------------------------------------------------------------------------
This email message is for the sole use of the intended recipient(s) and may contain confidential information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
-----------------------------------------------------------------------------------

#7 lore raw
In reply to Florian Fainelli
Quoted 104 lines

-----Original Message-----
From: Florian Fainelli [mailto:[email protected]]
Sent: Saturday, September 02, 2017 7:25 AM
To: Bhadram Varka <[email protected]>; [email protected]
Cc: linux-netdev <[email protected]>
Subject: Re: netdev carrier changes is one even after ethernet link up.

Quoted 64 lines · Bhadram Varka

Thanks for responding. Now responding inline

Quoted 62 lines

-----Original Message-----
From: Florian Fainelli [mailto:[email protected]]
Sent: Friday, September 01, 2017 5:53 AM
To: Bhadram Varka <[email protected]>; [email protected]
Cc: linux-netdev <[email protected]>
Subject: Re: netdev carrier changes is one even after ethernet link up.

Quoted 29 lines · Bhadram Varka

Hi,

I have observed that carrier_changes is one even in case of the ethernet link is up.

After investigating the code below is my observation -

ethernet_driver_probe()

+--->phy_connect()

| +--->phy_attach_direct()

| +---> netif_carrier_off() : which increments
carrier_changes to one.

+--->register_netdevice() : will the carrier_changes becomes zero here ?

+--->netif_carrier_off(): not increment the carrier_changes since __LINK_STATE_NOCARRIER already set.

Quoted 5 lines

From ethernet driver open will start the PHY and trigger the phy_state_machine.

Phy_state_machine workqueue calling netif_carrier_on() once the link is

UP.

Quoted 2 lines

netif_carrier_on() increments the carrier_changes by one.

If the call trace is correct, then there is at least two problems here:

- phy_connect() does start the PHY machine which means that as soon as it detects a link state of any kind (up or down) it can call netif_carrier_off() respectively netif_carrier_on()

- as soon as you call register_netdevice() notifiers run and other parts of the kernel or user-space programs can see an inconsistent link state

I would suggest doing the following sequence instead:

netif_carrier_off()
register_netdevice()
phy_connect()

Quoted 5 lines
Quoted 2 lines

Which should result in a consistent link state and carrier value.

Yes, It will address the issue.

If we did the phy_conect in ndo_open it will make the carrier changes as

two. But if we did in probe function then it's not working.

Quoted 6 lines

In ethernet driver probe - (below sequence is not working) phy_connect()
register_netdevice()
netif_carrier_off()

Quoted 7 lines

working sequence:
In probe():
register_netdevice()
ndo_open:
phy_connect()

After reverting - https://lkml.org/lkml/2016/1/9/173 this works if we do

phy_connect in probe as well.

But as mentioned before you should not be doing the PHY probe in your driver's probe function for different reasons:

- the probe function's responsibility is to initialize the driver and the HW to a state where they both have everything needed but it should be in quiesced state. There is no guarantee that your network device may ever be used after probe unless something calls ndo_open(), you should therefore keep all resources to a minimum: memory allocated, HW powered down etc.

Quoted 3 lines

- there is a race condition between the PHY state machine started in phy_connect(), and when register_netdevice() is called and notifiers running which can lead to an inconsistent state for the carrier

Agreed. But I could see lot of drivers performing the phy_connect() in driver_probe() function itself. Also observed that these drivers are performing netif_carrier_off() after register_netdevice().

My point is that why do we need to do netif_carrier_off() in phy_connect() since we already doing the same after register_netdevice() in MAC driver ?

Thanks!
-----------------------------------------------------------------------------------
This email message is for the sole use of the intended recipient(s) and may contain confidential information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
-----------------------------------------------------------------------------------