Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use /sys/class/net/<device>/carrier instead of operstate in is_up() #605

Merged
merged 2 commits into from
May 1, 2020

Conversation

itsfarseen
Copy link
Contributor

When using android usb tethering in Arch Linux (kernel: Linux 5.6.5-zen4-1-zen),
operstate file shows unknown. But the carrier file correctly shows 1.
ethtool also correctly says Link detected: yes.

When using android usb tethering in Arch Linux (kernel: Linux 5.6.5-zen4-1-zen),
`operstate` file shows `unknown`. But the `carrier` file correctly shows `1`.
  `ethtool` also correctly says `Link detected: yes`.
@ammgws
Copy link
Collaborator

ammgws commented Apr 24, 2020

It seems like there isn't 100% overlap between the two as operstate seems to offer some more context, but to be honest I'm not sure of the implications of losing that information. Unless you're sure that switching to just checking carrier is fine, perhaps we could check for both?

/sys/class/net/xxx/carrier

Indicates the current physical link state of the interface.
Posssible values are:
0: physical link is down
1: physical link is up

/sys/class/net/xxx/operstate

Indicates the interface RFC2863 operational state as a string.
Possible values are:
"unknown", "notpresent", "down", "lowerlayerdown", "testing",
"dormant", "up".

IF_OPER_UNKNOWN (0):
Interface is in unknown state, neither driver nor userspace has set
operational state. Interface must be considered for user data as
setting operational state has not been implemented in every driver.
IF_OPER_DOWN (2):
Interface is unable to transfer data on L1, f.e. ethernet is not
plugged or interface is ADMIN down.
IF_OPER_UP (6):
Interface is operational up and can be used.

https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-net
https://www.kernel.org/doc/Documentation/networking/operstates.txt

@atheriel
Copy link
Collaborator

I'd go with the official docs here and say we should check both. It's also a good idea to check the source of ethtool to figure out what it's doing here, if possible.

@itsfarseen
Copy link
Contributor Author

Yeah it is a better idea to check both. I will re-add the code.
But by the way, I have been using this patch for a few days and it shows carrier file is not found error when I disconnect and reconnect the device. It only happens sometimes and goes away if I reload i3. This problem was not there when using operstate file.
What could be happening here?

@ammgws
Copy link
Collaborator

ammgws commented Apr 25, 2020

Seems like the file doesn't exist when the interface is down:
https://linuxconfig.org/how-to-detect-whether-a-physical-cable-is-connected-to-network-card-slot-on-linux

Checking both could be like:

  1. check operstate
  2. if not 'up', then check carrier
    3a. if carrier is 1 then return 'up'
    3b if carrier is 0/doesn't exist then return the original operstate result

@ammgws
Copy link
Collaborator

ammgws commented May 1, 2020

Ended up adding it myself. Thanks!

@ammgws ammgws merged commit eb74b55 into greshake:master May 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants