function devicify() {
local device=${1}
+ assert isset device
+
if device_exists ${device}; then
echo "${device}"
return ${EXIT_OK}
function macify() {
local device=${1}
+ assert isset device
+
if mac_is_valid ${device}; then
echo "${device}"
return ${EXIT_OK}
# If device name was not found, exit.
[ -n "${device}" ] || return ${EXIT_ERROR}
- [ -d "${SYS_CLASS_NET}/${device}" ]
+ # Check for a normal network device.
+ [ -d "${SYS_CLASS_NET}/${device}" ] && return ${EXIT_OK}
+
+ # If the check above, did not find a result,
+ # we check for serial devices.
+ serial_exists ${device}
+}
+
+function device_delete() {
+ local device=${1}
+ assert isset device
+
+ # Nothing to do, it device does not exist.
+ device_exists ${device} || return ${EXIT_OK}
+
+ # Delete the device.
+ cmd_quiet ip link delete ${device}
+ local ret=$?
+
+ if [ ${ret} -ne ${EXIT_OK} ]; then
+ log ERROR "device: Could not delete device '${device}': ${ret}"
+ return ${EXIT_ERROR}
+ fi
+
+ return ${ret}
+}
+
+function device_has_flag() {
+ local device=${1}
+ local flag=${2}
+
+ local flags=$(__device_get_file ${device} flags)
+
+ if [[ "$(( ${flags} & ${flag} ))" -eq 0 ]]; then
+ return ${EXIT_FALSE}
+ else
+ return ${EXIT_TRUE}
+ fi
}
# Check if the device is up
device_exists ${device} || return ${EXIT_ERROR}
- ip link show ${device} 2>/dev/null | grep -qE "<.*UP.*>"
+ device_has_flag ${device} 0x1
+}
+
+function device_ifindex_to_name() {
+ local idx=${1}
+ assert isset idx
+
+ local device device_idx
+ for device in ${SYS_CLASS_NET}/*; do
+ device=$(basename ${device})
+ device_exists ${device} || continue
+
+ device_idx=$(device_get_ifindex ${device})
+
+ if [ "${device_idx}" = "${idx}" ]; then
+ print "${device}"
+ return ${EXIT_OK}
+ fi
+ done
+
+ return ${EXIT_ERROR}
+}
+
+function device_get_ifindex() {
+ local device=${1}
+ assert isset device
+
+ local path="${SYS_CLASS_NET}/${1}/ifindex"
+
+ # Check if file can be read.
+ [ -r "${path}" ] || return ${EXIT_ERROR}
+
+ print "$(<${path})"
+}
+
+# Check if the device is a batman-adv bridge
+function device_is_batman_adv() {
+ [ -d "${SYS_CLASS_NET}/${1}/mesh" ]
+}
+
+# Check if the device is a batman-adv bridge port
+function device_is_batman_adv_port() {
+ [ -d "${SYS_CLASS_NET}/${1}/batman_adv" ]
}
# Check if the device is a bonding device
# 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
function device_is_bridge_attached() {
local device=${1}
-
[ -d "${SYS_CLASS_NET}/${device}/brport" ]
}
-# Check if the device is a virtual device
-function device_is_virtual() {
+function device_get_bridge() {
local device=${1}
+ assert isset device
+
+ # Check if device is attached to a bridge.
+ device_is_bridge_attached ${device} || return ${EXIT_ERROR}
- [ -e "/proc/net/vlan/${device}" ]
+ local ifindex_path="${SYS_CLASS_NET}/${device}/brport/bridge/ifindex"
+ [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
+
+ local ifindex=$(<${ifindex_path})
+ assert isset ifindex
+
+ device_ifindex_to_name ${ifindex}
}
-# Check if the device has virtual devices
-function device_has_virtuals() {
+# Check if the device is a vlan device
+function device_is_vlan() {
local device=${1}
+ assert isset device
- if device_is_virtual ${device}; then
- return 1
- fi
+ [ -e "${PROC_NET_VLAN}/${device}" ]
+}
- if [ ! -e "/proc/net/vlan/config" ]; then
- return 1
+# Check if the device has vlan devices
+function device_has_vlans() {
+ local device=${1}
+ assert isset device
+
+ if device_is_vlan ${device}; then
+ return ${EXIT_FALSE}
fi
- grep -q "${1}$" /proc/net/vlan/config
+
+ local vlans=$(device_get_vlans ${device})
+ [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
}
-function device_is_vlan() { # XXX Compat function
- log DEBUG "Deprecated function device_is_vlan() was used."
+function device_get_vlans() {
+ local device=${1}
+ assert isset device
+
+ # If no 8021q module has been loaded into the kernel,
+ # we cannot do anything.
+ [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK}
+
+ local dev spacer1 id spacer2 parent
+ while read dev spacer1 id spacer2 parent; do
+ [ "${parent}" = "${device}" ] || continue
- device_is_virtual $@
+ print "${dev}"
+ done < ${PROC_NET_VLAN_CONFIG}
}
# Check if the device is a ppp device
function device_is_ppp() {
local device=${1}
- ip link show ${device} 2>/dev/null | grep -qE "<.*POINTOPOINT.*>"
+ local type=$(__device_get_file ${device} type)
+
+ [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
+}
+
+# Check if the device is a pointopoint device.
+function device_is_ptp() {
+ local device=${1}
+
+ device_has_flag ${device} 0x10
}
# Check if the device is a loopback device
function device_is_loopback() {
- local device=$(devicify ${1})
+ local device=${1}
+
[ "${device}" = "lo" ]
}
+# Check if the device is a wireless device
+function device_is_wireless() {
+ local device=${1}
+
+ [ -d "${SYS_CLASS_NET}/${device}/phy80211" ]
+}
+
+function device_is_serial() {
+ serial_exists $@
+}
+
# Check if the device is a physical network interface
-function device_is_real() {
+function device_is_ethernet() {
local device=${1}
device_is_loopback ${device} && \
device_is_ppp ${device} && \
return ${EXIT_ERROR}
- device_is_virtual ${device} && \
+ device_is_vlan ${device} && \
+ return ${EXIT_ERROR}
+
+ [ "$(__device_get_file ${device} type)" != "1" ] && \
return ${EXIT_ERROR}
return ${EXIT_OK}
# Get the device type
function device_get_type() {
- local device=$(devicify ${1})
+ local device=${1}
if device_is_vlan ${device}; then
echo "vlan"
elif device_is_ppp ${device}; then
echo "ppp"
+ elif device_is_batman_adv ${device}; then
+ echo "batman-adv"
+
+ elif device_is_batman_adv_port ${device}; then
+ echo "batman-adv-port"
+
elif device_is_loopback ${device}; then
echo "loopback"
- elif device_is_real ${device}; then
- echo "real"
+ elif device_is_wireless ${device}; then
+ echo "wireless"
+
+ elif device_is_ethernet ${device}; then
+ echo "ethernet"
+
+ elif device_is_serial ${device}; then
+ echo "serial"
else
echo "unknown"
fi
}
+function device_get_status() {
+ local device=${1}
+ assert isset device
+
+ local status=${STATUS_DOWN}
+
+ if device_is_up ${device}; then
+ status=${STATUS_UP}
+
+ if ! device_has_carrier ${device}; then
+ status=${STATUS_NOCARRIER}
+ fi
+ fi
+
+ echo "${status}"
+}
+
function device_get_address() {
local device=${1}
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
function device_has_carrier() {
- local device=$(devicify ${1})
- [ "$(<${SYS_CLASS_NET}/${device}/carrier)" = "1" ]
+ local device=${1}
+ assert isset device
+
+ local carrier=$(__device_get_file ${device} carrier)
+ [ "${carrier}" = "1" ]
+}
+
+function device_is_promisc() {
+ local device=${1}
+
+ device_has_flag ${device} 0x200
+}
+
+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
# Check if the device is used
function device_is_used() {
- local device=$(devicify ${1})
+ local device=${1}
- device_has_virtuals ${device} && \
+ device_has_vlans ${device} && \
return ${EXIT_OK}
device_is_bonded ${device} && \
return ${EXIT_OK}
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
# Set device up
function device_set_up() {
- local device=$(devicify ${1})
+ local device=${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}
local device=${1}
local parent
- if device_is_virtual ${device}; then
- parent=$(device_virtual_get_parent ${device})
+ if device_is_vlan ${device}; then
+ parent=$(vlan_get_parent ${device})
device_is_up ${parent} && return ${EXIT_OK}
# Set device down
function device_set_down() {
- local device=$(devicify ${1})
+ local device=${1}
+ assert isset device
local ret=${EXIT_OK}
local device=${1}
local parent
- if device_is_virtual ${device}; then
- parent=$(device_virtual_get_parent ${device})
+ if device_is_vlan ${device}; then
+ parent=$(vlan_get_parent ${device})
device_is_up ${parent} || return ${EXIT_OK}
return ${EXIT_OK}
}
-# Set new address to a device
-function device_set_mac() {
- warning_log "Called deprecated function 'device_set_mac'"
-
- device_set_address $@
-}
-
function device_get_mtu() {
local device=${1}
return ${EXIT_ERROR}
fi
- cat ${SYS_CLASS_NET}/${device}/mtu
+ echo $(<${SYS_CLASS_NET}/${device}/mtu)
}
# Set mtu to a device
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}
+ local file=${2}
- [ -z "${mac}" ] && mac=$(mac_generate)
+ assert isset device
+ assert isset file
- log INFO "Creating bonding device '${device}' (${mac})."
+ local path="${SYS_CLASS_NET}/${device}/${file}"
+ [ -r "${path}" ] || return ${EXIT_ERROR}
- echo "+${device}" > /sys/class/net/bonding_masters
- device_set_mac ${mac}
- device_set_up ${device}
+ echo "$(<${path})"
}
-function device_bonding_remove() {
- local device=$(devicify ${1})
-
- log INFO "Remove bonding device '${device}'."
+function device_get_rx_bytes() {
+ local device=${1}
- device_set_down ${device}
- echo "-${device}" > /sys/class/net/bonding_masters
+ __device_get_file ${device} statistics/rx_bytes
}
-function bonding_set_mode() {
+function device_get_tx_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/tx_bytes
}
-function bonding_enslave_device() {
- local device=$(devicify ${1})
- local slave=$(devicify ${2})
- shift 2
-
- log INFO "Enslaving slave '${slave}' to '${device}'."
+function device_get_rx_packets() {
+ local device=${1}
- device_set_down ${slave}
- echo "+${slave}" > /sys/class/net/${device}/bonding/slaves
+ __device_get_file ${device} statistics/rx_packets
}
-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_packets() {
+ local device=${1}
- brctl addif ${bridge} ${device}
+ __device_get_file ${device} statistics/tx_packets
}
-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_errors() {
+ local device=${1}
- device_set_down ${device}
+ __device_get_file ${device} statistics/rx_errors
}
-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_errors() {
+ local device=${1}
- return ${EXIT_ERROR}
+ __device_get_file ${device} statistics/tx_errors
}
-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_speed() {
+ local device=${1}
- return ${has_carrier}
+ __device_get_file ${device} speed
}
-function device_has_ipv4() {
+function device_get_duplex() {
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} duplex
}