From 162e5ef9eb859a8709399fcc9be868d8b7b07ca3 Mon Sep 17 00:00:00 2001 From: Matthias Schoepfer Date: Tue, 6 Jul 2021 16:04:46 +0200 Subject: [PATCH 1/4] ethercatctl: allow to use the network device name instead of mac address In the /etc/ethercat.conf file, the MASTERX_DEVICE string is given as a mac address or a wildcard. When we want to deploy the (same) software (image) to a bunch of different hardware, and these computers do have more than one interface that matches the driver, we need a way to define the device by network interface name. This allows to also define the network device by name (e.g. eth0, eno1, ...) Signed-off-by: Matthias Schoepfer --- script/ethercatctl.in | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/script/ethercatctl.in b/script/ethercatctl.in index 62dae0bd..1664a31f 100755 --- a/script/ethercatctl.in +++ b/script/ethercatctl.in @@ -37,6 +37,7 @@ LSMOD=/sbin/lsmod MODPROBE=/sbin/modprobe RMMOD=/sbin/rmmod MODINFO=/sbin/modinfo +IP=/bin/ip ETHERCAT=@bindir@/ethercat @@ -55,13 +56,19 @@ fi #------------------------------------------------------------------------------ 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 + elif echo ${1} | grep -qE '^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$'; then MAC=${1} else - echo Invalid MAC address \""${1}"\" in ${ETHERCAT_CONFIG} - exit 1 + 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} + else + echo Invalid MAC address \""${1}"\" in ${ETHERCAT_CONFIG} + exit 1 + fi fi } From 72ae0c090c0d31279bbfee14442e699e5e3bb182 Mon Sep 17 00:00:00 2001 From: Florian Pose Date: Tue, 24 Aug 2021 12:36:29 +0200 Subject: [PATCH 2/4] Removed tabs. --- script/ethercatctl.in | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/script/ethercatctl.in b/script/ethercatctl.in index 1664a31f..337d0da0 100755 --- a/script/ethercatctl.in +++ b/script/ethercatctl.in @@ -4,9 +4,7 @@ # # Start script for EtherCAT to use with systemd # -# $Id$ -# -# Copyright (C) 2006-2012 Florian Pose, Ingenieurgemeinschaft IgH +# Copyright (C) 2006-2021 Florian Pose, Ingenieurgemeinschaft IgH # # This file is part of the IgH EtherCAT Master. # @@ -62,13 +60,13 @@ parse_mac_address() { elif echo ${1} | grep -qE '^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$'; 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} - 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} + else echo Invalid MAC address \""${1}"\" in ${ETHERCAT_CONFIG} exit 1 - fi + fi fi } From d06444d53e0e5d2bfbdf41c4b24369ad35947c40 Mon Sep 17 00:00:00 2001 From: Florian Pose Date: Tue, 24 Aug 2021 13:20:54 +0200 Subject: [PATCH 3/4] Broke long lines. --- script/ethercatctl.in | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/script/ethercatctl.in b/script/ethercatctl.in index 337d0da0..361b95d8 100755 --- a/script/ethercatctl.in +++ b/script/ethercatctl.in @@ -60,11 +60,14 @@ parse_mac_address() { elif echo ${1} | grep -qE '^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$'; 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 + 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} else - echo Invalid MAC address \""${1}"\" in ${ETHERCAT_CONFIG} + echo Invalid MAC address or interface name \""${1}"\" \ + in ${ETHERCAT_CONFIG} exit 1 fi fi From b47f9249d7cb6c337d6860bd30e5db03477a5f2b Mon Sep 17 00:00:00 2001 From: Florian Pose Date: Tue, 24 Aug 2021 13:27:28 +0200 Subject: [PATCH 4/4] Documentation for interface names. --- script/ethercat.conf | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/script/ethercat.conf b/script/ethercat.conf index 91ccaeb8..f6f0a549 100644 --- a/script/ethercat.conf +++ b/script/ethercat.conf @@ -17,6 +17,10 @@ # Specify the MAC address (hexadecimal with colons) of the Ethernet device to # use. Example: "00:00:08:44:ab:66" # +# Alternatively, a network interface name can be specified. The interface +# name will be resolved to a MAC address using the 'ip' command. +# Example: "eth0" +# # The broadcast address "ff:ff:ff:ff:ff:ff" has a special meaning: It tells # the master to accept the first device offered by any Ethernet driver. # @@ -24,6 +28,10 @@ # created: A non-empty variable MASTER0_DEVICE will create one master, adding a # non-empty variable MASTER1_DEVICE will create a second master, and so on. # +# Examples: +# MASTER0_DEVICE="00:00:08:44:ab:66" +# MASTER0_DEVICE="eth0" +# MASTER0_DEVICE="" #MASTER1_DEVICE=""