]> git.ipfire.org Git - people/stevee/network.git/commitdiff
Fix "network device" command and document it.
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 7 Jun 2012 13:31:58 +0000 (13:31 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 7 Jun 2012 13:31:58 +0000 (13:31 +0000)
functions.cli
functions.device
hooks/ports/bonding
man/Makefile
man/network-device.8.in [new file with mode: 0644]
network
udev/network-hotplug

index 3866495cdbfa260e0a449d983c7d9eda4dc2e9e4..a884e327332599b762c113f80e7b066a58b2b55f 100644 (file)
@@ -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() {
index e092b67b1ff110ef39761fe0a6d726bd0706c800..a15586ecf7ec08a71f6ec76ff5e2f68e63dc5f27 100644 (file)
@@ -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
+}
index 880d987e274c67de256672dacae5eb35214ffc14..58ae34521f36a41b6a0c9158f9657d5ae0b19e79 100755 (executable)
@@ -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
index 23ee47492b52ea81205f5fec4d4e5fcafc230c3c..285f9a36369a002c76b2ed3d000b785a92e9ae37 100644 (file)
@@ -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 (file)
index 0000000..bb65e00
--- /dev/null
@@ -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 <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<device> 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<device> 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 d11d2ac789d29ce1a1eaeb82ba1f630f113c3ea1..614efcf1f96710211bd0a9b9e5e4d82ce4f0f86f 100755 (executable)
--- 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() {
index eb49f92fc8c2d31731acea1ef302bdc2b12e4222..063d33fb656379cbdca519f7cd16458f3857782e 100755 (executable)
@@ -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