From bfd84c8f3e3c7cc0e129cad276e74eb9aea41c37 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 0125fb78..689be68e 100644 --- a/script/ethercat.conf +++ b/script/ethercat.conf @@ -61,10 +61,6 @@ MASTER0_DEVICE="" # Note: The e100, e1000, e1000e, r8169, ccat, igb and igc 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="" # If you have any issues about network interfaces not being configured @@ -72,6 +68,19 @@ DEVICE_MODULES="" # Have a look at the service file, you'll find some details there. # +# +# 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 8c938a05..763147ac 100755 --- a/script/ethercatctl.in +++ b/script/ethercatctl.in @@ -47,18 +47,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} @@ -72,6 +76,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="" @@ -167,6 +176,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 ;;