Stefan Jaritz Stefan Jaritz - 12 days ago 6
Linux Question

How to set up a udev rule for ETH "link down"/"link up"?

I like to switch on a green LED(connected through GPIOs), when eth0 is connected. When disconnected I like to switch the green LED of and a red one on.

Thought that udev is maybe the right place for it. I created the simple demo rule:

KERNEL=="eth0", SUBSYSTEM=="net", ACTION=="add", RUN+="/sbin/set_BSPleds eth0 on"


This rule should call a script when the eth0 is added. It was never executed.
After I was looking to the udev monitor by entering "udevadm monitor -k -u" at the shell. There were no events coming when I unplug/plug the lan cable.

root@sama5d3xek:/etc/udev/rules.d# udevadm monitor -k -uh0
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent

root@sama5d3xek:/etc/udev/rules.d#


Seems that there are no uevents for eth0. The ethernet driver is provided my ATMEL. I am building a custom Linux by the help of Yocto.

My question is, how to get the "link down"/"link up" events to udev?
If it does not works with udev, what alternative way is the best?

Answer

Ethernet devices are devices, but connections are not.

You could trace connection through /dev/xconsole, dmesg or /var/log/kern.log.

Using rsyslog, you could (as root):

echo ':msg, contains, "Link is" |/run/netlink' >/etc/rsyslog.d/netlinkstate.conf
mkfifo /run/netlink
chgrp user /run/netlink
chmod 640 /run/netlink
service rsyslog force-reload

Then, logged as user, simply:

read line < /run/netlink

will wait for input from fifo, then hold until something happen.

state=${line#*Link is } eventtime=${line%% $HOSTNAME*}
echo $eventtime $state
2016-11-21T17:40:50.956181+01:00 Down

or

echo $eventtime $state
2016-11-21T17:40:50.956181+01:00 Up 100 Mbps Full Duplex, Flow Control: Rx/Tx

echo $eventtime ${state%% *}
2016-11-21T17:40:50.956181+01:00 Up

Under , you could use timeout for emptying fifo and read only last entry:

while read -t .01 entry </run/netlink;do line="$entry";done
state=${line#*Link is }
eventtime=${line%% $HOSTNAME*}
shortstate=${state%% *}
Comments