2005-05-18 10:37:44

by filipe abrantes

[permalink] [raw]
Subject: Detecting link up

Hi all,

I need to detect when an interface (wired ethernet) has link up/down. Is
there a system signal which is sent when this happens? What is the best
way to this programatically?

Best Regards

Filipe


--
Filipe Lameiro Abrantes
INESC Porto
Campus da FEUP
Rua Dr. Roberto Frias, 378
4200-465 Porto
Portugal

Phone: +351 22 209 4266
E-mail: [email protected]


2005-05-18 11:40:43

by Martin Zwickel

[permalink] [raw]
Subject: Re: Detecting link up

On Wed, 18 May 2005 11:35:12 +0100
Filipe Abrantes <[email protected]> bubbled:

> Hi all,
>
> I need to detect when an interface (wired ethernet) has link up/down.
> Is there a system signal which is sent when this happens? What is the
> best way to this programatically?

mii-tool?

--
MyExcuse:
Not enough interrupts

Martin Zwickel <[email protected]>
Research & Development

TechnoTrend AG <http://www.technotrend.de>


Attachments:
(No filename) (189.00 B)

2005-05-18 13:11:53

by Vaibhav Nivargi

[permalink] [raw]
Subject: Re: Detecting link up

try looking at the ioctls which mii-tool / ethtool make

regards,
Vaibhav

On 5/18/05, Martin Zwickel <[email protected]> wrote:
> On Wed, 18 May 2005 11:35:12 +0100
> Filipe Abrantes <[email protected]> bubbled:
>
> > Hi all,
> >
> > I need to detect when an interface (wired ethernet) has link up/down.
> > Is there a system signal which is sent when this happens? What is the
> > best way to this programatically?
>
> mii-tool?
>
> --
> MyExcuse:
> Not enough interrupts
>
> Martin Zwickel <[email protected]>
> Research & Development
>
> TechnoTrend AG <http://www.technotrend.de>
>
>
>

2005-05-18 14:27:00

by linux-os (Dick Johnson)

[permalink] [raw]
Subject: Re: Detecting link up

On Wed, 18 May 2005, Vaibhav Nivargi wrote:

> try looking at the ioctls which mii-tool / ethtool make
>
> regards,
> Vaibhav
>
> On 5/18/05, Martin Zwickel <[email protected]> wrote:
>> On Wed, 18 May 2005 11:35:12 +0100
>> Filipe Abrantes <[email protected]> bubbled:
>>
>>> Hi all,
>>>
>>> I need to detect when an interface (wired ethernet) has link up/down.
>>> Is there a system signal which is sent when this happens? What is the
>>> best way to this programatically?
>>
>> mii-tool?
>>
>> --
>> MyExcuse:
>> Not enough interrupts

>>
>> Martin Zwickel <[email protected]>
>> Research & Development
>>
>> TechnoTrend AG <http://www.technotrend.de>



Cut from some working project........


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <stdint.h>
#include <net/if.h>
#include <netinet/in.h>

typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t u8;

#include <linux/sockios.h>
#include <linux/ethtool.h>

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Get the link status. This returns TRUE if the link is up and FALSE
// otherwise.
//
int32_t link_stat()
{
int s;
struct ifreq ifr;
struct ethtool_value eth;
if((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) == FAIL)
ERRORS(Socket);
bzero(&ifr, sizeof(ifr));
strcpy(ifr.ifr_name, Eth0);
ifr.ifr_data = (caddr_t) &eth;
eth.cmd = ETHTOOL_GLINK;
if(ioctl(s, SIOCETHTOOL, &ifr) == FAIL)
ERRORS(Ioctl);
(void)close(s);
return (eth.data) ? TRUE:FALSE;
}


ERRORS, TRUE, FALSE, Eth0, FAIL are macros that you should be able
to figure out for yourself. Maybe you don't have bzero() you
can use memset(x, 0x00, n).


Cheers,
Dick Johnson
Penguin : Linux version 2.6.11.9 on an i686 machine (5537.79 BogoMips).
Notice : All mail here is now cached for review by Dictator Bush.
98.36% of all statistics are fiction.

2005-05-18 14:39:09

by Max Kellermann

[permalink] [raw]
Subject: Re: Detecting link up

On 2005/05/18 13:40, Martin Zwickel <[email protected]> wrote:
> On Wed, 18 May 2005 11:35:12 +0100
> Filipe Abrantes <[email protected]> bubbled:
> > I need to detect when an interface (wired ethernet) has link up/down.
> > Is there a system signal which is sent when this happens? What is the
> > best way to this programatically?
>
> mii-tool?

A thought on a related topic:

When a NIC driver knows that there is no link, why does it even try to
transmit a packet? It could return immediately with an error code,
without applications having to wait for a timeout.

(I had a quick peek at two drivers, and they don't check the link
status)

Max

2005-05-18 15:04:40

by Oliver Neukum

[permalink] [raw]
Subject: Re: Detecting link up

Am Mittwoch, 18. Mai 2005 16:37 schrieb Max Kellermann:
> A thought on a related topic:
>
> When a NIC driver knows that there is no link, why does it even try to
> transmit a packet? It could return immediately with an error code,
> without applications having to wait for a timeout.

