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}
# 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
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}
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
[ "$(<${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 $@
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_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}
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
}
local vid=${2}
local mac=${3}
local newport=${port}v${vid}
-
+
if [ -z "${mac}" ]; then
mac=$(mac_generate)
fi
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
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}'."
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}
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() {
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
+}