]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/functions/functions.device
device: Refactor check for device type
[people/ms/network.git] / src / functions / functions.device
index 7e830ed2d84cbc22b5b62fdb5f27891f6c759eeb..d1f60c436572fee5c48ea825967959631efb3f3c 100644 (file)
 #                                                                             #
 ###############################################################################
 
-device_list() {
-       local devices
+declare -A DEVICE_LINK_SPEEDS=(
+       [10BaseT-Half]=0x1
+       [10BaseT-Full]=0x2
+       [100BaseT-Half]=0x4
+       [100BaseT-Full]=0x8
+       [1000BaseT-Half]=0x10
+       [1000BaseT-Full]=0x20
+       [10000BaseT-Full]=0x1000
+)
 
+device_list() {
        # Add all interfaces
        local device
        for device in $(list_directory ${SYS_CLASS_NET}); do
-               list_append_one devices "${device}"
+               if device_exists "${device}"; then
+                       print "${device}"
+               fi
        done
 
-       # Add all PHYs
-       list_append devices $(phy_list)
+       # List all PHYs
+       phy_list
 
-       # Add all serial devices
-       list_append devices $(serial_list)
-
-       # Return a sorted result
-       list_sort ${devices}
+       # List all serial devices
+       serial_list
 }
 
 # Check if the device exists
@@ -143,6 +150,13 @@ device_get_ifindex() {
        print "$(<${path})"
 }
 
+device_get_driver() {
+       local device="${1}"
+       assert isset device
+
+       get_driver_from_path "${SYS_CLASS_NET}/${device}/device/driver/module"
+}
+
 # Check if the device is a bonding device
 device_is_bonding() {
        [ -d "/sys/class/net/${1}/bonding" ]
@@ -234,13 +248,25 @@ device_get_vlans() {
        done < ${PROC_NET_VLAN_CONFIG}
 }
 
+__device_type_matches() {
+       local device="${1}"
+       local type="${2}"
+
+       local _type="$(__device_get_file "${device}" "type")"
+
+       if [ "${type}" = "${_type}" ]; then
+               return ${EXIT_TRUE}
+       fi
+
+       return ${EXIT_FALSE}
+}
+
 # Check if the device is a ppp device
 device_is_ppp() {
-       local device=${1}
-
-       local type=$(__device_get_file ${device} type)
+       local device="${1}"
+       assert isset device
 
-       [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
+       __device_type_matches "${device}" 512
 }
 
 # Check if the device is a pointopoint device.
@@ -279,19 +305,17 @@ device_is_wireless() {
 }
 
 device_is_vti() {
-       local device=${1}
-
-       local type=$(__device_get_file ${device} type)
+       local device="${1}"
+       assert isset device
 
-       [ "${type}" = "768" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
+       __device_type_matches "${device}" 768
 }
 
 device_is_vti6() {
-       local device=${1}
-
-       local type=$(__device_get_file ${device} type)
+       local device="${1}"
+       assert isset device
 
-       [ "${type}" = "769" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
+       __device_type_matches "${device}" 769
 }
 
 device_get_phy() {
@@ -881,6 +905,41 @@ device_get_tx_errors() {
        __device_get_file ${device} statistics/tx_errors
 }
 
+device_advertise_link_speeds() {
+       local device="${1}"
+       shift
+
+       assert isset device
+
+       # Advertised modes in hex
+       local advertise=0
+
+       local mode
+       for mode in $@; do
+               local m="${DEVICE_LINK_SPEEDS[${mode}]}"
+               if isset m; then
+                       advertise="$(( advertise | m ))"
+               fi
+       done
+
+       # If nothing was selected, we reset and enable everything
+       if [ ${advertise} -eq 0 ]; then
+               advertise=0xffffff
+       fi
+
+       # Enable auto-negotiation
+       cmd_quiet ethtool --change "${device}" autoneg on
+
+       # Set advertised link speeds
+       if ! cmd_quiet ethtool --change "${device}" advertise "0x$(hex "${advertise}")"; then
+               log ERROR "Could not set link modes of ${device}: $@"
+               return ${EXIT_ERROR}
+       fi
+
+       log DEBUG "Set device link modes of ${device} to $@"
+       return ${EXIT_ERROR}
+}
+
 device_get_speed() {
        local device=${1}
 
@@ -895,23 +954,6 @@ 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}