public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Detecting link up
@ 2005-05-18 10:35 Filipe Abrantes
  2005-05-18 11:40 ` Martin Zwickel
  2005-05-18 18:20 ` Stephen Hemminger
  0 siblings, 2 replies; 11+ messages in thread
From: Filipe Abrantes @ 2005-05-18 10:35 UTC (permalink / raw)
  To: linux-kernel

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: fla@inescporto.pt

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Detecting link up
  2005-05-18 10:35 Detecting link up Filipe Abrantes
@ 2005-05-18 11:40 ` Martin Zwickel
  2005-05-18 13:11   ` Vaibhav Nivargi
                     ` (2 more replies)
  2005-05-18 18:20 ` Stephen Hemminger
  1 sibling, 3 replies; 11+ messages in thread
From: Martin Zwickel @ 2005-05-18 11:40 UTC (permalink / raw)
  To: Filipe Abrantes; +Cc: linux-kernel

[-- Attachment #1: Type: text/plain, Size: 458 bytes --]

On Wed, 18 May 2005 11:35:12 +0100
Filipe Abrantes <fla@inescporto.pt> 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 <martin.zwickel@technotrend.de>
Research & Development

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

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Detecting link up
  2005-05-18 11:40 ` Martin Zwickel
@ 2005-05-18 13:11   ` Vaibhav Nivargi
  2005-05-18 13:57     ` Richard B. Johnson
  2005-05-18 14:37   ` Max Kellermann
  2005-05-18 17:33   ` Michael Tokarev
  2 siblings, 1 reply; 11+ messages in thread
From: Vaibhav Nivargi @ 2005-05-18 13:11 UTC (permalink / raw)
  To: Martin Zwickel; +Cc: Filipe Abrantes, linux-kernel

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

regards,
Vaibhav

On 5/18/05, Martin Zwickel <martin.zwickel@technotrend.de> wrote:
> On Wed, 18 May 2005 11:35:12 +0100
> Filipe Abrantes <fla@inescporto.pt> 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 <martin.zwickel@technotrend.de>
> Research & Development
> 
> TechnoTrend AG <http://www.technotrend.de>
> 
> 
>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Detecting link up
  2005-05-18 13:11   ` Vaibhav Nivargi
@ 2005-05-18 13:57     ` Richard B. Johnson
  0 siblings, 0 replies; 11+ messages in thread
From: Richard B. Johnson @ 2005-05-18 13:57 UTC (permalink / raw)
  To: Vaibhav Nivargi; +Cc: Martin Zwickel, Filipe Abrantes, linux-kernel

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 <martin.zwickel@technotrend.de> wrote:
>> On Wed, 18 May 2005 11:35:12 +0100
>> Filipe Abrantes <fla@inescporto.pt> 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 <martin.zwickel@technotrend.de>
>> 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.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Detecting link up
  2005-05-18 11:40 ` Martin Zwickel
  2005-05-18 13:11   ` Vaibhav Nivargi
@ 2005-05-18 14:37   ` Max Kellermann
  2005-05-18 15:00     ` Oliver Neukum
                       ` (2 more replies)
  2005-05-18 17:33   ` Michael Tokarev
  2 siblings, 3 replies; 11+ messages in thread
From: Max Kellermann @ 2005-05-18 14:37 UTC (permalink / raw)
  To: linux-kernel

On 2005/05/18 13:40, Martin Zwickel <martin.zwickel@technotrend.de> wrote:
> On Wed, 18 May 2005 11:35:12 +0100
> Filipe Abrantes <fla@inescporto.pt> 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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Detecting link up
  2005-05-18 14:37   ` Max Kellermann
@ 2005-05-18 15:00     ` Oliver Neukum
  2005-05-18 15:06     ` Coywolf Qi Hunt
  2005-05-18 15:11     ` Richard B. Johnson
  2 siblings, 0 replies; 11+ messages in thread
From: Oliver Neukum @ 2005-05-18 15:00 UTC (permalink / raw)
  To: Max Kellermann; +Cc: linux-kernel

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Detecting link up
  2005-05-18 14:37   ` Max Kellermann
  2005-05-18 15:00     ` Oliver Neukum
@ 2005-05-18 15:06     ` Coywolf Qi Hunt
  2005-05-18 15:11     ` Richard B. Johnson
  2 siblings, 0 replies; 11+ messages in thread
From: Coywolf Qi Hunt @ 2005-05-18 15:06 UTC (permalink / raw)
  To: linux-kernel

On 5/18/05, Max Kellermann <max@duempel.org> wrote:
> On 2005/05/18 13:40, Martin Zwickel <martin.zwickel@technotrend.de> wrote:
> > On Wed, 18 May 2005 11:35:12 +0100
> > Filipe Abrantes <fla@inescporto.pt> 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Detecting link up
  2005-05-18 14:37   ` Max Kellermann
  2005-05-18 15:00     ` Oliver Neukum
  2005-05-18 15:06     ` Coywolf Qi Hunt
@ 2005-05-18 15:11     ` Richard B. Johnson
  2 siblings, 0 replies; 11+ messages in thread
From: Richard B. Johnson @ 2005-05-18 15:11 UTC (permalink / raw)
  To: Max Kellermann; +Cc: linux-kernel

On Wed, 18 May 2005, Max Kellermann wrote:

> On 2005/05/18 13:40, Martin Zwickel <martin.zwickel@technotrend.de> wrote:
>> On Wed, 18 May 2005 11:35:12 +0100
>> Filipe Abrantes <fla@inescporto.pt> 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.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Detecting link up
  2005-05-18 11:40 ` Martin Zwickel
  2005-05-18 13:11   ` Vaibhav Nivargi
  2005-05-18 14:37   ` Max Kellermann
@ 2005-05-18 17:33   ` Michael Tokarev
  2 siblings, 0 replies; 11+ messages in thread
From: Michael Tokarev @ 2005-05-18 17:33 UTC (permalink / raw)
  To: linux-kernel

Martin Zwickel wrote:
> On Wed, 18 May 2005 11:35:12 +0100
> Filipe Abrantes <fla@inescporto.pt> 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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Detecting link up
  2005-05-18 10:35 Detecting link up Filipe Abrantes
  2005-05-18 11:40 ` Martin Zwickel
@ 2005-05-18 18:20 ` Stephen Hemminger
  2005-05-18 22:20   ` Baruch Even
  1 sibling, 1 reply; 11+ messages in thread
From: Stephen Hemminger @ 2005-05-18 18:20 UTC (permalink / raw)
  To: Filipe Abrantes; +Cc: linux-kernel

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.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Detecting link up
  2005-05-18 18:20 ` Stephen Hemminger
@ 2005-05-18 22:20   ` Baruch Even
  0 siblings, 0 replies; 11+ messages in thread
From: Baruch Even @ 2005-05-18 22:20 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: Filipe Abrantes, linux-kernel

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2005-05-18 22:26 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-05-18 10:35 Detecting link up Filipe Abrantes
2005-05-18 11:40 ` Martin Zwickel
2005-05-18 13:11   ` Vaibhav Nivargi
2005-05-18 13:57     ` Richard B. Johnson
2005-05-18 14:37   ` Max Kellermann
2005-05-18 15:00     ` Oliver Neukum
2005-05-18 15:06     ` Coywolf Qi Hunt
2005-05-18 15:11     ` Richard B. Johnson
2005-05-18 17:33   ` Michael Tokarev
2005-05-18 18:20 ` Stephen Hemminger
2005-05-18 22:20   ` Baruch Even

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox