]> git.ipfire.org Git - network.git/blobdiff - src/functions/functions.device
device: add new function device_tunnel_get_type()
[network.git] / src / functions / functions.device
index a851b669d50e864f358549e3620ce8bd108ce59c..0cd6e4ea0cf3cc74f78077c1e118d3fc9206c7ad 100644 (file)
@@ -23,7 +23,10 @@ device_list() {
        local devices
 
        # Add all interfaces
-       list_append devices $(devices_get_all)
+       local device
+       for device in $(list_directory ${SYS_CLASS_NET}); do
+               list_append_one devices "${device}"
+       done
 
        # Add all PHYs
        list_append devices $(phy_list)
@@ -74,6 +77,9 @@ device_delete() {
        # Nothing to do, it device does not exist.
        device_exists ${device} || return ${EXIT_OK}
 
+       # Shut down device before we delete it
+       device_set_down "${device}"
+
        # Delete the device.
        cmd_quiet ip link delete ${device}
        local ret=$?
@@ -113,10 +119,7 @@ device_ifindex_to_name() {
        assert isset idx
 
        local device device_idx
-       for device in ${SYS_CLASS_NET}/*; do
-               device=$(basename ${device})
-               device_exists ${device} || continue
-
+       for device in $(list_directory "${SYS_CLASS_NET}"); do
                device_idx=$(device_get_ifindex ${device})
 
                if [ "${device_idx}" = "${idx}" ]; then
@@ -140,31 +143,6 @@ device_get_ifindex() {
        print "$(<${path})"
 }
 
-# Check if the device is a batman-adv bridge
-device_is_batman_adv() {
-       [ -d "${SYS_CLASS_NET}/${1}/mesh" ]
-}
-
-# Check if the device is a batman-adv slave port
-device_is_batman_adv_slave() {
-       local device="${1}"
-
-       if [ -d "${SYS_CLASS_NET}/${device}/batman_adv" ]; then
-               local status="$(<${SYS_CLASS_NET}/${device}/batman_adv/iface_status)"
-
-               case "${status}" in
-                       "active")
-                               return ${EXIT_TRUE}
-                               ;;
-                       *)
-                               return ${EXIT_FALSE}
-                               ;;
-               esac
-       fi
-
-       return ${EXIT_FALSE}
-}
-
 # Check if the device is a bonding device
 device_is_bonding() {
        [ -d "/sys/class/net/${1}/bonding" ]
@@ -287,6 +265,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 +278,22 @@ 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_is_vti6() {
+       local device=${1}
+
+       local type=$(__device_get_file ${device} type)
+
+       [ "${type}" = "769" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
+}
+
 device_get_phy() {
        local device="${1}"
 
@@ -306,11 +306,18 @@ 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
+device_is_tun() {
+       local device="${1}"
+
+       [ -e "${SYS_CLASS_NET}/${device}/tun_flags" ]
 }
 
 # Check if the device is a physical network interface
@@ -338,6 +345,9 @@ device_is_ethernet() {
        device_is_dummy ${device} && \
                return ${EXIT_ERROR}
 
+       device_is_tun ${device} && \
+               return ${EXIT_ERROR}
+
        return ${EXIT_OK}
 }
 
@@ -362,9 +372,6 @@ device_get_type() {
        elif device_is_ppp ${device}; then
                echo "ppp"
 
-       elif device_is_batman_adv ${device}; then
-               echo "batman-adv"
-
        elif device_is_loopback ${device}; then
                echo "loopback"
 
@@ -377,6 +384,9 @@ device_get_type() {
        elif device_is_dummy ${device}; then
                echo "dummy"
 
+       elif device_is_tun ${device}; then
+               echo "tun"
+
        elif device_is_ethernet ${device}; then
                echo "ethernet"
 
@@ -386,6 +396,27 @@ device_get_type() {
        elif device_is_phy ${device}; then
                echo "phy"
 
+       else
+               echo "$(device_tunnel_get_type "${device}")"
+       fi
+}
+
+# This function just checks the types a ip-tunnel device usually have
+# so when we know that the device is an ip-tunnel device we save time
+device_tunnel_get_type() {
+       local device=${1}
+
+       # If the device does not exist (happens on udev remove events),
+       # we do not bother to run all checks.
+       if ! device_exists "${device}"; then
+               echo "unknown"
+
+       elif device_is_vti ${device}; then
+               echo "vti"
+
+       elif device_is_vti6 ${device}; then
+               echo "vti6"
+
        else
                echo "unknown"
        fi
@@ -463,25 +494,16 @@ device_set_address() {
 
 device_get() {
        local device
-       local devices
-
-       for device in ${SYS_CLASS_NET}/*; do
-               device=$(basename ${device})
-
+       for device in $(list_directory "${SYS_CLASS_NET}"); do
                # bonding_masters is no device
                [ "${device}" = "bonding_masters" ] && continue
 
-               devices="${devices} ${device}"
+               echo "${device}"
        done
 
-       echo ${devices}
        return ${EXIT_OK}
 }
 
-devices_get_all() {
-       device_get
-}
-
 # Check if a device has a cable plugged in
 device_has_carrier() {
        local device=${1}
@@ -510,7 +532,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
@@ -550,21 +572,55 @@ 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() {
-       local device=${1}
+       assert [ $# -eq 1 ]
 
-       # Silently fail if device was not found
-       [ -z "${device}" ] && return ${EXIT_ERROR}
+       local device=${1}
 
        # Do nothing if device is already up
        device_is_up ${device} && return ${EXIT_OK}
 
+       log INFO "Bringing up ${device}"
+
        device_set_parent_up ${device}
+       if ! cmd ip link set ${device} up; then
+               return ${EXIT_ERROR}
+       fi
 
-       log DEBUG "Setting up device '${device}'"
+       # Set SMP affinity
+       if interrupt_use_smp_affinity; then
+               device_auto_configure_smp_affinity ${device}
+       fi
 
-       ip link set ${device} up
+       return ${EXIT_OK}
 }
 
 device_set_parent_up() {
@@ -587,15 +643,15 @@ device_set_parent_up() {
 
 # Set device down
 device_set_down() {
-       local device=${1}
-       assert isset device
+       assert [ $# -eq 1 ]
 
+       local device=${1}
        local ret=${EXIT_OK}
 
        if device_is_up ${device}; then
-               log DEBUG "Tearing down device '${device}'"
+               log INFO "Bringing down ${device}"
 
-               ip link set ${device} down
+               cmd ip link set ${device} down
                ret=$?
        fi
 
@@ -626,10 +682,8 @@ device_set_parent_down() {
 device_get_mtu() {
        local device=${1}
 
-       if ! device_exists ${device}; then
-               error "Device '${device}' does not exist."
-               return ${EXIT_ERROR}
-       fi
+       # Return an error if the device does not exist
+       device_exists ${device} || return ${EXIT_ERROR}
 
        echo $(<${SYS_CLASS_NET}/${device}/mtu)
 }
@@ -639,19 +693,17 @@ device_set_mtu() {
        local device=${1}
        local mtu=${2}
 
-       if ! device_exists ${device}; then
-               error "Device '${device}' does not exist."
-               return ${EXIT_ERROR}
-       fi
-       
-       local oldmtu=$(device_get_mtu ${device})
+       assert device_exists ${device}
 
-       if [ "${oldmtu}" = "${mtu}" ]; then
-               # No need to set mtu.
-               return ${EXIT_OK}
+       # Handle bridges differently
+       if device_is_bridge ${device}; then
+               local port
+               for port in $(bridge_get_members ${device}); do
+                       device_set_mtu ${port} ${mtu}
+               done
        fi
 
-       log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}."
+       log INFO "Setting MTU of ${device} to ${mtu}"
 
        local up
        if device_is_up ${device}; then
@@ -659,15 +711,15 @@ device_set_mtu() {
                up=1
        fi
 
-       ip link set ${device} mtu ${mtu}
-       local ret=$?
+       local ret=${EXIT_OK}
+       if ! cmd ip link set ${device} mtu ${mtu}; then
+               ret=${EXIT_ERROR}
 
-       if [ "${up}" = "1" ]; then
-               device_set_up ${device}
+               log ERROR "Could not set MTU ${mtu} on ${device}"
        fi
 
-       if [ "${ret}" != "0" ]; then
-               error_log "Could not set mtu '${mtu}' on device '${device}'."
+       if [ "${up}" = "1" ]; then
+               device_set_up ${device}
        fi
 
        return ${ret}
@@ -694,6 +746,56 @@ device_discover() {
        done
 }
 
+device_identify() {
+       assert [ $# -ge 1 ]
+
+       local device="${1}"
+
+       # Flash for ten seconds by default
+       local seconds="10"
+
+       # Run in background?
+       local background="false"
+
+       local arg
+       while read arg; do
+               case "${arg}" in
+                       --background)
+                               background="true"
+                               ;;
+                       --seconds=*)
+                               seconds="$(cli_get_val "${arg}")"
+                               ;;
+               esac
+       done <<< "$(args "$@")"
+
+       assert isinteger seconds
+
+       if ! device_exists "${device}"; then
+               log ERROR "Cannot identify device ${device}: Does not exist"
+               return ${EXIT_ERROR}
+       fi
+
+       if ! device_is_ethernet "${device}"; then
+               log DEBUG "Cannot identify device ${device}: Not an ethernet device"
+               return ${EXIT_NOT_SUPPORTED}
+       fi
+
+       log DEBUG "Identifying device ${device}"
+
+       local command="ethtool --identify ${device} ${seconds}"
+       local ret=0
+
+       if enabled background; then
+               cmd_background "${command}"
+       else
+               cmd_quiet "${command}"
+               ret=$?
+       fi
+
+       return ${ret}
+}
+
 device_has_ip() {
        local device=${1}
        local addr=${2}
@@ -709,7 +811,7 @@ device_has_ip() {
                        ;;
        esac
 
-       listmatch ${addr} $(device_get_addresses ${device})
+       list_match ${addr} $(device_get_addresses ${device})
 }
 
 device_get_addresses() {
@@ -730,13 +832,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() {
@@ -746,13 +842,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() {
@@ -794,13 +884,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() {
@@ -821,3 +928,186 @@ device_get_link_string() {
 
        print "${s}"
 }
+
+device_auto_configure_smp_affinity() {
+       assert [ $# -eq 1 ]
+
+       local device=${1}
+
+       if lock_acquire "smp-affinity" 60; then
+               device_set_smp_affinity ${device} auto
+
+               lock_release "smp-affinity"
+       fi
+}
+
+device_set_smp_affinity() {
+       assert [ $# -eq 2 ]
+
+       local device=${1}
+       local mode=${2}
+
+       # mode can be auto which will automatically try to find
+       # the least busy processor, or an integer for the desired
+       # processor that should handle this device
+
+       local num_processors=$(system_get_processors)
+
+       if [ "${mode}" = "auto" ]; then
+               local processor=$(interrupt_choose_least_busy_processor)
+       else
+               assert isinteger mode
+               local processor=${mode}
+
+               if [ ${processor} -gt ${num_processors} ]; then
+                       log ERROR "Processor ${processor} does not exist"
+                       return ${EXIT_ERROR}
+               fi
+       fi
+
+       local interrupts=$(interrupts_for_device ${device})
+       if ! isset interrupts; then
+               log DEBUG "${device} has no interrupts. Not changing SMP affinity"
+               return ${EXIT_OK}
+       fi
+
+       # Set SMP affinity
+       local interrupt
+       for interrupt in ${interrupts}; do
+               interrupt_set_smp_affinity ${interrupt} ${processor}
+       done
+
+       # Find all queues and assign them to the next processor
+       local queue
+       for queue in $(device_get_queues ${device}); do
+               case "${queue}" in
+                       # Only handle receive queues
+                       rx-*)
+                               for interrupt in $(interrupts_for_device_queue ${device} ${queue}); do
+                                       interrupt_set_smp_affinity ${interrupt} ${processor}
+                               done
+
+                               device_queue_set_smp_affinity ${device} ${queue} ${processor}
+                               ;;
+
+                       # Ignore the rest
+                       *)
+                               continue
+                               ;;
+               esac
+
+               # Get the next available processor if in auto mode
+               [ "${mode}" = "auto" ] && processor=$(system_get_next_processor ${processor})
+       done
+
+       return ${EXIT_OK}
+}
+
+device_get_queues() {
+       assert [ $# -eq 1 ]
+
+       local device=${1}
+
+       list_directory "${SYS_CLASS_NET}/${device}/queues"
+}
+
+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 ]
+
+       local device=${1}
+       local queue=${2}
+       local processor=${3}
+
+       local path="${SYS_CLASS_NET}/${device}/queues/${queue}/rps_cpus"
+       assert [ -w "${path}" ]
+
+       log DEBUG "Setting SMP affinity of ${device} (${queue}) to processor ${processor}"
+
+       __processor_id_to_bitmap ${processor} > ${path}
+}
+
+# Tries to find a device which has the given IP address assigned
+device_get_by_assigned_ip_address() {
+       local ip=${1}
+
+       assert isset ip
+
+       local device
+
+       # Read the first line of ip addr show to
+       read -r device <<< $(ip addr show to "${ip}")
+
+       # If we did not found a device we return with ${EXIT_ERROR}
+       if ! isset device; then
+               return ${EXIT_ERROR}
+       fi
+
+       # We get something like:
+       # 3: upl0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
+       # and we want upl0 so we take the second word and removing the :
+       device=(${device})
+       device=${device[1]}
+       device=${device%:}
+
+       print "${device}"
+       return ${EXIT_OK}
+}