]> git.ipfire.org Git - people/arne_f/network.git/blobdiff - functions.device
Fix weird device CLI command.
[people/arne_f/network.git] / functions.device
index 102c5ceaa494ef5af515f4dea94f62a3a086eebf..1ddc7d2c9d9b34349dbb29fd1e3d87fdd93ec69a 100644 (file)
@@ -22,6 +22,8 @@
 function devicify() {
        local device=${1}
 
+       assert isset device
+
        if device_exists ${device}; then
                echo "${device}"
                return ${EXIT_OK}
@@ -41,6 +43,8 @@ function devicify() {
 function macify() {
        local device=${1}
 
+       assert isset device
+
        if mac_is_valid ${device}; then
                echo "${device}"
                return ${EXIT_OK}
@@ -80,20 +84,9 @@ function device_is_bonding() {
 
 # Check if the device bonded in a bonding device
 function device_is_bonded() {
-       local dev
-       for dev in /sys/class/net/*; do
-               # Skip crappy files
-               [ -d "${dev}" ] || continue
-
-               # Continue if not a bonding device
-               device_is_bonding "${dev##*/}" || continue
-
-               if grep -q "\<${1}\>" ${dev}/bonding/slaves; then
-                       return 0
-               fi
-       done
+       local device=${1}
 
-       return 1
+       [ -d "${SYS_CLASS_NET}/${device}/master" ]
 }
 
 # Check if the device is a bridge
@@ -128,12 +121,6 @@ function device_has_virtuals() {
        grep -q "${1}$" /proc/net/vlan/config
 }
 
-function device_is_vlan() { # XXX Compat function
-       log DEBUG "Deprecated function device_is_vlan() was used."
-
-       device_is_virtual $@
-}
-
 # Check if the device is a ppp device
 function device_is_ppp() {
        local device=${1}
@@ -166,6 +153,9 @@ function device_is_real() {
        device_is_virtual ${device} && \
                return ${EXIT_ERROR}
 
+       [ "$(__device_get_file ${device} type)" != "1" ] && \
+               return ${EXIT_ERROR}
+
        return ${EXIT_OK}
 }
 
@@ -173,7 +163,7 @@ function device_is_real() {
 function device_get_type() {
        local device=$(devicify ${1})
 
-       if device_is_vlan ${device}; then
+       if device_is_virtual ${device}; then
                echo "vlan"
 
        elif device_is_bonding ${device}; then
@@ -196,6 +186,26 @@ function device_get_type() {
        fi
 }
 
+function device_get_status() {
+       local device=${1}
+
+       assert isset device
+
+       local status=${STATUS_UNKNOWN}
+
+       if ! device_has_carrier ${device}; then
+               status=${STATUS_NOCARRIER}
+       elif device_is_up ${device}; then
+               status=${STATUS_UP}
+       elif device_is_down ${device}; then
+               status=${STATUS_DOWN}
+       fi
+
+       assert isset status
+
+       echo "${status}"
+}
+
 function device_get_address() {
        local device=${1}
 
@@ -233,11 +243,25 @@ function device_set_address() {
        return ${ret}
 }
 
-function devices_get_all() {
+function device_get() {
        local device
+       local devices
+
        for device in ${SYS_CLASS_NET}/*; do
-               echo "$(basename ${device})"
-       done | sort
+               device=$(basename ${device})
+
+               # bonding_masters is no device
+               [ "${device}" = "bonding_masters" ] && continue
+
+               devices="${devices} ${device}"
+       done
+
+       echo ${devices}
+       return ${EXIT_OK}
+}
+
+function devices_get_all() {
+       device_get
 }
 
 # Check if a device has a cable plugged in
@@ -246,6 +270,23 @@ function device_has_carrier() {
        [ "$(<${SYS_CLASS_NET}/${device}/carrier)" = "1" ]
 }
 
+function device_is_promisc() {
+       local device=${1}
+
+       ip link show ${device} | grep -qE "<.*PROMISC.*>"
+}
+
+function device_set_promisc() {
+       local device=${1}
+       local state=${2}
+
+       assert device_exists ${device}
+       assert isset state
+       assert isoneof state on off
+
+       ip link set ${device} promisc ${state}
+}
+
 # Check if the device is free
 function device_is_free() {
        ! device_is_used $@
@@ -265,41 +306,20 @@ function device_is_used() {
        return ${EXIT_ERROR}
 }
 
-# XXX to be removed I think
-function device_get_free() {
-       local destination=${1}
-
-       # Replace + by a valid number
-       if grep -q "+$" <<<${destination}; then
-               local number=0
-               destination=$(sed -e "s/+//" <<<$destination)
-               while [ "${number}" -le "100" ]; do
-                       if ! device_exists "${destination}${number}"; then
-                               destination="${destination}${number}"
-                               break
-                       fi
-                       number=$(($number + 1))
-               done
-       fi
-       echo "${destination}"
-}
-
-function device_rename() {
-       warning_log "Called deprecated function 'device_rename'"
-
-       device_set_name $@
-}
-
 function device_hash() {
        local device=${1}
 
-       macify ${device} | tr -d ':'
+       # Get mac address of device and remove all colons (:)
+       # that will result in a hash.
+       device=$(macify ${device})
+
+       echo "${device//:/}"
 }
 
 # Give the device a new name
 function device_set_name() {
        local source=$1
-       local destination=$(device_get_free ${2})
+       local destination=${2}
 
        # Check if devices exists
        if ! device_exists ${source} || device_exists ${destination}; then
@@ -323,6 +343,9 @@ function device_set_name() {
 function device_set_up() {
        local device=$(devicify ${1})
 
+       # Silently fail if device was not found
+       [ -z "${device}" ] && return ${EXIT_ERROR}
+
        # Do nothing if device is already up
        device_is_up ${device} && return ${EXIT_OK}
 
@@ -338,7 +361,7 @@ function device_set_parent_up() {
        local parent
 
        if device_is_virtual ${device}; then
-               parent=$(device_virtual_get_parent ${device})
+               parent=$(virtual_get_parent ${device})
 
                device_is_up ${parent} && return ${EXIT_OK}
 
@@ -374,7 +397,7 @@ function device_set_parent_down() {
        local parent
 
        if device_is_virtual ${device}; then
-               parent=$(device_virtual_get_parent ${device})
+               parent=$(virtual_get_parent ${device})
 
                device_is_up ${parent} || return ${EXIT_OK}
 
@@ -396,7 +419,7 @@ function device_get_mtu() {
                return ${EXIT_ERROR}
        fi
 
-       cat ${SYS_CLASS_NET}/${device}/mtu
+       echo $(<${SYS_CLASS_NET}/${device}/mtu)
 }
 
 # Set mtu to a device
@@ -444,281 +467,85 @@ function device_discover() {
        log INFO "Running discovery process on device '${device}'."
 
        local hook
-       for hook in $(hooks_get_all); do
-               hook_exec ${hook} discover ${device}
+       for hook in $(hook_zone_get_all); do
+               hook_zone_exec ${hook} discover ${device}
        done
 }
 
-function device_create_virtual() {
-       log WARN "Called deprecated function device_create_virtual"
-       device_virtual_create $@
-}
-
-function device_virtual_create() {
-       local port=$(devicify ${1})
-       local vid=${2}
-       local mac=${3}
-       local newport=${port}v${vid}
-       
-       if [ -z "${mac}" ]; then
-               mac=$(mac_generate)
-       fi
-
-       log INFO "Creating virtual device '${newport}' with address '${mac}'."
-
-       local oldport=$(device_virtual_get_by_parent_and_vid ${port} ${vid})
-
-       if device_exists ${oldport}; then
-               local differences
-
-               if [ "${oldport}" != "${newport}" ]; then
-                       differences="${differences} name"
-               fi
-               if [ "$(device_get_address ${oldport})" != "${mac}" ]; then
-                       differences="${differences} address"
-               fi
-               
-               echo "differences: $differences"
-
-               if [ -n "${differences}" ]; then
-                       if device_is_used ${oldport}; then
-                               error_log "There was a device '${oldport}' set up with VID '${vid}' and parent '${port}' which is used somewhere else. Cannot go on." 
-                               return ${EXIT_ERROR}
-                       else
-                               log DEBUG "There is a device '${oldport}' but it not used, so we grab it to ourselves."
-                       fi
-               else
-                       log DEBUG "Device '${newport}' already exists and reflects our configuration. Go on."
-
-                       device_set_up ${oldport}
-                       return ${EXIT_OK}
-               fi
-
-       else
-               log DEBUG "Virtual device '${newport}' does not exist, yet."
-
-               vconfig set_name_type DEV_PLUS_VID_NO_PAD >/dev/null
-               vconfig add ${port} ${vid} >/dev/null
-               
-               if [ $? -ne ${EXIT_OK} ]; then
-                       error_log "Could not create virtual device '${newport}'."
-                       return ${EXIT_ERROR}
-               fi
-
-               oldport=$(device_virtual_get_by_parent_and_vid ${port} ${vid})
-
-       fi
-
-       assert device_exists ${oldport}
-
-       if ! device_exists ${oldport}; then
-               error "Could not determine the created virtual device '${newport}'."
-               return ${EXIT_ERROR}
-       fi
-
-       # The device is expected to be named like ${port}.${vid}
-       # and will be renamed to the virtual schema
-       device_set_name ${oldport} ${newport}
-
-       if [ $? -ne ${EXIT_OK} ]; then
-               error_log "Could not set name of virtual device '${newport}'."
-               return ${EXIT_ERROR}
-       fi
-
-       assert device_exists ${newport}
-
-       # Setting new mac address
-       device_set_address ${newport} ${mac}
-       
-       if [ $? -ne ${EXIT_OK} ]; then
-               error_log "Could not set address '${mac}' to virtual device '${newport}'."
-               return ${EXIT_ERROR}
-       fi
-
-       # Bring up the new device
-       device_set_up ${newport}
-
-       return ${EXIT_OK}
-}
-
-function device_virtual_remove() {
-       local device=$(devicify ${1})
-
-       log INFO "Removing virtual device '${device}' with address '$(macify ${device})'."
-
-       device_set_down ${device}
-
-       vconfig rem ${device} >/dev/null
-
-       if [ $? -ne ${EXIT_OK} ]; then
-               error_log "Could not remote virtual device '${newport}'."
-               return ${EXIT_ERROR}
-       fi
-
-       return ${EXIT_OK}
-}
-
-function device_virtual_get_parent() {
+function device_has_ip() {
        local device=${1}
+       local addr=${2}
 
-       local parent=$(grep "^${device}" < /proc/net/vlan/config | awk '{ print $NF }')
+       assert isset addr
+       assert device_exists ${device}
 
-       if device_exists ${parent}; then
-               echo "${parent}"
-               return ${EXIT_OK}
-       fi
+       # IPv6 addresses must be fully imploded
+       local protocol=$(ip_detect_protocol ${addr})
+       case "${protocol}" in
+               ipv6)
+                       addr=$(ipv6_implode ${addr})
+                       ;;
+       esac
 
-       return ${EXIT_ERROR}
+       listmatch ${addr} $(device_get_addresses ${device})
 }
 
-function device_virtual_get_by_parent_and_vid() {
-       local parent=${1}
-       local vid=${2}
+function device_get_addresses() {
+       local device=${1}
 
-       local v_port
-       local v_id
-       local v_parent
+       assert device_exists ${device}
 
-       fgrep '|' < /proc/net/vlan/config | tr -d '|' | \
-               while read v_port v_id v_parent; do
-                       if [ "${v_parent}" = "${parent}" ] && [ "${v_id}" = "${vid}" ]; then
-                               echo "${v_port}"
-                               return ${EXIT_OK}
-                       fi
+       local prot
+       local addr
+       local line
+       ip addr show ${device} | \
+               while read prot addr line; do
+                       [ "${prot:0:4}" = "inet" ] && echo "${addr}"
                done
-
-       return ${EXIT_ERROR}
 }
 
-function device_bonding_create() {
+function __device_get_file() {
        local device=${1}
-       local mac=${2}
-
-       [ -z "${mac}" ] && mac=$(mac_generate)
-
-       log INFO "Creating bonding device '${device}' (${mac})."
-
-       echo "+${device}" > /sys/class/net/bonding_masters
-       device_set_address ${mac}
-       device_set_up ${device}
-}
-
-function device_bonding_remove() {
-       local device=$(devicify ${1})
+       local file=${2}
 
-       log INFO "Remove bonding device '${device}'."
+       assert isset device
+       assert isset file
 
-       device_set_down ${device}
-       echo "-${device}" > /sys/class/net/bonding_masters
+       cat ${SYS_CLASS_NET}/${device}/${file}
 }
 
-function bonding_set_mode() {
+function device_get_rx_bytes() {
        local device=${1}
-       local mode=${2}
-
-       log INFO "Setting bonding mode on '${device}' '${mode}'."
-
-       echo "${mode}" > /sys/class/net/${device}/bonding/mode
-}
-
-function bonding_enslave_device() {
-       local device=$(devicify ${1})
-       local slave=$(devicify ${2})
-       shift 2
-
-       log INFO "Enslaving slave '${slave}' to '${device}'."
 
-       device_set_down ${slave}
-       echo "+${slave}" > /sys/class/net/${device}/bonding/slaves
+       __device_get_file ${device} statistics/rx_bytes
 }
 
-function bridge_attach_device() {
-       local bridge=${1}
-       local device=${2}
-       
-       if ! device_exists ${bridge}; then
-               error "Bridge '${bridge}' does not exist."
-               return ${EXIT_ERROR}
-       fi
-
-       if ! device_exists ${device}; then
-               error "Device '${device}' does not exist."
-               return ${EXIT_ERROR}
-       fi
-
-       log INFO "Attaching device '${device}' to bridge '${bridge}'."
-
-       # XXX device_set_up ${device} # Do we need this here?
+function device_get_tx_bytes() {
+       local device=${1}
 
-       brctl addif ${bridge} ${device}
+       __device_get_file ${device} statistics/tx_bytes
 }
 
-function bridge_detach_device() {
-       local bridge=${1}
-       local device=${2}
-
-       if ! device_exists ${bridge}; then
-               error "Bridge '${bridge}' does not exist."
-               return ${EXIT_ERROR}
-       fi
-
-       if ! device_exists ${device}; then
-               error "Device '${device}' does not exist."
-               return ${EXIT_ERROR}
-       fi
-
-       log INFO "Detaching device '${device}' from bridge '${bridge}'."
-
-       brctl delif ${bridge} ${device}
+function device_get_rx_packets() {
+       local device=${1}
 
-       device_set_down ${device}
+       __device_get_file ${device} statistics/rx_packets
 }
 
-function bridge_is_forwarding() {
-       local seconds=45
-       local zone=${1}
-
-       bridge_has_carrier ${zone} || return ${EXIT_ERROR}
-
-       local device
-       while [ ${seconds} -gt 0 ]; do
-               for device in ${SYS_CLASS_NET}/${zone}/brif/*; do
-                       [ -e "${device}/state" ] || continue
-                       if [ "$(<${device}/state)" = "3" ]; then
-                               return ${EXIT_OK}
-                       fi
-               done
-               sleep 1
-               seconds=$((${seconds} - 1))
-       done
+function device_get_tx_packets() {
+       local device=${1}
 
-       return ${EXIT_ERROR}
+       __device_get_file ${device} statistics/tx_packets
 }
 
-function bridge_has_carrier() {
-       local zone=${1}
-
-       local has_carrier=${EXIT_ERROR}
-
-       local device
-       for device in ${SYS_CLASS_NET}/${zone}/brif/*; do
-               device=$(basename ${device})
-               device_exists ${device} || continue
-
-               device_has_carrier ${device} && has_carrier=${EXIT_OK}
-       done
+function device_get_rx_errors() {
+       local device=${1}
 
-       return ${has_carrier}
+       __device_get_file ${device} statistics/rx_errors
 }
 
-function device_has_ipv4() {
+function device_get_tx_errors() {
        local device=${1}
-       local addr=${2}
-
-       if ! device_exists ${device}; then
-               error "Device '${device}' does not exist."
-               return ${EXIT_ERROR}
-       fi
 
-       ip addr show ${device} | grep -q -e "inet " -e "${addr}"
+       __device_get_file ${device} statistics/tx_errors
 }