]> git.ipfire.org Git - people/arne_f/network.git/blobdiff - functions.device
network: Again very much changes that are hard to break down.
[people/arne_f/network.git] / functions.device
index 102c5ceaa494ef5af515f4dea94f62a3a086eebf..375d106508267e063f1424079a40bdcfebfb7a1b 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
@@ -196,6 +189,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 +246,40 @@ function device_set_address() {
        return ${ret}
 }
 
+function device_get() {
+       local from_config
+
+       while [ $# -gt 0 ]; do
+               case "${1}" in
+                       --from-config)
+                               from_config=1
+                               ;;
+                       --no-config)
+                               from_config=0
+                               ;;
+               esac
+               shift
+       done
+
+       local devices
+
+       if [ "${from_config}" != "0" ]; then
+               devices="${devices} $(device_config_list)"
+       fi
+
+       if [ "${from_config}" != "1" ]; then
+               local device
+               for device in ${SYS_CLASS_NET}/*; do
+                       devices="${devices} $(basename ${device})"
+               done
+       fi
+
+       echo ${devices}
+       return ${EXIT_OK}
+}
+
 function devices_get_all() {
-       local device
-       for device in ${SYS_CLASS_NET}/*; do
-               echo "$(basename ${device})"
-       done | sort
+       device_get
 }
 
 # Check if a device has a cable plugged in
@@ -246,6 +288,12 @@ function device_has_carrier() {
        [ "$(<${SYS_CLASS_NET}/${device}/carrier)" = "1" ]
 }
 
+function device_is_promisc() {
+       local device=${1}
+
+       ip link show ${device} | grep -qE "<.*PROMISC.*>"
+}
+
 # Check if the device is free
 function device_is_free() {
        ! device_is_used $@
@@ -293,7 +341,11 @@ function device_rename() {
 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
@@ -323,6 +375,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}
 
@@ -396,7 +451,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,8 +499,8 @@ 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
 }
 
@@ -459,7 +514,7 @@ function device_virtual_create() {
        local vid=${2}
        local mac=${3}
        local newport=${port}v${vid}
-       
+
        if [ -z "${mac}" ]; then
                mac=$(mac_generate)
        fi
@@ -575,10 +630,15 @@ function device_virtual_get_by_parent_and_vid() {
        local parent=${1}
        local vid=${2}
 
+       assert isset parent
+       assert isset vid
+
        local v_port
        local v_id
        local v_parent
 
+       assert [ -e "/proc/net/vlan/config" ]
+
        fgrep '|' < /proc/net/vlan/config | tr -d '|' | \
                while read v_port v_id v_parent; do
                        if [ "${v_parent}" = "${parent}" ] && [ "${v_id}" = "${vid}" ]; then
@@ -590,60 +650,19 @@ function device_virtual_get_by_parent_and_vid() {
        return ${EXIT_ERROR}
 }
 
-function device_bonding_create() {
-       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})
-
-       log INFO "Remove bonding device '${device}'."
-
-       device_set_down ${device}
-       echo "-${device}" > /sys/class/net/bonding_masters
-}
-
-function bonding_set_mode() {
-       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
-}
-
 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}
+       assert isset bridge
+       assert isset device
+
+       assert device_exists ${bridge}
+       assert device_exists ${device}
+
+       # If device is already attached, exit silently
+       if listmatch ${device} $(bridge_get_members ${bridge}); then
+               return ${EXIT_OK}
        fi
 
        log INFO "Attaching device '${device}' to bridge '${bridge}'."
@@ -657,6 +676,9 @@ function bridge_detach_device() {
        local bridge=${1}
        local device=${2}
 
+       assert isset bridge
+       assert isset device
+       
        if ! device_exists ${bridge}; then
                error "Bridge '${bridge}' does not exist."
                return ${EXIT_ERROR}
@@ -671,7 +693,21 @@ function bridge_detach_device() {
 
        brctl delif ${bridge} ${device}
 
-       device_set_down ${device}
+       #device_set_down ${device}
+}
+
+function bridge_get_members() {
+       local bridge=${1}
+
+       assert isset bridge
+
+       local member
+       for member in ${SYS_CLASS_NET}/${bridge}/brif/*; do
+               member=$(basename ${master})
+               if device_exists ${member}; then
+                       echo "${member}"
+               fi
+       done
 }
 
 function bridge_is_forwarding() {
@@ -722,3 +758,68 @@ function device_has_ipv4() {
 
        ip addr show ${device} | grep -q -e "inet " -e "${addr}"
 }
+
+function device_has_ipv6() {
+       local device=${1}
+       local addr=${2}
+
+       if ! device_exists ${device}; then
+               error "Device '${device}' does not exist."
+               return ${EXIT_ERROR}
+       fi
+
+       local prefix=${addr##*/}
+       addr=$(ipv6_implode ${addr%%/*})
+
+       if [ -n "${prefix}" ]; then
+               addr="${addr}/${prefix}"
+       fi
+
+       ip addr show ${device} | grep -q "inet6 ${addr}"
+}
+
+function __device_get_file() {
+       local device=${1}
+       local file=${2}
+
+       assert isset device
+       assert isset file
+
+       cat ${SYS_CLASS_NET}/${device}/${file}
+}
+
+function device_get_rx_bytes() {
+       local device=${1}
+
+       __device_get_file ${device} statistics/rx_bytes
+}
+
+function device_get_tx_bytes() {
+       local device=${1}
+
+       __device_get_file ${device} statistics/tx_bytes
+}
+
+function device_get_rx_packets() {
+       local device=${1}
+
+       __device_get_file ${device} statistics/rx_packets
+}
+
+function device_get_tx_packets() {
+       local device=${1}
+
+       __device_get_file ${device} statistics/tx_packets
+}
+
+function device_get_rx_errors() {
+       local device=${1}
+
+       __device_get_file ${device} statistics/rx_errors
+}
+
+function device_get_tx_errors() {
+       local device=${1}
+
+       __device_get_file ${device} statistics/tx_errors
+}