From: Michael Tremer Date: Thu, 20 Sep 2018 22:19:02 +0000 (+0100) Subject: port: ethernet: Allow setting link speed X-Git-Tag: 010~52 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3a0e1ddab115f021f02f84322ffa577fe1423ae1;p=network.git port: ethernet: Allow setting link speed Signed-off-by: Michael Tremer --- diff --git a/src/functions/functions.device b/src/functions/functions.device index ace40220..7e830ed2 100644 --- a/src/functions/functions.device +++ b/src/functions/functions.device @@ -895,6 +895,23 @@ device_get_speed() { print "${speed}" } +device_set_speed() { + local device="${1}" + assert isset device + + local speed="${2}" + assert isinteger speed + + if ! cmd_quiet ethtool --change "${device}" speed "${speed}"; then + log ERROR "Could not set speed of ${device} to ${speed} MBit/s" + return ${EXIT_ERROR} + fi + + log DEBUG "Set speed of ${device} to ${speed} MBit/s" + + return ${EXIT_OK} +} + device_get_duplex() { local device=${1} diff --git a/src/hooks/ports/ethernet b/src/hooks/ports/ethernet index 4c8eee83..beec3ccd 100644 --- a/src/hooks/ports/ethernet +++ b/src/hooks/ports/ethernet @@ -24,10 +24,13 @@ # Default MTU DEFAULT_MTU=1500 +# Selectable speeds in MBit/s +VALID_SPEEDS="10000 1000 100 10" + # 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" +HOOK_SETTINGS="HOOK ADDRESS DEVICE MTU SPEED" hook_check_settings() { assert ismac DEVICE @@ -39,6 +42,10 @@ hook_check_settings() { if isset MTU; then assert mtu_is_valid "ethernet" "${MTU}" fi + + if isset SPEED; then + assert isoneof SPEED ${VALID_SPEEDS} + fi } hook_parse_cmdline() { @@ -61,6 +68,16 @@ hook_parse_cmdline() { return ${EXIT_ERROR} fi ;; + + --speed=*) + SPEED="$(cli_get_val "${1}")" + + if ! isoneof SPEED ${VALID_SPEEDS}; then + error "Invalid speed: ${SPEED}" + return ${EXIT_ERROR} + fi + ;; + *) error "Unknown argument: ${1}" return ${EXIT_ERROR} @@ -95,6 +112,11 @@ hook_up() { device_set_mtu "${port}" "${DEFAULT_MTU}" fi + # Set speed + if isset SPEED; then + device_set_speed "${port}" "${SPEED}" + fi + # Bring up the device device_set_up "${port}"