Skip to contentSkip to conversation

Detecting link up

DISCUSSION Filipe Abrantes 10 participants 11 messages May 18, 2005

lore
Share & tools
lore
#0 lore raw

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

Signature · 9 lines
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]
#1 lore raw

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

Quoted 5 lines

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?

Signature · 7 lines
MyExcuse:
Not enough interrupts

Martin Zwickel <[email protected]>
Research & Development

TechnoTrend AG <http://www.technotrend.de>
#2 lore raw

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

regards,
Vaibhav

Quoted 22 lines · Martin Zwickel

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

Quoted 5 lines

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>

#3 lore raw
Quoted 26 lines · Vaibhav Nivargi

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

regards,
Vaibhav

Quoted 20 lines · Martin Zwickel

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

Quoted 5 lines

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.

#4 lore raw
In reply to Martin Zwickel
Quoted 7 lines · Martin Zwickel

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

Quoted 3 lines

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

#5 lore raw

Am Mittwoch, 18. Mai 2005 16:37 schrieb Max Kellermann:

Quoted 5 lines

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

#6 lore raw
In reply to Max Kellermann
Quoted 9 lines · Max Kellermann
Quoted 7 lines · Martin Zwickel

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

Quoted 3 lines

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?

Quoted 8 lines

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.

Signature · 2 lines
Coywolf Qi Hunt
http://sosdg.org/~coywolf/
#7 lore raw
In reply to Max Kellermann
Quoted 11 lines · Max Kellermann
Quoted 7 lines · Martin Zwickel

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

Quoted 3 lines

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:

Quoted 8 lines

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.

#8 lore raw
In reply to Martin Zwickel
Quoted 10 lines · Martin Zwickel

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

Quoted 5 lines

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

#9 lore raw
In reply to Filipe Abrantes
Quoted 9 lines · Filipe Abrantes

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.

#10 lore raw
Quoted 14 lines · Stephen Hemminger
Quoted 11 lines · Filipe Abrantes

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

Quoted 6 lines

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