Skip to contentSkip to conversation

Network link detection

DISCUSSION Nico Schümann 7 participants 10 messages Mar 3, 2011 – Mar 7, 2011

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

Dear kernel developers,

currently I'm looking for a way to find out whether a network link went down or up. Of course there is mii-tool, which can watch devices, too.

So for now, I created a small program that polls the MII_LINK_OK flag with the SIOCGMIIREG ioctl - the same thing that mii-tools does. But polling that often for a link change that occurs maybe once in a month sounds like waste of energy.

Unfortunately, I was not able to find out a way to be notified about link status changes asynchronously. Is there a way? I'm looking for something like "inotify for link states".

There are some debug outputs as in drivers/net/natsemi.c:1672

                        printk(KERN_NOTICE "%s: link up.\n", dev->name);

so it would be quite easy to insert a notification.

If there is no way yet to get these notifications asynchronosly, would there be real use for it or am I just missing something?

Thanks,

Nico

#1 lore raw
Quoted 5 lines · Nico Schümann

Dear kernel developers,

currently I'm looking for a way to find out whether a network link went down or up. Of course there is mii-tool, which can watch devices, too.

Quoted 9 lines

So for now, I created a small program that polls the MII_LINK_OK flag with the SIOCGMIIREG ioctl - the same thing that mii-tools does. But polling that often for a link change that occurs maybe once in a month sounds like waste of energy.

Unfortunately, I was not able to find out a way to be notified about link status changes asynchronously. Is there a way? I'm looking for something like "inotify for link states".

I guess you could use inotify to keep an eye on the 'carrier' file in sysfs (for example, from my system; /sys/devices/pci0000:00/0000:00:19.0/net/eth0/carrier )
That file will contain "0" if there is no link and "1" if there is a link.

Signature · 3 lines
Jesper Juhl <[email protected]>            http://www.chaosbits.net/
Plain text mails only, please.
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
#2 lore raw
Quoted 12 lines · Jesper Juhl
Quoted 10 lines · Nico Schümann

Dear kernel developers,

currently I'm looking for a way to find out whether a network link went down or up. Of course there is mii-tool, which can watch devices, too.

So for now, I created a small program that polls the MII_LINK_OK flag with the SIOCGMIIREG ioctl - the same thing that mii-tools does. But polling that often for a link change that occurs maybe once in a month sounds like waste of energy.

Quoted 8 lines
Quoted 4 lines

Unfortunately, I was not able to find out a way to be notified about link status changes asynchronously. Is there a way? I'm looking for something like "inotify for link states".

I guess you could use inotify to keep an eye on the 'carrier' file in sysfs (for example, from my system; /sys/devices/pci0000:00/0000:00:19.0/net/eth0/carrier )
That file will contain "0" if there is no link and "1" if there is a link.

Does sysfs support inotify?
I don't think so.

Quoted 5 lines

--
Jesper Juhl <[email protected]>            http://www.chaosbits.net/
Plain text mails only, please.
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html

Signature · 2 lines
Thanks,
//richard
#3 lore raw
In reply to Nico Schümann
Quoted 8 lines · Nico Schümann

Dear kernel developers,

currently I'm looking for a way to find out whether a network link went down or up. Of course there is mii-tool, which can watch devices, too.

Unfortunately, I was not able to find out a way to be notified about link status changes asynchronously. Is there a way? I'm looking for something like "inotify for link states".

You might look at whether you could write a kernel module to register for NETDEV_CHANGE notifications and pass that back to userspace.

Chris

Signature · 5 lines
Chris Friesen
Software Developer
GENBAND
[email protected]
www.genband.com
maintainer
#4 lore raw

From: Chris Friesen <[email protected]>
Date: Thu, 03 Mar 2011 15:38:35 -0600

Quoted 2 lines

You might look at whether you could write a kernel module to register for NETDEV_CHANGE notifications and pass that back to userspace.

This is the kind of responses you get when you ask networking specific questions and don't CC: netdev :-/

There is this thing called netlink, you can listen for arbitrary network state change events on a socket, and get the link state notifications you are looking for. It's in use by many real applications like NetworkManager and co.

#5 lore raw
Quoted 9 lines · David Miller

From: Chris Friesen <[email protected]>
Date: Thu, 03 Mar 2011 15:38:35 -0600

Quoted 2 lines

You might look at whether you could write a kernel module to register for NETDEV_CHANGE notifications and pass that back to userspace.

This is the kind of responses you get when you ask networking specific questions and don't CC: netdev :-/

Thank you for CC.

Quoted 4 lines

There is this thing called netlink, you can listen for arbitrary network state change events on a socket, and get the link state notifications you are looking for. It's in use by many real applications like NetworkManager and co.

That really looks like what I'm looking for. I was already wondering where NetworkManager gets the link state changes from, but I just expected it to poll. So now I'll read a bit of documentation and hopefully get it work.

Thanks to everyone,

Nico

#6 lore raw
Quoted 18 lines · Nico Schümann
Quoted 9 lines · David Miller

From: Chris Friesen <[email protected]>
Date: Thu, 03 Mar 2011 15:38:35 -0600

Quoted 2 lines

You might look at whether you could write a kernel module to register for NETDEV_CHANGE notifications and pass that back to userspace.

This is the kind of responses you get when you ask networking specific questions and don't CC: netdev :-/

Thank you for CC.

Quoted 4 lines

There is this thing called netlink, you can listen for arbitrary network state change events on a socket, and get the link state notifications you are looking for. It's in use by many real applications like NetworkManager and co.

Quoted 4 lines

That really looks like what I'm looking for. I was already wondering where NetworkManager gets the link state changes from, but I just expected it to poll. So now I'll read a bit of documentation and hopefully get it work.

http://cgit.freedesktop.org/NetworkManager/NetworkManager/tree/src/nm-netlink-monitor.c

NM uses libnl as the basic library for parsing netlink messages and handling communication with the kernel. Which is why you'll see a lot of nl_* calls in there. NM sets up the netlink connection using libnl, then creates a GIOChannel to handle communication over the netlink socket. When something comes in (to event_handler()) the code handles error conditions on the socket, then dispatches to libnl for processing. libnl then calls back into NM to handle the actual message in event_msg_ready().

Dan

#7 lore raw
In reply to David Miller
Quoted 8 lines · David Miller

From: Chris Friesen <[email protected]>
Date: Thu, 03 Mar 2011 15:38:35 -0600

Quoted 2 lines

You might look at whether you could write a kernel module to register for NETDEV_CHANGE notifications and pass that back to userspace.

This is the kind of responses you get when you ask networking specific questions and don't CC: netdev :-/

My apologies for misleading the original poster. I can only claim a brain fart since I've actually used rtnetlink for other things.

Quoted 4 lines

There is this thing called netlink, you can listen for arbitrary network state change events on a socket, and get the link state notifications you are looking for. It's in use by many real applications like NetworkManager and co.

For future reference then, to listen for link state notifications you'd use NETLINK_ROUTE with nl_groups set to RTMGRP_LINK, and the link state will be signaled in the if_flags field of received messages?

Chris

Signature · 5 lines
Chris Friesen
Software Developer
GENBAND
[email protected]
www.genband.com
#9 lore raw
Quoted 5 lines · Stephen Hemminger

Simple answer is use netlink. That is what all real services use (NM, Quagga, ...) Netlink can be hard to parse, so I recommend using a wrapper library.

Simplest example is the link-event example in libmnl
http://www.netfilter.org/projects/libmnl/index.html

The link-event example does about what I plan to do - and it just works. So that pointer was just right. Thank you!

Nico