2011-03-03 19:58:20

by Nico Schümann

[permalink] [raw]
Subject: Network link detection

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


2011-03-03 20:04:10

by Jesper Juhl

[permalink] [raw]
Subject: Re: Network link detection

On Thu, 3 Mar 2011, Nico Sch?mann wrote:

> 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".
>
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.

--
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

2011-03-03 20:45:38

by Richard Weinberger

[permalink] [raw]
Subject: Re: Network link detection

On Thu, Mar 3, 2011 at 9:03 PM, Jesper Juhl <[email protected]> wrote:
> On Thu, 3 Mar 2011, Nico Sch?mann wrote:
>
>> 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".
>>
> 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.

> --
> 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
>

--
Thanks,
//richard

2011-03-03 21:54:26

by Chris Friesen

[permalink] [raw]
Subject: Re: Network link detection

On 03/03/2011 01:30 PM, Nico Sch?mann wrote:
> 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

--
Chris Friesen
Software Developer
GENBAND
[email protected]
http://www.genband.com

2011-03-03 22:00:31

by David Miller

[permalink] [raw]
Subject: Re: Network link detection

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

> 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.

2011-03-03 22:07:52

by Stephen Hemminger

[permalink] [raw]
Subject: Re: Network link detection

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

2011-03-03 22:30:00

by Nico Schümann

[permalink] [raw]
Subject: Re: Network link detection

On Thu, Mar 03, 2011 at 02:01:06PM -0800, David Miller wrote:
> From: Chris Friesen <[email protected]>
> Date: Thu, 03 Mar 2011 15:38:35 -0600
>
> > 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.

> 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

2011-03-03 23:55:00

by Chris Friesen

[permalink] [raw]
Subject: Re: Network link detection

On 03/03/2011 04:01 PM, David Miller wrote:
> From: Chris Friesen <[email protected]>
> Date: Thu, 03 Mar 2011 15:38:35 -0600
>
>> 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.

> 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

--
Chris Friesen
Software Developer
GENBAND
[email protected]
http://www.genband.com

2011-03-04 06:36:04

by Nico Schümann

[permalink] [raw]
Subject: Re: Network link detection

On Thu, Mar 03, 2011 at 02:07:33PM -0800, Stephen Hemminger wrote:
> 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

2011-03-07 19:47:50

by Dan Williams

[permalink] [raw]
Subject: Re: Network link detection

On Thu, 2011-03-03 at 23:29 +0100, Nico Schümann wrote:
> On Thu, Mar 03, 2011 at 02:01:06PM -0800, David Miller wrote:
> > From: Chris Friesen <[email protected]>
> > Date: Thu, 03 Mar 2011 15:38:35 -0600
> >
> > > 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.
>
> > 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.

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