diff options
author | dteske <dteske@FreeBSD.org> | 2014-01-15 07:42:31 +0000 |
---|---|---|
committer | dteske <dteske@FreeBSD.org> | 2014-01-15 07:42:31 +0000 |
commit | 44abef95373cf49911f4c9bf27ad329cccbd3a05 (patch) | |
tree | 7aeb1ba6a3adf35cc19d1331c5df50b19f52e32f /usr.sbin/bsdconfig/share | |
parent | a623494f5ed13ab9649c5e3e09428310f68164d9 (diff) | |
download | FreeBSD-src-44abef95373cf49911f4c9bf27ad329cccbd3a05.zip FreeBSD-src-44abef95373cf49911f4c9bf27ad329cccbd3a05.tar.gz |
MFC r258458:
Improve network device scanning in the netdev module. First, make it use the
`device.subr' framework (improving performane and reducing sub-shells). Next
improve the `device.subr' framework itself. Make use of the `flags' device
struct member for network interfaces to indicate if an interface is Active,
Wired Ethernet, or 802.11 Wireless. Functions have been added to make checks
against the `flags' bit-field quick and efficient. Last, add function for
rescanning the network to update the device registers. Remove an unnecessary
local (ifn) while we're here (use already provided local `if').
Diffstat (limited to 'usr.sbin/bsdconfig/share')
-rw-r--r-- | usr.sbin/bsdconfig/share/device.subr | 165 |
1 files changed, 151 insertions, 14 deletions
diff --git a/usr.sbin/bsdconfig/share/device.subr b/usr.sbin/bsdconfig/share/device.subr index f15c067..f5d8ad7 100644 --- a/usr.sbin/bsdconfig/share/device.subr +++ b/usr.sbin/bsdconfig/share/device.subr @@ -76,6 +76,11 @@ setvar DEVICE_TYPE_ANY 11 setvar DEVICE_TYPE_HTTP_PROXY 12 setvar DEVICE_TYPE_HTTP 13 +# Network devices have the following flags available +setvar IF_ETHERNET 1 +setvar IF_WIRELESS 2 +setvar IF_ACTIVE 4 + # # Default behavior is to call f_device_get_all() automatically when loaded. # @@ -175,6 +180,33 @@ f_device_reset() DEVICES= } +# f_device_reset_network +# +# Reset the registered network device chain. +# +f_device_reset_network() +{ + local dev type private pruned_list= + for dev in $DEVICES; do + device_$dev get type type + if [ "$type" != "$DEVICE_TYPE_NETWORK" ]; then + pruned_list="$pruned_list $dev" + continue + fi + + # + # Leave the device up (don't call shutdown routine) + # + + # Network devices may have DEVICE_INFO private member + device_$dev get private private + [ "$private" ] && f_struct_free "$private" + + f_struct_free device_$dev + done + DEVICES="${pruned_list# }" +} + # f_device_get_all # # Get all device information for devices we have attached. @@ -187,20 +219,7 @@ f_device_get_all() f_dialog_info "$msg_probing_devices_please_wait_this_can_take_a_while" # First go for the network interfaces - for devname in $( ifconfig -l ); do - # Eliminate network devices that don't make sense - case "$devname" in - lo*) continue ;; - esac - - # Try and find its description - f_device_desc "$devname" $DEVICE_TYPE_NETWORK desc - - f_dprintf "Found a network device named %s" "$devname" - f_device_register $devname \ - "$desc" "$devname" $DEVICE_TYPE_NETWORK 1 \ - f_media_init_network "" f_media_shutdown_network "" -1 - done + f_device_get_all_network # Next, try to find all the types of devices one might use # as a media source for content @@ -378,6 +397,48 @@ f_device_get_all() done # disks } +# f_device_get_all_network +# +# Get all network device information for attached network devices. +# +f_device_get_all_network() +{ + local devname desc flags + for devname in $( ifconfig -l ); do + # Eliminate network devices that don't make sense + case "$devname" in + lo*) continue ;; + esac + + # Try and find its description + f_device_desc "$devname" $DEVICE_TYPE_NETWORK desc + + f_dprintf "Found a network device named %s" "$devname" + f_device_register $devname \ + "$desc" "$devname" $DEVICE_TYPE_NETWORK 1 \ + f_media_init_network "" f_media_shutdown_network "" -1 + + # Set flags based on media and status + flags=0 + eval "$( ifconfig $devname 2> /dev/null | awk -v var=flags ' + function _or(var, mask) { + printf "%s=$(( $%s | $%s ))\n", var, var, mask + } + BEGIN { S = "[[:space:]]+" } + { + if (!match($0, "^" S "(media|status):" S)) next + value = substr($0, RLENGTH + 1) + if ($1 == "media:") { + if (value ~ /Ethernet/) _or(var, "IF_ETHERNET") + if (value ~ /802\.11/) _or(var, "IF_WIRELESS") + } else if ($1 == "status:") { + if (value ~ /^active/) _or(var, "IF_ACTIVE") + } + }' )" + device_$devname set flags $flags + done +} + # f_device_name_get $type $name type|desc|max [$var_to_set] # # Fetch the device type (type), description (desc), or maximum number of @@ -571,6 +632,72 @@ f_device_desc() return $FAILURE } +# f_device_is_ethernet $device +# +# Returns true if $device is a wired Ethernet network interface. Otherwise +# returns false. Example wired interfaces include: fxp0 em0 bge0 rl0 etc. +# +f_device_is_ethernet() +{ + local dev="$1" type flags + + # Make sure we have an actual device by that name + f_struct "device_$dev" || return $FAILURE + + # Make sure that the device is a network device + device_$dev get type type + [ "$type" = "$DEVICE_TYPE_NETWORK" ] || return $FAILURE + + # Make sure that the media flags indicate that it is Ethernet + device_$dev get flags flags + [ $(( ${flags:-0} & $IF_ETHERNET )) -eq $IF_ETHERNET ] +} + +# f_device_is_wireless $device +# +# Returns true if $device is a Wireless network interface. Otherwise returns +# false. Examples of wireless interfaces include: iwn0 +# +f_device_is_wireless() +{ + local dev="$1" type flags + + # Make sure we have an actual device by that name + f_struct "device_$dev" || return $FAILURE + + # Make sure that the device is a network device + device_$dev get type type + [ "$type" = "$DEVICE_TYPE_NETWORK" ] || return $FAILURE + + # Make sure that the media flags indicate that it is Ethernet + device_$dev get flags flags + [ $(( ${flags:-0} & $IF_WIRELESS )) -eq $IF_WIRELESS ] +} + +# f_device_is_active $device +# +# Returns true if $device is active. Otherwise returns false. Currently this +# only works for network interfaces. +# +f_device_is_active() +{ + local dev="$1" type flags=0 + + # Make sure we have an actual device by that name + f_struct "device_$dev" || return $FAILURE + + device_$dev get type type + case "$type" in + $DEVICE_TYPE_NETWORK) + # Make sure that the media flags indicate that it is active + device_$dev get flags flags + [ $(( ${flags:-0} & $IF_ACTIVE )) -eq $IF_ACTIVE ] + ;; + *) + return $FAILURE + esac +} + # f_device_rescan # # Rescan all devices, after closing previous set - convenience function. @@ -581,6 +708,16 @@ f_device_rescan() f_device_get_all } +# f_device_rescan_network +# +# Rescan all network devices, after closing previous set - for convenience. +# +f_device_rescan_network() +{ + f_device_reset_network + f_device_get_all_network +} + # f_device_find $name [$type [$var_to_set]] # # Find one or more registered devices by name, type, or both. Returns a space- |