That would be a duplication of work. If the driver provides
link detection the network core could check for it.

Regards
Oliver

2005-05-18 15:10:55

by Coywolf Qi Hunt

[permalink] [raw]
Subject: Re: Detecting link up

On 5/18/05, Max Kellermann <[email protected]> wrote:
> On 2005/05/18 13:40, Martin Zwickel <[email protected]> wrote:
> > On Wed, 18 May 2005 11:35:12 +0100
> > Filipe Abrantes <[email protected]> bubbled:
> > > I need to detect when an interface (wired ethernet) has link up/down.
> > > Is there a system signal which is sent when this happens? What is the
> > > best way to this programatically?
> >
> > mii-tool?
>
> A thought on a related topic:
>
> When a NIC driver knows that there is no link, why does it even try to
> transmit a packet? It could return immediately with an error code,
> without applications having to wait for a timeout.
>
> (I had a quick peek at two drivers, and they don't check the link
> status)

An NIC driver doesn't know if there's other links or not. One NIC
driver is for one type of NIC. And there's also interface lo.
--
Coywolf Qi Hunt
http://sosdg.org/~coywolf/

2005-05-18 15:15:56

by linux-os (Dick Johnson)

[permalink] [raw]
Subject: Re: Detecting link up

On Wed, 18 May 2005, Max Kellermann wrote:

> On 2005/05/18 13:40, Martin Zwickel <[email protected]> wrote:
>> On Wed, 18 May 2005 11:35:12 +0100
>> Filipe Abrantes <[email protected]> bubbled:
>>> I need to detect when an interface (wired ethernet) has link up/down.
>>> Is there a system signal which is sent when this happens? What is the
>>> best way to this programatically?
>>
>> mii-tool?
>
> A thought on a related topic:
>
> When a NIC driver knows that there is no link, why does it even try to
> transmit a packet? It could return immediately with an error code,
> without applications having to wait for a timeout.
>
> (I had a quick peek at two drivers, and they don't check the link
> status)
>
> Max

The driver(s) don't transmit directly. They put data into a buffer,
usually a ring. The hardware will (depends upon the type) try to
transmit up to 16 times, anything at the current buffer location.
This allows hardware, not software, to try to get a packet on the
wire. The wire can become active or inactive at any time. Usually
the driver correctly assumes that the packet will eventually get
out. If it doesn't, upper levels will send it again. Note that
this is Ethernet, a noisy channel, sh* happens. If the protocol
is connection oriented, the data will be retried forever in an
attempt to get it through. Otherwise, the data is just dropped
on the floor. That's how networking works. You don't waste
valuable CPU resources checking to see if data got onto a wire
when it might get trashed later anyway.

Cheers,
Dick Johnson
Penguin : Linux version 2.6.11.9 on an i686 machine (5537.79 BogoMips).
Notice : All mail here is now cached for review by Dictator Bush.
98.36% of all statistics are fiction.

2005-05-18 17:33:09

by Michael Tokarev

[permalink] [raw]
Subject: Re: Detecting link up

Martin Zwickel wrote:
> On Wed, 18 May 2005 11:35:12 +0100
> Filipe Abrantes <[email protected]> bubbled:
>
>>Hi all,
>>
>>I need to detect when an interface (wired ethernet) has link up/down.
>>Is there a system signal which is sent when this happens? What is the
>>best way to this programatically?
>
> mii-tool?

BTW, it might be a good idea to trigger some hotplug event on
interface up/down... or just send a netlink message. instead of
polling the interface. The same applies to removable media too
(CD or ZIP drive).

/mjt

2005-05-18 18:21:27

by Stephen Hemminger

[permalink] [raw]
Subject: Re: Detecting link up

Filipe Abrantes wrote:
> Hi all,
>
> I need to detect when an interface (wired ethernet) has link up/down. Is
> there a system signal which is sent when this happens? What is the best
> way to this programatically?
>
> Best Regards
>
> Filipe
>
>

The best way is to open a netlink socket and look for the mesaages about
link up/down there. Read iproute2 http://developer.osdl.org/dev/iproute2
source for ip command (ipmonitor.c).

This works for almost all devices unlike ethtool and mii which only
work on a small subset of devices.

2005-05-18 22:26:07

by Baruch Even

[permalink] [raw]
Subject: Re: Detecting link up

Stephen Hemminger wrote:
> Filipe Abrantes wrote:
>
>> Hi all,
>>
>> I need to detect when an interface (wired ethernet) has link up/down.
>> Is there a system signal which is sent when this happens? What is the
>> best way to this programatically?
>>
>> Best Regards
>>
>> Filipe
>>
>>
>
> The best way is to open a netlink socket and look for the mesaages about
> link up/down there. Read iproute2 http://developer.osdl.org/dev/iproute2
> source for ip command (ipmonitor.c).
>
> This works for almost all devices unlike ethtool and mii which only
> work on a small subset of devices.

And libnl is a very good library to get just that information without
the need to manually parse netlink messages.

Baruch