# 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
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}
+
+ local ifindex_path="${SYS_CLASS_NET}/${device}/brport/bridge/ifindex"
+ [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
- [ -e "/proc/net/vlan/${device}" ]
+ 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}" ]
+}
+
+# Check if the device has vlan devices
+function device_has_vlans() {
+ local device=${1}
+ assert isset device
- if [ ! -e "/proc/net/vlan/config" ]; then
- return 1
+ 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_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
+
+ 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" ] && \
# Get the device type
function device_get_type() {
- local device=$(devicify ${1})
+ local device=${1}
- if device_is_virtual ${device}; then
+ if device_is_vlan ${device}; then
echo "vlan"
elif device_is_bonding ${device}; then
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"
function device_get_status() {
local device=${1}
-
assert isset device
- local status=${STATUS_UNKNOWN}
+ local status=${STATUS_DOWN}
- if ! device_has_carrier ${device}; then
- status=${STATUS_NOCARRIER}
- elif device_is_up ${device}; then
+ if device_is_up ${device}; then
status=${STATUS_UP}
- elif device_is_down ${device}; then
- status=${STATUS_DOWN}
- fi
- assert isset status
+ if ! device_has_carrier ${device}; then
+ status=${STATUS_NOCARRIER}
+ fi
+ fi
echo "${status}"
}
# 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}
- ip link show ${device} | grep -qE "<.*PROMISC.*>"
+ device_has_flag ${device} 0x200
}
function device_set_promisc() {
# 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}
# 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}
local device=${1}
local parent
- if device_is_virtual ${device}; then
- parent=$(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=$(virtual_get_parent ${device})
+ if device_is_vlan ${device}; then
+ parent=$(vlan_get_parent ${device})
device_is_up ${parent} || return ${EXIT_OK}
assert isset device
assert isset file
- cat ${SYS_CLASS_NET}/${device}/${file}
+ local path="${SYS_CLASS_NET}/${device}/${file}"
+ [ -r "${path}" ] || return ${EXIT_ERROR}
+
+ echo "$(<${path})"
}
function device_get_rx_bytes() {
__device_get_file ${device} statistics/tx_errors
}
-function device_hotplug() {
+function device_get_speed() {
local device=${1}
- shift
-
- assert isset device
- #assert device_exists ${device}
- if ! device_is_free ${device}; then
- log ERROR "The device '${device}' does not look to be unused."
- log ERROR "Don't proceed."
- # return ${EXIT_ERROR}
- fi
+ __device_get_file ${device} speed
+}
- for port in $(ports_get_all); do
- port_cmd hotplug ${port} ${device}
- if [ $? -eq ${EXIT_OK} ]; then
- echo "${port}"
- exit ${EXIT_OK}
- fi
- done
+function device_get_duplex() {
+ local device=${1}
- exit ${EXIT_ERROR}
+ __device_get_file ${device} duplex
}