From: Michael Tremer Date: Thu, 7 Jun 2012 13:31:58 +0000 (+0000) Subject: Fix "network device" command and document it. X-Git-Tag: 004~46 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ec63256ada01539ba81cbc635986557c8fc085a8;p=network.git Fix "network device" command and document it. --- diff --git a/functions.cli b/functions.cli index 3866495c..a884e327 100644 --- a/functions.cli +++ b/functions.cli @@ -31,6 +31,12 @@ function cli_help_requested() { return ${EXIT_ERROR} } +function cli_zone_headline() { + local zone=${1} + + cli_status_headline ${zone} +} + function cli_status_headline() { local zone=${1} @@ -41,8 +47,68 @@ function cli_status_headline() { } function cli_headline() { - echo - echo -e "${COLOUR_BOLD}$@${COLOUR_NORMAL}" + local level=${1} + shift + + local message="$@" + local ident=$(cli_ident ${level}) + + printf "${ident}${COLOUR_BOLD}$@${COLOUR_NORMAL}\n" +} + +function cli_print() { + local level=${1} + local format=${2} + shift 2 + + local ident=$(cli_ident $(( ${level} + 1 ))) + + local out + printf -v out "${ident}${format}\n" "$@" + printf "${out}" +} + +function cli_print_fmt1() { + local level=${1} + shift + + local space=$(( 30 - (${level} * 4) )) + local format="%-${space}s %s" + + cli_print ${level} "${format}" "$@" +} + +function cli_print_bool() { + if [ "${1}" = "${EXIT_TRUE}" ]; then + echo "true" + else + echo "false" + fi +} + +function cli_print_yesno() { + if [ "${1}" = "${EXIT_TRUE}" ]; then + echo "yes" + else + echo "false" + fi +} + +function cli_space() { + printf "\n" +} + +function cli_ident() { + local level=${1} + shift + + local ident="" + while [ ${level} -gt 1 ]; do + ident="${ident} " + level=$(( ${level} - 1 )) + done + + echo "${ident}" } function cli_yesno() { diff --git a/functions.device b/functions.device index e092b67b..a15586ec 100644 --- a/functions.device +++ b/functions.device @@ -125,13 +125,20 @@ function device_has_virtuals() { local device=${1} if device_is_virtual ${device}; then - return 1 + return ${EXIT_FALSE} fi - if [ ! -e "/proc/net/vlan/config" ]; then - return 1 - fi - grep -q "${1}$" /proc/net/vlan/config + local virtuals=$(device_get_virtuals ${device}) + [ -n "${virtuals}" ] && return ${EXIT_OK} || return ${EXIT_ERROR} +} + +function device_get_virtuals() { + local device=${1} + + local dev spacer1 id spacer2 parent + while read dev spacer1 id spacer2 parent; do + [ "${parent}" = "${device}" ] && echo "${dev}" + done < /proc/net/vlan/config | sort } # Check if the device is a ppp device @@ -165,7 +172,7 @@ function device_is_wireless() { } # Check if the device is a physical network interface -function device_is_real() { +function device_is_ethernet() { local device=${1} device_is_loopback ${device} && \ @@ -211,8 +218,8 @@ function device_get_type() { elif device_is_wireless ${device}; then echo "wireless" - elif device_is_real ${device}; then - echo "real" + elif device_is_ethernet ${device}; then + echo "ethernet" else echo "unknown" @@ -302,7 +309,8 @@ function device_has_carrier() { local device=${1} assert isset device - [ "$(<${SYS_CLASS_NET}/${device}/carrier)" = "1" ] + local carrier=$(__device_get_file ${device} carrier) + [ "${carrier}" = "1" ] } function device_is_promisc() { @@ -588,3 +596,15 @@ function device_get_tx_errors() { __device_get_file ${device} statistics/tx_errors } + +function device_get_speed() { + local device=${1} + + __device_get_file ${device} speed +} + +function device_get_duplex() { + local device=${1} + + __device_get_file ${device} duplex +} diff --git a/hooks/ports/bonding b/hooks/ports/bonding index 880d987e..58ae3452 100755 --- a/hooks/ports/bonding +++ b/hooks/ports/bonding @@ -81,7 +81,7 @@ function _edit() { local slave for slave in ${SLAVES}; do - if ! device_is_real $(devicify ${slave}); then + if ! device_is_ethernet $(devicify ${slave}); then error "Slave device '${slave}' is not an ethernet device." exit ${EXIT_ERROR} fi diff --git a/man/Makefile b/man/Makefile index 23ee4749..285f9a36 100644 --- a/man/Makefile +++ b/man/Makefile @@ -28,6 +28,7 @@ MANPAGES_IN = $(foreach file,$(MANPAGES),$(file).in) MANPAGES8 = \ network.8 \ network-config.8 \ + network-device.8 \ network-zone.8 .PHONY: all @@ -35,7 +36,7 @@ all: $(MANPAGES) @: # Do nothing. # Replace all placeholders. -$(MANPAGES): Makefile $(MANPAGES_IN) +$(MANPAGES): ../Makeconfig Makefile $(MANPAGES_IN) sed \ -e "s/@VERSION@/$(PACKAGE_VERSION)/g" \ < $@.in > $@ diff --git a/man/network-device.8.in b/man/network-device.8.in new file mode 100644 index 00000000..bb65e00b --- /dev/null +++ b/man/network-device.8.in @@ -0,0 +1,37 @@ +.TH network-device 8 "6 Jun 2012" "@VERSION@" "network man page" + +.SH NAME +network-device \- Network Configuration Control Program + +.SH SYNOPSIS +\fBnetwork [OPTIONS] device [status|discover] ...\fR + +.SH DESCRIPTION +By the device subcommands, it is very easy to get status information +about network devices and do some more things. + +.SH OPTIONS +The \fBnetwork device\fR command offers various commands: + +\fB status\fR +.RS 4 +This will show you very detailed information about the given device. +.PP +This is all about the ethernet parts of the device and does not contain +any IP information as this is defined as a zone (\fBnetwork-zone\fR(8)). +.RE +.PP + +\fB discover\fR +.RS 4 +Runs a discovery for many hooks on the given device. This will check +if the hook can find for example a DHCP server or DSLAM and thus predict +for what the device should be used. +.RE +.PP + +.SH SEE ALSO +network(8) + +.SH AUTHOR +Michael Tremer (michael.tremer@ipfire.org) diff --git a/network b/network index d11d2ac7..614efcf1 100755 --- a/network +++ b/network @@ -54,28 +54,108 @@ function cli_device() { local action=${2} shift 2 - assert device_exists ${device} - - if zone_exists ${device} || port_exists ${device}; then - error "The device '${device}' has already been configured." - error "You cannot do a device action." + if ! isset device; then + cli_show_man network-device return ${EXIT_ERROR} fi + assert device_exists ${device} + case "${action}" in discover) - echo "# XXX need to implement --raw here" cli_device_discover ${device} $@ ;; - - show|"") - # XXX device_show needs to be implemented - device_show ${device} + status) + cli_device_status ${device} ;; *) cli_show_man network-device ;; esac + + return ${EXIT_OK} +} + +function cli_device_status() { + local device=${1} + assert device_exists ${device} + + # Save the type of the device for later. + local type=$(device_get_type ${device}) + + cli_headline 1 "Device status: ${device}" + cli_print_fmt1 1 "Name" "${device}" + + # Print the device status. + device_is_up ${device} &>/dev/null + local status=$? + + case "${status}" in + ${EXIT_TRUE}) + status="${COLOUR_GREEN}UP${COLOUR_NORMAL}" + ;; + ${EXIT_FALSE}) + status="${COLOUR_RED}DOWN${COLOUR_NORMAL}" + ;; + esac + + cli_print_fmt1 1 "Status" "${status}" + cli_print_fmt1 1 "Type" "${type}" + cli_print_fmt1 1 "Address" "$(device_get_address ${device})" + cli_space + + # Print the link speed for ethernet devices. + case "${type}" in + ethernet) + cli_print_fmt1 1 "Link" \ + "$(device_get_speed ${device}) MBit/s $(device_get_duplex ${device}) duplex" + ;; + esac + + cli_print_fmt1 1 "MTU" "$(device_get_mtu ${device})" + cli_space + + # This section will print statistical data from the device. + local packets bytes errors + + cli_headline 2 "Statistics" + local format="%-10s %9d packets %6s (%d errors)" + + # RX + packets=$(device_get_rx_packets ${device}) + bytes=$(device_get_rx_bytes ${device}) + errors=$(device_get_rx_errors ${device}) + + cli_print 2 "${format}" "Received" "${packets}" "$(beautify_bytes ${bytes})" "${errors}" + + # TX + packets=$(device_get_tx_packets ${device}) + bytes=$(device_get_tx_bytes ${device}) + errors=$(device_get_tx_errors ${device}) + + cli_print 2 "${format}" "Sent" "${packets}" "$(beautify_bytes ${bytes})" "${errors}" + cli_space + + # Print some more information. + device_has_carrier ${device} &>/dev/null + cli_print_fmt1 1 "Has carrier?" "$(cli_print_bool $?)" + + device_is_promisc ${device} &>/dev/null + cli_print_fmt1 1 "Promisc" "$(cli_print_bool $?)" + cli_space + + # Print all virtual devices. + local virtuals=$(device_get_virtuals ${device}) + if [ -n "${virtuals}" ]; then + cli_headline 2 "Virtual devices" + + local virtual + for virtual in ${virtuals}; do + cli_print 2 "* %-6s - %s" "${virtual}" "$(device_get_address ${virtual})" + done + cli_space + fi + } function cli_device_discover() { diff --git a/udev/network-hotplug b/udev/network-hotplug index eb49f92f..063d33fb 100755 --- a/udev/network-hotplug +++ b/udev/network-hotplug @@ -33,7 +33,7 @@ assert isset INTERFACE # Check, if the device is a physical network interface and # if we can handle it. if device_exists ${INTERFACE}; then - if ! device_is_real ${INTERFACE}; then + if ! device_is_ethernet ${INTERFACE}; then log DEBUG "Called for interface '${INTERFACE}' which is a virtual interface. Exiting." exit ${EXIT_OK} fi