]> git.ipfire.org Git - people/stevee/network.git/blobdiff - src/functions/functions.device
network fix parameter passing when using ""
[people/stevee/network.git] / src / functions / functions.device
index cdd524816a319f8665a13b51c7674b4b45740a92..12bf203ffe9e4c44268c6bc20dfaf1b4e07b0847 100644 (file)
@@ -287,6 +287,12 @@ device_is_dummy() {
        [[ ${device} =~ ^dummy[0-9]+$ ]]
 }
 
+device_is_ipsec() {
+       local device="${1}"
+
+       [[ ${device} =~ ^ipsec\- ]]
+}
+
 # Check if the device is a wireless device
 device_is_wireless() {
        local device=${1}
@@ -294,6 +300,14 @@ device_is_wireless() {
        [ -d "${SYS_CLASS_NET}/${device}/phy80211" ]
 }
 
+device_is_vti() {
+       local device=${1}
+
+       local type=$(__device_get_file ${device} type)
+
+       [ "${type}" = "768" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
+}
+
 device_get_phy() {
        local device="${1}"
 
@@ -306,11 +320,11 @@ device_get_phy() {
 }
 
 device_is_phy() {
-       phy_exists $@
+       phy_exists "$@"
 }
 
 device_is_serial() {
-       serial_exists $@
+       serial_exists "$@"
 }
 
 # Returns true if a device is a tun device
@@ -399,6 +413,9 @@ device_get_type() {
        elif device_is_phy ${device}; then
                echo "phy"
 
+       elif device_is_vti ${device}; then
+               echo "vti"
+
        else
                echo "unknown"
        fi
@@ -523,7 +540,7 @@ device_set_promisc() {
 
 # Check if the device is free
 device_is_free() {
-       ! device_is_used $@
+       ! device_is_used "$@"
 }
 
 # Check if the device is used
@@ -563,6 +580,33 @@ device_set_name() {
        fi
 }
 
+device_set_master() {
+       local device="${1}"
+       assert isset device
+
+       local master="${2}"
+       assert isset master
+
+       if ! cmd ip link set "${device}" master "${master}"; then
+               log ERROR "Could not set master ${master} for device ${device}"
+               return ${EXIT_ERROR}
+       fi
+
+       return ${EXIT_OK}
+}
+
+device_remove_master() {
+       local device="${1}"
+       assert isset device
+
+       if ! cmd ip link set "${device}" nomaster; then
+               log ERROR "Could not remove master for device ${device}"
+               return ${EXIT_ERROR}
+       fi
+
+       return ${EXIT_OK}
+}
+
 # Set device up
 device_set_up() {
        assert [ $# -eq 1 ]
@@ -731,7 +775,7 @@ device_identify() {
                                seconds="$(cli_get_val "${arg}")"
                                ;;
                esac
-       done <<< "$(args $@)"
+       done <<< "$(args "$@")"
 
        assert isinteger seconds
 
@@ -775,7 +819,7 @@ device_has_ip() {
                        ;;
        esac
 
-       listmatch ${addr} $(device_get_addresses ${device})
+       list_match ${addr} $(device_get_addresses ${device})
 }
 
 device_get_addresses() {
@@ -796,13 +840,7 @@ __device_get_file() {
        local device=${1}
        local file=${2}
 
-       assert isset device
-       assert isset file
-
-       local path="${SYS_CLASS_NET}/${device}/${file}"
-       [ -r "${path}" ] || return ${EXIT_ERROR}
-
-       echo "$(<${path})"
+       fread "${SYS_CLASS_NET}/${device}/${file}"
 }
 
 __device_set_file() {
@@ -812,13 +850,7 @@ __device_set_file() {
        local file="${2}"
        local value="${3}"
 
-       local path="${SYS_CLASS_NET}/${device}/${file}"
-       if [ ! -w "${path}" ]; then
-               log DEBUG "Cannot write to file '${file}' (${value})"
-               return ${EXIT_ERROR}
-       fi
-
-       echo "${value}" > "${path}"
+       fappend "${SYS_CLASS_NET}/${device}/${file}" "${value}"
 }
 
 device_get_rx_bytes() {
@@ -860,13 +892,30 @@ device_get_tx_errors() {
 device_get_speed() {
        local device=${1}
 
-       __device_get_file ${device} speed
+       local speed=$(__device_get_file ${device} speed)
+
+       # Exit for no output (i.e. no link detected)
+       isset speed || return ${EXIT_ERROR}
+
+       # Don't return anything for negative values
+       [ ${speed} -lt 0 ] && return ${EXIT_ERROR}
+
+       print "${speed}"
 }
 
 device_get_duplex() {
        local device=${1}
 
-       __device_get_file ${device} duplex
+       local duplex=$(__device_get_file ${device} duplex)
+
+       case "${duplex}" in
+               unknown)
+                       return ${EXIT_ERROR}
+                       ;;
+               *)
+                       print "${duplex}"
+                       ;;
+       esac
 }
 
 device_get_link_string() {
@@ -893,7 +942,7 @@ device_auto_configure_smp_affinity() {
 
        local device=${1}
 
-       if lock_acquire "smp-affinity"; then
+       if lock_acquire "smp-affinity" 60; then
                device_set_smp_affinity ${device} auto
 
                lock_release "smp-affinity"
@@ -969,10 +1018,71 @@ device_get_queues() {
 
        local queue
        for queue in ${SYS_CLASS_NET}/${device}/queues/*; do
+               [ -d "${queue}" ] || continue
+
                basename "${queue}"
        done
 }
 
+device_supports_multiqueue() {
+       local device=${1}
+
+       local num_queues=$(device_num_queues ${device})
+
+       if isset num_queues && [ ${num_queues} -gt 2 ]; then
+               return ${EXIT_TRUE}
+       fi
+
+       return ${EXIT_FALSE}
+}
+
+device_num_queues() {
+       local device=${1}
+       local type=${2}
+
+       isset type && assert isoneof type rx tx
+
+       local i=0
+
+       local q
+       for q in $(device_get_queues ${device}); do
+               case "${type},${q}" in
+                       rx,rx-*)
+                               (( i++ ))
+                               ;;
+                       tx,tx-*)
+                               (( i++ ))
+                               ;;
+                       *,*)
+                               (( i++ ))
+                               ;;
+               esac
+       done
+
+       print ${i}
+}
+
+device_queue_get_smp_affinity() {
+       assert [ $# -eq 2 ]
+
+       local device=${1}
+       local queue=${2}
+
+       local path="${SYS_CLASS_NET}/${device}/queues/${queue}"
+
+       case "${queue}" in
+               rx-*)
+                       path="${path}/rps_cpus"
+                       ;;
+               tx-*)
+                       path="${path}/xps_cpus"
+                       ;;
+       esac
+       assert [ -r "${path}" ]
+
+       __bitmap_to_processor_ids $(<${path})
+}
+
 device_queue_set_smp_affinity() {
        assert [ $# -eq 3 ]