]> git.ipfire.org Git - people/ms/network.git/blobdiff - functions.device
network: Experimental support for wireless access points.
[people/ms/network.git] / functions.device
index 8f28871359c14ff6d13040792c4ee388c9221cd0..d0ab0ce36085c60146637ee40c92a678c03edeb4 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}
@@ -173,7 +160,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 +183,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 +240,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
@@ -290,12 +311,6 @@ function device_get_free() {
        echo "${destination}"
 }
 
-function device_rename() {
-       warning_log "Called deprecated function 'device_rename'"
-
-       device_set_name $@
-}
-
 function device_hash() {
        local device=${1}
 
@@ -333,6 +348,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}
 
@@ -348,7 +366,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}
 
@@ -384,7 +402,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}
 
@@ -454,281 +472,84 @@ 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}
+function device_has_ipv4() {
+       local device=${1}
+       local addr=${2}
 
-       # 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}'."
+       if ! device_exists ${device}; then
+               error "Device '${device}' does not exist."
                return ${EXIT_ERROR}
        fi
 
-       # Bring up the new device
-       device_set_up ${newport}
-
-       return ${EXIT_OK}
+       ip addr show ${device} | grep -q -e "inet " -e "${addr}"
 }
 
-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
+function device_has_ipv6() {
+       local device=${1}
+       local addr=${2}
 
-       if [ $? -ne ${EXIT_OK} ]; then
-               error_log "Could not remote virtual device '${newport}'."
+       if ! device_exists ${device}; then
+               error "Device '${device}' does not exist."
                return ${EXIT_ERROR}
        fi
 
-       return ${EXIT_OK}
-}
-
-function device_virtual_get_parent() {
-       local device=${1}
+       local prefix=${addr##*/}
+       addr=$(ipv6_implode ${addr%%/*})
 
-       local parent=$(grep "^${device}" < /proc/net/vlan/config | awk '{ print $NF }')
-
-       if device_exists ${parent}; then
-               echo "${parent}"
-               return ${EXIT_OK}
+       if [ -n "${prefix}" ]; then
+               addr="${addr}/${prefix}"
        fi
 
-       return ${EXIT_ERROR}
-}
-
-function device_virtual_get_by_parent_and_vid() {
-       local parent=${1}
-       local vid=${2}
-
-       local v_port
-       local v_id
-       local v_parent
-
-       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
-               done
-
-       return ${EXIT_ERROR}
+       ip addr show ${device} | grep -q "inet6 ${addr}"
 }
 
-function device_bonding_create() {
+function __device_get_file() {
        local device=${1}
-       local mac=${2}
+       local file=${2}
 
-       [ -z "${mac}" ] && mac=$(mac_generate)
+       assert isset device
+       assert isset file
 
-       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})
-
-       log INFO "Remove bonding device '${device}'."
-
-       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
+       __device_get_file ${device} statistics/rx_bytes
 }
 
-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
-}
-
-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
 }