From: Michael Tremer Date: Thu, 20 Sep 2018 22:26:45 +0000 (+0100) Subject: port: ethernet: Allow setting duplex mode X-Git-Tag: 010~51 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=178aabc0704445372f219a9163ef68d1fd541b65;p=network.git port: ethernet: Allow setting duplex mode Signed-off-by: Michael Tremer --- diff --git a/src/functions/functions.device b/src/functions/functions.device index 7e830ed2..412099cd 100644 --- a/src/functions/functions.device +++ b/src/functions/functions.device @@ -927,6 +927,22 @@ device_get_duplex() { esac } +device_set_duplex() { + local device="${1}" + assert isset device + + local duplex="${2}" + assert isset duplex + + if ! cmd_quiet ethtool --change "${device}" duplex "${duplex}"; then + log ERROR "Could not set ${device} to ${duplex}-duplex mode" + return ${EXIT_ERROR} + fi + + log DEBUG "Set ${device} to ${duplex}-duplex mode" + return ${EXIT_OK} +} + device_get_link_string() { local device="${1}" assert isset device diff --git a/src/hooks/ports/ethernet b/src/hooks/ports/ethernet index beec3ccd..2db7f488 100644 --- a/src/hooks/ports/ethernet +++ b/src/hooks/ports/ethernet @@ -27,10 +27,13 @@ DEFAULT_MTU=1500 # Selectable speeds in MBit/s VALID_SPEEDS="10000 1000 100 10" +# We can run in full or half duplex +VALID_DUPLEXES="full half" + # DEVICE equals the actual MAC address of the device. # If ADDRESS is set, the device will get ADDRESS set for its MAC address. -HOOK_SETTINGS="HOOK ADDRESS DEVICE MTU SPEED" +HOOK_SETTINGS="HOOK ADDRESS DEVICE DUPLEX MTU SPEED" hook_check_settings() { assert ismac DEVICE @@ -43,6 +46,10 @@ hook_check_settings() { assert mtu_is_valid "ethernet" "${MTU}" fi + if isset DUPLEX; then + assert isoneof DUPLEX ${VALID_DUPLEXES} + fi + if isset SPEED; then assert isoneof SPEED ${VALID_SPEEDS} fi @@ -60,6 +67,15 @@ hook_parse_cmdline() { fi ;; + --duplex=*) + DUPLEX="$(cli_get_val "${1}")" + + if ! isoneof DUPLEX ${VALID_DUPLEXES}; then + error "Invalid duplex mode: ${DUPLEX}" + return ${EXIT_ERROR} + fi + ;; + --mtu=*) MTU="$(cli_get_val "${1}")" @@ -112,6 +128,11 @@ hook_up() { device_set_mtu "${port}" "${DEFAULT_MTU}" fi + # Set duplex mode + if isset DUPLEX; then + device_set_duplex "${port}" "${DUPLEX}" + fi + # Set speed if isset SPEED; then device_set_speed "${port}" "${SPEED}"