From 0ae450d7c7a0e83a44b2df83af33e8e4f805f26e Mon Sep 17 00:00:00 2001 From: Nicola Fontana Date: Mon, 13 Dec 2021 09:56:32 +0000 Subject: [PATCH] Add UPDOWN_INTERFACES option to ethercat.conf --- script/ethercat.conf | 17 +++++++++++++---- script/ethercatctl.in | 28 +++++++++++++++++++++------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/script/ethercat.conf b/script/ethercat.conf index f6f0a549..efff1249 100644 --- a/script/ethercat.conf +++ b/script/ethercat.conf @@ -61,12 +61,21 @@ MASTER0_DEVICE="" # Note: The e100, e1000, e1000e, r8169, ccat and igb drivers are not built by # default. Enable them with the --enable- configure switches. # -# Attention: When using the generic driver, the corresponding Ethernet device -# has to be activated (with OS methods, for example 'ip link set ethX up'), -# before the master is started, otherwise all frames will time out. -# DEVICE_MODULES="" +# +# List of interfaces to bring up and down automatically. +# +# Specify a space-separated list of interface names (such as eth0 or +# enp0s1) that shall be brought up on `ethercatctl start` and down on +# `ethercatctl stop`. +# +# When using the generic driver, the corresponding Ethernet device has to be +# activated before the master is started, otherwise all frames will time out. +# This the perfect use-case for `UPDOWN_INTERFACES`. +# +UPDOWN_INTERFACES="" + # # Flags for loading kernel modules. # diff --git a/script/ethercatctl.in b/script/ethercatctl.in index 2da6e95b..074da23c 100755 --- a/script/ethercatctl.in +++ b/script/ethercatctl.in @@ -53,18 +53,22 @@ fi #------------------------------------------------------------------------------ +is_mac_address() { + local x='[0-9a-fA-F]' + echo "$1" | grep -qE "^($x$x:){5}$x$x\$" - +} + +#------------------------------------------------------------------------------ + parse_mac_address() { local DEVICENAMETOMAC - if [ -z "${1}" ]; then - MAC="" - elif echo ${1} | grep -qE '^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$'; then - MAC=${1} + if [ -z "${1}" ] || is_mac_address "${1}"; then + MAC="${1}" else DEVICENAMETOMAC=$("${IP}" address show dev "${1}" | awk '/link\/ether/ { print $2; }') - if echo "${DEVICENAMETOMAC}" | - grep -qE '^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$'; then - MAC=${DEVICENAMETOMAC} + if is_mac_address "${DEVICENAMETOMAC}"; then + MAC="${DEVICENAMETOMAC}" else echo Invalid MAC address or interface name \""${1}"\" \ in ${ETHERCAT_CONFIG} @@ -78,6 +82,11 @@ parse_mac_address() { case "${1}" in start) + # bring up all updown interfaces before anything else + for interface in $UPDOWN_INTERFACES; do + $IP link set dev $interface up + done + # construct DEVICES and BACKUPS from configuration variables DEVICES="" BACKUPS="" @@ -165,6 +174,11 @@ stop) ${MODPROBE} ${MODPROBE_FLAGS} "${MODULE}" done + # bring down all updown interfaces + for interface in $UPDOWN_INTERFACES; do + $IP link set dev $interface down + done + exit 0 ;;